Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default language in google translation script

I am using below code to translate my web page. I am trying to set a default language(AF) when page load first time but it always translate my page in English(default) language so how can i set a default language with below script?

Here is my code:

   function googleTranslateElementInit() {
    new google.translate.TranslateElement({pageLanguage: 'af', layout: 
    google.translate.TranslateElement.InlineLayout.SIMPLE}, 
    'google_translate_element');
   }
like image 750
Anuj Negi Avatar asked Oct 29 '22 00:10

Anuj Negi


1 Answers

Google Translate uses a cookie called "googtrans" to track which language is selected. You can set that cookie yourself before the page loads and Google Translate will use it.

In your case :

 function googleTranslateElementInit() {
    
     // here we setting cookie by default for language we want
     $.cookie('googtrans', '/af/af');
     // cookie selection END here
    
     new google.translate.TranslateElement({pageLanguage: 'af', layout: 
     google.translate.TranslateElement.InlineLayout.SIMPLE}, 
     'google_translate_element');
  }

Hopefully, this can solve your issue.

like image 104
Sammar Iqbal Avatar answered Nov 09 '22 11:11

Sammar Iqbal