0

I am creating a phone application for my website using cordova. I want to translate my website between Arabic and English.I implement it using google translator API in a java script.This is the code that I used

<script type="text/javascript">
    function googleTranslateElementInit() {
      new google.translate.TranslateElement({pageLanguage: '', includedLanguages: 'ar,en'}, 'google_translate_element');
    }
</script>

<script type="text/javascript" src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

This code works well for my index.html page(home page).But the problem is translation only take place on my index.html page.When I redirect to any other pages from home page,I still remains in English.How can I translate entire pages in my application

payam_sbr
  • 1,428
  • 1
  • 15
  • 24
user7160306
  • 49
  • 11
  • Are you sure that you are calling this script in every pages ? Try adding alert into the function and check when you change the page alert is display or not. – Kirankumar Dafda Dec 14 '16 at 06:35
  • I added alert to all pages where this script is used.The alert boxes are also shown in all pages.But language remains english. I need to select language separate for each page to change language. – user7160306 Dec 14 '16 at 06:47
  • You need to set your last selected lang into localStorage and call it on page change or tell the script that which language was selected last. Otherwise it will consider new page to set default language on first load. – Kirankumar Dafda Dec 14 '16 at 06:58
  • How can I find last selected language from script – user7160306 Dec 14 '16 at 07:14
  • This is the div to display dropdown box and the java script is given in my question – user7160306 Dec 14 '16 at 07:17
  • Check [this](http://stackoverflow.com/a/27488298/3840093) answer of stackoverflow you will get the language name which you have selected first time it will display `undefined` – Kirankumar Dafda Dec 14 '16 at 07:28
  • Now I get the selected language identifier (eg:ar for arabic) in all pages.Now I need to convert page to that selected language.How can I convert it using language identifier – user7160306 Dec 14 '16 at 09:01

1 Answers1

0

Set any default language first time for your pageLanguage for e.g., pageLanguage: 'en'

This function will return you selected language which you can set into localStorage and get it in every page you required

function readCookie(name) {
    var c = document.cookie.split('; '),
    cookies = {}, i, C;

    for (i = c.length - 1; i >= 0; i--) {
        C = c[i].split('=');
        cookies[C[0]] = C[1];
     }

     return cookies[name];
     localStorage.setItem("selLanguage",cookies[name])
}
console.log(readCookie('googtrans'));

Which you can set with the page load script like

function googleTranslateElementInit() {
    var selLang = localStorage.getItem("selLanguage");
    if(selLang !== null && selLang == "ar")
    {
        new google.translate.TranslateElement({pageLanguage: 'ar', includedLanguages: 'ar,en'}, 'google_translate_element');
    }
    else
    {
        new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'ar,en'}, 'google_translate_element');
    }
}
Kirankumar Dafda
  • 2,354
  • 5
  • 29
  • 56