l10n.js is a JavaScript library that enables passive localization through native JavaScript methods, gracefully degrading if the library is not present. You can make Ajax applications, JavaScript libraries, etc. that can be localized but not require l10n.
npm install l10n.jsl10n.js
=======
l10n.js is a JavaScript library that enables passive localization through native
JavaScript methods, gracefully degrading if the library is not present. You can make
Ajax applications, JavaScript libraries, etc. that can be localized but not require
l10n.js to function. There is already a placeholder method for all API calls
as specified in the ECMAScript specification and is present in all JavaScript
engines, so when l10n.js isn't present, your application works fine.
Demo
----
You can try out the [online demo][1] to see l10n.js in action.
Currently the demo supports the following locales.
* English
* American English
* British English
* Canadian English
* Australian English
* Portuguese
* Spanish
* French
* Dutch
* German
* Finnish
* Vietnamese
* Italian
* Norwegian
* Russian
* Lojban
* Danish
* Hebrew
* Bulgarian
* Simplified Chinese
* Swedish
* Turkish
* Hungarian
* Czech
* Catalan
* Japanese
If you know a language that isn't currently supported in the demo, I encourage you to
contribute a localization by sending me your own localizations, either [through GitHub][2]
or [directly][3]. The following strings would need to be localized:
* %title to {Locale} - l10n.js demo in the locale.
* %info to You are viewing a {locale} localization
in the locale.
of this page.
* Optionally, %locale.dir to rtl if the locale uses right-to-left directionality.
Supported Browsers
------------------
* Internet Explorer 5+
* Firefox 2+
* Opera 9+
* Doesn't support region-specific locales. Only gives "en" in the case of "en-US".
* Google Chrome 1+
* Safari 4+
Getting Started
---------------
1. [Download l10n.js][4].
2. Localize strings used in your JavaScript application. See the [demo localizations
file][5] for an example localizations file. You can also specify external
localizations in your main localizations file by assigning a URL string to a language
code, such as "en-us": "localizations/en-us.json".
3. Include the appropriate link elements, as described in the usage section, anywhere in
your document. I recommend putting it in the document's .
4. Place
anywhere after the tag.
5. Call toLocaleString() on any strings you wish to localize.
Usage
-----
toLocaleString() on every localizable string can create a lot of extra typing
l("Your localizable string") instead
"Your localizable string".toLocaleString(). I chose l instead of _ (an
document.title = l("%title.search")
"Seach - Acme, Inc."
confirm(l("%confirm.deleteAccount"))
"Are you sure you want to delete your account?"
link.href = "http://www.google." + l("%locale.tld")
"http://www.google.co.uk"
sprintf(). A nice
sprintf()][6].
),
), then the file
.
String.toLocaleString() witht the JSON
hreflang link
localization instead of localizations with an
String.toLocaleString([localizations])localizations is an object, it is added to the
localizations is a string, it is requested as JSON and
localizations is false, then all
localizations is an object, and a locale is
false, then all localizations for that locale are reset.
The string representation of the String contructor is returned, to
maintain backwards compatibility with any code using this method to actually get it.
String.toLocaleString("path/to/localizations.json")
The nearest locale to the user's locale that has the string being localized is
used in localization.
String.toLocaleString({
"es": { // Spanish
"Hello, world!": "¡Hola, mundo!"
// more localizations...
},
"en-US": { // American English
"Hello, world!": "Hello, America!" // Locale-specific message
// more localizations...
},
"en-GB": false, // resetting British English localizations
// Specifying external localization JSON for Japanese:
// The URL isn't requested unless the user's locale is Japanese
"ja": "localizations/ja.json"
})
String.toLocaleString(false)
aString.toLocaleString()aString in the user's locale,
String.localeString.defaultLocale