Main Page

String considerations

This example places the message string into a variable called
. All other inter-
nationalized strings should be stored alongside this variable so you can change any and all values in
only one place.
The best way to handle internationalized strings is to separate all strings into a separate JavaScript file
(similar to the way JSP applications use properties files). Each language you support should have its own
JavaScript file. For example, suppose you have three languages to support: English (language code en),
German (de), and French (fr). Each language should have its own JavaScript file containing any strings
necessary for the Web site or Web application. The easiest way to do this is to give each file a filename that
differs only in the language code. For example, these filenames make selecting the correct file easy:
Then, using a little server-side logic, you can ensure that the correct one is included. In PHP, you could
do this:
$supported = array(“en”,”de”,”fr”);
if (in_array($lang, $supported)) {
$filename = “Strings_$lang.js”;
} else {
$filename = “Strings_en.js”;
<script type=”text/javascript”
src=”scripts/<?php echo $filename ?>”></script>
This example assumes a variable named
contains the language to use and then matches it up
against an array of supported languages (
). If the language is supported, the JavaScript file
for that language is loaded; otherwise, the default (English) language script is loaded. This ensures that
the correct JavaScript string values are used for the given language and that there is a default language
to fall back on if an unsupported language is encountered.
String considerations
The first edition of ECMAScript introduced support for Unicode characters (which number upwards of
65,000 as compared to 128 ASCII characters), effectively assuring that ECMAScript can handle strings of
any kind, including typically problematic double-byte characters.
What exactly is Unicode?
According to the official Unicode home page, “Unicode provides a unique number for every character,
no matter what the platform, no matter what the program, no matter what the language.”
Unicode was developed to provide a common encoding to handle all the characters that exist in the world.
Prior to Unicode, each language had its own encoding, meaning that characters in different languages
Chapter 19
22_579088 ch19.qxd 3/28/05 11:43 AM Page 570

JavaScript EditorFree JavaScript Editor     Ajax Editor