My page is available in 3 languages and I'm including in the header of each file (index, contact, etc.) the selected language file (en, ru, pl).

<?php
session_start();
include('language/'.$_SESSION['lang'].'.php');
?>

en.php

<?php
session_start();
$lang = array();

$lang['hello'] = 'Hello';
$lang['world'] = 'World !';
?>

ru.php

<?php
session_start();
$lang = array();

$lang['hello'] = 'привет';
$lang['world'] = 'мир';
?>

And this is how I'm echoing them:

<p><? echo $lang['hello'].' '.$lang['world'];?></p>

This is working actually fine, but I'm having problem with my .js files. If I want to alert or insert some text somewhere I can't.

For example in one of my .js file I have an alert:

if (something_obj.val() == ''){
   alert('Wrong username given !');
   return false;
}

How can I implement language files in my js files? Am I need to create a news .js language file?

share|improve this question
1  
You can use JSON. See the following answer: stackoverflow.com/a/5619038/290172 – GExGE 18 hours ago
The answer for JavaScript has already been given. However, for translations in PHP; Why re-invent the wheel? Most PHP installations have gettext installed. Gettext is a standardised way to translate strings in your application and uses separate 'translation' files (.po/.mo) that contain translations per language. You can translate a string by setting the current 'locale' and translating the text via _('text-to-translate') – thaJeztah 18 hours ago

2 Answers

up vote 2 down vote accepted

You could have one js file per language with translations looking like this:

// german.js
var translations = {
  'hello': 'hallo',
  'world': 'welt'
};

In other JS scripts, just do something like this:

alert(translations['hello'] + " " + translations['world'])

Of course, you might want to choose a shorter variable name than translations.

Then, in your php file, include the correct JS script based on the language:

<script src="languages/<?php echo $_SESSION['lang']; ?>.js"></script>
share|improve this answer
+1 For translations in JavaScript, it is common practice to use separate .js files per language for an example, have a look at the jquery-ui date picker; jquery-ui.datepicker-fr.js. – thaJeztah 18 hours ago

You can create new file - for example js.php which will load proper language file and print out all variables into JavaScript array:

js.php

<script type="text/javascript">
var lang = {
<?php
session_start();
include('language/'.$_SESSION['lang'].'.php');

$data = array();
foreach ( $lang as $key => $value ) {
  $data[] = '"' . $key . '" : "' . $value . '"';
}
echo implode(',', $data);
?>
};
</script>

Then include this file on the page which have to use JavaScript translation array and use it with:

alert(lang.key);

Also if there will be " sign in the translation string, you have to escape it with \", for example with:

$value = str_replace('"', '\"', $value);
share|improve this answer
Not a good idea unless you don't mind that caching won't work, I think. – thejh 18 hours ago
It's just an idea which can be extended with cache feature, etc. – hsz 18 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.