//file EN.php
$locale['welcome'] = 'Welcome to our site';
$locale['alert'] = 'Why you do this?';
//file ES.php
$locale['welcome'] = 'Bienvenido al Sitio';
$locale['alert'] = '¿Porque Haces esto?';
In order to use these variables, we can load the right array depending on the user locale, convert it to JSON (json_encode()) and send it to the client. The result will be:
{welcome : 'Welcome to our site', alert: 'Why you do this?'}
It is important to load the locals before any of your javascript is loaded, otherwise your app will break. We can do this by an AJAX call or printing that string inside a <script> tag.
var lang = {
welcome : 'Welcome to our site',
alert : 'Why you do this?'
}
alert(lang.welcome);
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented: