Improved i18n JS docs

This commit is contained in:
Ingo Schommer 2014-01-24 11:12:23 +13:00
parent 53e790ff98
commit 48949ba89a
1 changed files with 13 additions and 16 deletions

View File

@ -323,19 +323,14 @@ in your `mysite/_config.php`:
## Javascript Usage
i18n in javascript works with mostly the same assumption as its PHP-equivalent.
The i18n system in JavaScript is similar to its PHP equivalent.
Languages are typically stored in `<my-module-dir>/javascript/lang`.
Unlike the PHP logic, these files aren't auto-discovered and have to be included manually.
### Requirements
Add the i18n library requirement to your code.
:::php
Requirements::javascript(FRAMEWORK_DIR . "/javascript/i18n.js");
Each language has its own language table in a separate file.
To save bandwidth, only three tables are actually loaded by
To save bandwidth, only two files are actually loaded by
the browser: The current locale, and the default locale as a fallback.
The `Requirements` class has a special method to determine these includes:
Just point it to a directory instead of a file, and the class will figure out the includes.
@ -346,30 +341,31 @@ Just point it to a directory instead of a file, and the class will figure out th
### Translation Tables in JavaScript
Translation tables are automatically included as required, depending on the configured locale in *i18n::get_locale()*.
As a fallback for partially translated tables we always include the master table (en_US.js) as well.
Translation tables are automatically included as required, depending on the configured locale in `i18n::get_locale()`.
As a fallback for partially translated tables we always include the master table (`en.js`) as well.
Master Table (mymodule/javascript/lang/en_US.js)
Master Table (`<my-module-dir>/javascript/lang/en.js`)
:::js
if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
console.error('Class ss.i18n not defined');
} else {
ss.i18n.addDictionary('en_US', {
ss.i18n.addDictionary('en', {
'MYMODULE.MYENTITY' : "Really delete these articles?"
});
}
Example Translation Table (mymodule/javascript/lang/de_DE.js)
Example Translation Table (`<my-module-dir>/javascript/lang/de.js`)
:::js
ss.i18n.addDictionary('de_DE', {
ss.i18n.addDictionary('de', {
'MYMODULE.MYENTITY' : "Artikel wirklich löschen?"
});
For most core modules, these files are generated by a
[build task](https://github.com/silverstripe/silverstripe-buildtools/blob/master/src/GenerateJavascriptI18nTask.php), with the actual source files in a JSON
[build task](https://github.com/silverstripe/silverstripe-buildtools/blob/master/src/GenerateJavascriptI18nTask.php),
with the actual source files in a JSON
format which can be processed more easily by external translation providers (see `javascript/lang/src`).
### Basic Usage
@ -386,6 +382,7 @@ format which can be processed more easily by external translation providers (see
ss.i18n._t('MYMODULE.MYENTITY'),
42,
'Douglas Adams'
));
// Displays: "Really delete 42 articles by Douglas Adams?"