| 1 |
= cockpit.js: Localization |
| 2 |
|
| 3 |
Cockpit provides a https://www.gnu.org/software/gettext/[`+gettext()+`] |
| 4 |
like API for easy translation of strings. |
| 5 |
|
| 6 |
[[cockpit-locale-language]] |
| 7 |
== cockpit.language |
| 8 |
|
| 9 |
The current locale language code. This is set based on the |
| 10 |
link:#cockpit-locale-locale[`+cockpit.locale()+`] data loaded. |
| 11 |
|
| 12 |
[[cockpit-locale-locale]] |
| 13 |
== cockpit.locale() |
| 14 |
|
| 15 |
.... |
| 16 |
cockpit.locale(po) |
| 17 |
.... |
| 18 |
|
| 19 |
Load locale information for a given `+po+` data. The data should be JSON |
| 20 |
data in the https://www.npmjs.org/package/po2json[po2json] format. The |
| 21 |
data will be loaded globally. If `+po+` data has already been loaded, |
| 22 |
then this will extend that loaded data with additional strings. Any |
| 23 |
identical translations strings will be replaced with the new strings. A |
| 24 |
`+null+` argument clears all the locale information previously loaded. |
| 25 |
|
| 26 |
Various methods such as |
| 27 |
link:#cockpit-locale-gettext[`+cockpit.gettext()+`] make use of the |
| 28 |
loaded data. |
| 29 |
|
| 30 |
[[cockpit-locale-gettext]] |
| 31 |
== cockpit.gettext() |
| 32 |
|
| 33 |
.... |
| 34 |
translated = cockpit.gettext([context], string) |
| 35 |
var _ = cockpit.gettext |
| 36 |
var C_ = cockpit.gettext |
| 37 |
translated = _("string") |
| 38 |
translated = C_("context", "string") |
| 39 |
.... |
| 40 |
|
| 41 |
Lookup `+string+` for translation in the loaded locale data. The |
| 42 |
translated string will be returned, or `+string+` will be returned if no |
| 43 |
such translated string is present. The `+context+` argument is an |
| 44 |
optional string used to qualify the string. |
| 45 |
|
| 46 |
This function can be assigned to a variable called `+_+` (underscore) |
| 47 |
which will make your code work with the typical `+_("string")+` syntax. |
| 48 |
|
| 49 |
[[cockpit-locale-noop]] |
| 50 |
== cockpit.noop() |
| 51 |
|
| 52 |
.... |
| 53 |
var N_ = cockpit.noop |
| 54 |
var NC_ = cockpit.noop |
| 55 |
.... |
| 56 |
|
| 57 |
A noop function suitable for assigning to `+N_+` or `+NC_+` so that |
| 58 |
gettext scanners will be able to find translatable strings. More |
| 59 |
specifically this function returns its last argument. |
| 60 |
|
| 61 |
[[cockpit-locale-ngettext]] |
| 62 |
== cockpit.ngettext() |
| 63 |
|
| 64 |
.... |
| 65 |
translated = cockpit.ngettext([context], string1, stringN, number) |
| 66 |
.... |
| 67 |
|
| 68 |
Lookup a string appropriate for a pluralization form of the `+number+`. |
| 69 |
Various languages have complex pluralization forms that go far between |
| 70 |
the singular and plural forms speakers of English are familiar with. If |
| 71 |
no such translated string is found then either one of `+string1+` or |
| 72 |
`+stringN+` is returned according to simple pluralization rules. |
| 73 |
|
| 74 |
The `+context+` argument is an optional string used to qualify the |
| 75 |
string. |
| 76 |
|
| 77 |
[[cockpit-locale-translate]] |
| 78 |
== cockpit.translate() |
| 79 |
|
| 80 |
.... |
| 81 |
cockpit.translate() |
| 82 |
cockpit.translate(element, ...) |
| 83 |
cockpit.translate(selection) |
| 84 |
.... |
| 85 |
|
| 86 |
The document will be scanned for `+translate+` tags and they will be |
| 87 |
translated according to the strings in loaded locale data. One or more |
| 88 |
`+element+` arguments may be specified. These are DOM elements for |
| 89 |
specific parts of the document to be translated. If no `+element+` is |
| 90 |
specified then the entire document is translated. |
| 91 |
|
| 92 |
If an array or array-like object is passed as a `+selection+` then all |
| 93 |
DOM elements in the array will be treated as parts of the document to be |
| 94 |
translated. |