I am using the google translator api code to translate the page. It's working fine. But I need to set the default language as per client location, How it can be done? I have used the setCookie() javascript method to setting default language, but it's not working for me. Any suggestion, please share.
Asked
Active
Viewed 4,133 times
1
-
setcookie('googtrans', '/en/en'); or Adding #googtrans(en) to the end of the query string will also automatically translate the page – krutssss Apr 18 '16 at 11:47
-
I have used this but nothing happens when I open the page... Page translated only, with the previous store language cookie name – sourabh Apr 18 '16 at 11:49
-
debug it and check when it took value from cookie, and apply cookie change before that invocation -- hope helps – hemanjosko Apr 18 '16 at 12:50
-
See http://stackoverflow.com/a/42667484/5466401 – Sibin John Mattappallil Mar 08 '17 at 09:30
1 Answers
0
Please refer to the following testing php page code:
Step 1. Use setcookie('googtrans', '/en/fr') to set the page language to be translated before page loads. (Here, the target language is fr.)
Step 2. Put the javascript part to call the google translate script in an html div and
In CSS, make this html div display:none.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style type="text/css">
.yiping-language-switcher/*, .goog-te-banner-frame*/ {
display: none;
}
</style>
<div id="google_translate_element" class="yiping-language-switcher">
<?php
setcookie('googtrans', '/en/ko');
?>
<script type="text/javascript">
function initializeGoogleTranslateElement() {
new google.translate.TranslateElement({
pageLanguage: "en"
}, "google_translate_element");
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=initializeGoogleTranslateElement"></script>
</div>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Please refer to this post: Google Translate set default language - Please refer to the last answer for the setcookie() part

Epping_
- 29
- 2
- 6