Search code examples
javascriptpropertiesinternationalization

Can not read property of undefined i18n


When I try to pass language dynamically from select dropdown list, It shows following error:

Uncaught TypeError: Cannot read property 'properties' of undefined at changeLanguage

where changeLanguage is my function as follows:

function changeLanguage(lang) 
{ 
 lang = lang || "en_EN"; // if no language is set, use the browser's default 
 jQuery.i18n.properties({ 
 path : 'language', 
 mode : 'both', language: lang,
 name: 'Messages', 
 callback: refresh_i18n }); 
}

The following are the pictures of it:

Dropdown image

Console error

What is wrong with the code? Please do suggest! Thanks

UPDATE: FOR MORE EXPLANATION: This is my index file and initially I call it with onload it works fine.

<script> 
function refresh_i18n() { 
  console.log('Some code...') 
} 
function changeLanguage(lang) { 
     lang = lang || "en_EN"; 
     jQuery.i18n.properties({ 
     path : 'js/libs/language', 
     mode : 'both', 
     language: lang, 
     callback: refresh_i18n 
   });
} 
changeLanguage("en_US"); 
</script>

but when i call by changing dropdown like:

$('#selectLanguage').change(function(){ 
    changeLanguage(this.value); 
});

It gives above error


Solution

  • Probrably the i18n library is loading before the jquery (problems haha).

    So, put the jQuery loading in the head of the html and the plugin i18n (and others, if you are using) in the body (I suggest in the end of body) of the html.

    That must solve the problem.