Feb 5
Javascript Localization Reference
Three good ways to localize Javascript:
1. Creating message-only Javacript files
http://www.clearwired.com/loop/archives/29-Widget.html
function getLocalizedString(key) {
try {
var ret = LocalizedStrings[key];
if (ret === undefined) ret = key;
return ret;
} catch (ex) {}
return key;
}
<div id="status"></div>
document.getElementById('status').innerHTML = getLocalizedString('Status: Ready');
fr_FR/Localized.js
var LocalizedStrings = new Object;
LocalizedStrings[’Status: Ready‘] = ‘French String Here’;
* Using English keys can sometimes cause multiple translation issues. Using unique string id can minimize this issue, although it is cumbersome for developers to come up with string ids. For example, LocalizaedString[’account’] can be translated as an email account or a bank account.
2. Good tips for Java:
http://www.jspwiki.org/wiki/JavascriptLocalization
Just realized that we need to also localize the Javascript. What would be a good strategy for that? I have four ideas:
- Make Javascript files regular JSP files, so we can use normal Java i18n techniques.
- Problem: Needs careful consideration of caching; otherwise page loads may slow down considerably
- Use an AJAX technique to load i18n’d strings into the Javascript
- Problem: may be too complicated to implement, and also slow
- Develop localized versions of each JS file
- Problem: difficult to keep updated; can’t use the Java JAR’s to deploy new versions
- Remove any user-visible strings from the Javascript
- Problem: perhaps impossible to do without sacrificing big pieces?
I would like to keep this such that it’s possible to deploy a new localization by simply dropping a JAR file in the correct directory. That would mean option #1…
String.prototype.localize = function(){
var s = LocalizedStrings["javascript.”+this];
if( !s ) return( “§§§” + this + “§§§” );
for (var i = 0; i < arguments.length; i++)
{
s = s.replace(”{” + i + “}”, arguments[i]);
}
return s;
}
});
…
s += ”
“+ “SearchBox.clearrecent”.localize(1) + “
";
...
javascript.SearchBox.clearrecent=Clear {0} Recent Searches
3. Develop localized versions of each JS file with build tools
1. Create English XML & XSL & Javascript -> HTML + Javascript
2. Localize strings in XML files
3. Build tools create localized XML & XSL & Javascript
4. Localized: XML & XSL & Javascript -> localized HTML + Javascript
No Comments
Leave a comment