5

While developing AJAX program, I met the design decision to make string I18N in JavaScript.code. Some string is only used by JavaScript. For example.

$('#submit').click(function() {
    $(#target).html('Please wait while submitting...').load(someURI);
}

I'd like to I18N the string 'Please wait while submitting...'. I'm not sure what's the best way to do it. Currently, I just have the string I18N-ed in server and rendered into a javascript variable in page (I'm using PHP/wordpress).

<script>strSubmit = <?php  _e('Please wait while submitting...'); ?></script>

Then, in javascript, I just use the varialble

$('#submit').click(function() {
    $(#target).html(strSubmit).load(someURI);
}

This works, but it looks messy. Is there any better way to achieve this?

1

4 Answers 4

10

You should use JSON to convert the server side l10n strings to a JSON object :

// In the <head> tag :
<script type="text/javascript" src="locales.php"></script> 

and this in locales.php :

var l10n = <?php echo json_encode($l10n); ?>;

where $l10n is an array which contains all the locales, like this :

$l10n = array(
  'Please wait while submitting...' => 'Veuillez patienter durant le traitement...',
  'bah' => 'bih'
);

You can now use these strings like this in JS :

function $T(s){
  return l10n[s] || s;
}

alert($T('Please wait while submitting...'));
Sign up to request clarification or add additional context in comments.

6 Comments

Yep. This is very similar to how we do it too :)
In this approach, all I18N strings are downloaded even only few are used.
Yes, but the files is cached by the browser, so it is downloaded only once.
Fabien, thanks a lot for this simple yet powerful solution! :)
stereofrog's solution would avoid having both this and the i18n solution by wordpress. WP also has wp_localize_script() which essentially does the above though less readable without the full text string.
|
2

you can also automate this by using a php "preprocessor" for javascript

<script src="script.php?file=blah.js">

where script.php is something like

    function _repl($x) { return '"' . _e($x[1]) . '"'; }

    $js = file_get_contents($_GET['file']);
    $js = preg_replace_callback('~_e\("(.+?)"\)~', '_repl', $js);
    echo $js;

this will transparently replace _e(something) in javascript code with actual strings

1 Comment

Are you suggesting to generate a HTTP request for every string that requires localization?
0

You could generate the text into the original script itself.

$('#submit').click(function() {
    $(#target).html('<?php  _e('Please wait while submitting...'); ?>').load(someURI);
}

1 Comment

it looks more like generating the script itself in php then generating the language tag
0

you could create sort of REST application, where you would fill the elements of javascript strings on document load from a service:

$(function(){
   var handleResponse = function.....; // fill your string elements from response
   var lang = "fr"; // language of localized document

   $.ajax(
       type: "GET",
       url: "/i18n.php?lang=" + lang + "&names=someName+someName1+someName2",
       success: handleResponse
   ); 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.