Reputation: 47
I am using Gtranslate plugin
for Translation in my wordpress website. The plugin generated a script which i attached in header.php
. When i tried reloading a page by default it is displayed in English (without being translated). When i clicked Finnish
at the top, it switched to Finnish. I want my website to be only in Finnish, so someone help me out in converting onclick
event to pageload
event.
<!-- GTranslate: http://gtranslate.net/ -->
<div class="switcher notranslate">
<div class="selected">
<a href="#" onclick="return false;">
<span class="gflag" style="background-position:-100px -100px;">
<img src="/gtranslate/blank.png" height="16" width="16" alt="fi" />
</span>Finnish
</a>
</div>
<div class="option">
<a href="#" onclick="doGTranslate('fi|fi');jQuery(this).parent().parent().find('div.selected a').html(jQuery(this).html());return false;" title="Finnish" class="nturl selected">
<span class="gflag" style="background-position:-100px -100px;">
<img src="/gtranslate/blank.png" height="16" width="16" alt="fi" />
</span>Finnish
</a>
</div>
</div>
<script type="text/javascript">
jQuery('.switcher .selected').click(function() {
if (!(jQuery('.switcher .option').is(':visible'))) {
jQuery('.switcher .option')
.stop(true, true)
.delay(50)
.slideDown(800);
}
});
jQuery('body').not('.switcher .selected').mousedown(function() {
if (jQuery('.switcher .option').is(':visible')) {
jQuery('.switcher .option')
.stop(true, true)
.delay(300)
.slideUp(800);
}
});
</script>
<div id="google_translate_element2"></div>
<script type="text/javascript">
function googleTranslateElementInit2() {
new google.translate.TranslateElement({
pageLanguage: 'fi',
autoDisplay: false
}, 'google_translate_element2');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit2"></script>
<script type="text/javascript">
/* <![CDATA[ */
function GTranslateFireEvent(element, event) {
try {
if (document.createEventObject) {
var evt = document.createEventObject();
element.fireEvent('on' + event, evt)
} else {
var evt = document.createEvent('HTMLEvents');
evt.initEvent(event, true, true);
element.dispatchEvent(evt)
}
} catch (e) {}
}
function doGTranslate(lang_pair) {
if (lang_pair.value) lang_pair = lang_pair.value;
if (lang_pair == '') return;
var lang = lang_pair.split('|')[1];
var teCombo;
var sel = document.getElementsByTagName('select');
for (var i = 0; i < sel.length; i++)
if (sel[i].className == 'goog-te-combo') teCombo = sel[i];
if (document.getElementById('google_translate_element2') == null ||
document.getElementById('google_translate_element2').innerHTML.length == 0 ||
teCombo.length == 0 ||
teCombo.innerHTML.length == 0) {
setTimeout(function() {
doGTranslate(lang_pair)
}, 500)
} else {
teCombo.value = lang;
GTranslateFireEvent(teCombo, 'change');
GTranslateFireEvent(teCombo, 'change')
}
}
function GTranslateGetCurrentLang() {
var keyValue = document.cookie.match('(^|;) ?googtrans=([^;]*)(;|$)');
return keyValue ? keyValue[2].split('/')[2] : null;
}
if (GTranslateGetCurrentLang() != null) jQuery(document).ready(function() {
jQuery('div.switcher div.selected a').html(jQuery('div.switcher div.option').find('span.gflag img[alt="' + GTranslateGetCurrentLang() + '"]').parent().parent().html());
});
/* ]]> */
</script>
Upvotes: 1
Views: 3007
Reputation: 21
Worked for me!!
add_action('wp_footer', 'my_enqueue_front_scripts',0);
function my_enqueue_front_scripts(){
echo '<script type="text/javascript">
doGTranslate("en|iw");return false;
</script>';
}
Upvotes: 1
Reputation: 2398
Instead of calling doGTranslate()
on page load .. code the script to click <a>
tag on oage load..
<a href="#" id="load_clicked" onclick="doGTranslate('fi|fi');jQuery(this).parent().parent().find('div.selected a').html(jQuery(this).html());return false;" title="Finnish" class="nturl selected"><span class="gflag" style="background-position:-100px -100px;"><img src="/gtranslate/blank.png" height="16" width="16" alt="fi" /></span>Finnish</a>
<script type="text/javascript">
$( window ).load(function() {
$('#load_clicked').click();
});
</script>
Upvotes: 0
Reputation: 858
use this script:
<script>
$(document).ready(function(){
doGTranslate('fi|fi');jQuery(this).parent().parent().find('div.selected a').html(jQuery(this).html());return false;
}
</script>
jusy add it to your header (I think it works if you type it after all scripts in header.)
Upvotes: 0
Reputation: 24915
Per my understanding, following code is translating page to Finnish
:
doGTranslate('fi|fi');
jQuery(this)
.parent()
.parent()
.find('div.selected a')
.html(jQuery(this).html());
return false;
You just have to call this code on $(document).ready(function(){...})
Upvotes: 0