2025-12-02 03:16:34 -09:00
|
|
|
= cockpit.js: Localization
|
2025-12-02 03:08:41 -09:00
|
|
|
|
2025-12-02 03:16:34 -09:00
|
|
|
Cockpit provides a https://www.gnu.org/software/gettext/[`+gettext()+`]
|
2025-12-02 03:08:41 -09:00
|
|
|
like API for easy translation of strings.
|
|
|
|
|
|
|
|
|
|
[[cockpit-locale-language]]
|
|
|
|
|
== cockpit.language
|
|
|
|
|
|
|
|
|
|
The current locale language code. This is set based on the
|
2025-12-02 03:16:34 -09:00
|
|
|
link:#cockpit-locale-locale[`+cockpit.locale()+`] data loaded.
|
2025-12-02 03:08:41 -09:00
|
|
|
|
|
|
|
|
[[cockpit-locale-locale]]
|
|
|
|
|
== cockpit.locale()
|
|
|
|
|
|
|
|
|
|
....
|
|
|
|
|
cockpit.locale(po)
|
|
|
|
|
....
|
|
|
|
|
|
2025-12-02 03:16:34 -09:00
|
|
|
Load locale information for a given `+po+` data. The data should be JSON
|
2025-12-02 03:08:41 -09:00
|
|
|
data in the https://www.npmjs.org/package/po2json[po2json] format. The
|
2025-12-02 03:16:34 -09:00
|
|
|
data will be loaded globally. If `+po+` data has already been loaded,
|
|
|
|
|
then this will extend that loaded data with additional strings. Any
|
|
|
|
|
identical translations strings will be replaced with the new strings. A
|
|
|
|
|
`+null+` argument clears all the locale information previously loaded.
|
2025-12-02 03:08:41 -09:00
|
|
|
|
|
|
|
|
Various methods such as
|
2025-12-02 03:16:34 -09:00
|
|
|
link:#cockpit-locale-gettext[`+cockpit.gettext()+`] make use of the
|
|
|
|
|
loaded data.
|
2025-12-02 03:08:41 -09:00
|
|
|
|
|
|
|
|
[[cockpit-locale-gettext]]
|
|
|
|
|
== cockpit.gettext()
|
|
|
|
|
|
|
|
|
|
....
|
|
|
|
|
translated = cockpit.gettext([context], string)
|
|
|
|
|
var _ = cockpit.gettext
|
|
|
|
|
var C_ = cockpit.gettext
|
|
|
|
|
translated = _("string")
|
|
|
|
|
translated = C_("context", "string")
|
|
|
|
|
....
|
|
|
|
|
|
2025-12-02 03:16:34 -09:00
|
|
|
Lookup `+string+` for translation in the loaded locale data. The
|
|
|
|
|
translated string will be returned, or `+string+` will be returned if no
|
|
|
|
|
such translated string is present. The `+context+` argument is an
|
|
|
|
|
optional string used to qualify the string.
|
2025-12-02 03:08:41 -09:00
|
|
|
|
2025-12-02 03:16:34 -09:00
|
|
|
This function can be assigned to a variable called `+_+` (underscore)
|
|
|
|
|
which will make your code work with the typical `+_("string")+` syntax.
|
2025-12-02 03:08:41 -09:00
|
|
|
|
|
|
|
|
[[cockpit-locale-noop]]
|
|
|
|
|
== cockpit.noop()
|
|
|
|
|
|
|
|
|
|
....
|
|
|
|
|
var N_ = cockpit.noop
|
|
|
|
|
var NC_ = cockpit.noop
|
|
|
|
|
....
|
|
|
|
|
|
2025-12-02 03:16:34 -09:00
|
|
|
A noop function suitable for assigning to `+N_+` or `+NC_+` so that
|
2025-12-02 03:08:41 -09:00
|
|
|
gettext scanners will be able to find translatable strings. More
|
|
|
|
|
specifically this function returns its last argument.
|
|
|
|
|
|
|
|
|
|
[[cockpit-locale-ngettext]]
|
|
|
|
|
== cockpit.ngettext()
|
|
|
|
|
|
|
|
|
|
....
|
|
|
|
|
translated = cockpit.ngettext([context], string1, stringN, number)
|
|
|
|
|
....
|
|
|
|
|
|
2025-12-02 03:16:34 -09:00
|
|
|
Lookup a string appropriate for a pluralization form of the `+number+`.
|
2025-12-02 03:08:41 -09:00
|
|
|
Various languages have complex pluralization forms that go far between
|
|
|
|
|
the singular and plural forms speakers of English are familiar with. If
|
2025-12-02 03:16:34 -09:00
|
|
|
no such translated string is found then either one of `+string1+` or
|
|
|
|
|
`+stringN+` is returned according to simple pluralization rules.
|
2025-12-02 03:08:41 -09:00
|
|
|
|
2025-12-02 03:16:34 -09:00
|
|
|
The `+context+` argument is an optional string used to qualify the
|
|
|
|
|
string.
|
2025-12-02 03:08:41 -09:00
|
|
|
|
|
|
|
|
[[cockpit-locale-translate]]
|
|
|
|
|
== cockpit.translate()
|
|
|
|
|
|
|
|
|
|
....
|
|
|
|
|
cockpit.translate()
|
|
|
|
|
cockpit.translate(element, ...)
|
|
|
|
|
cockpit.translate(selection)
|
|
|
|
|
....
|
|
|
|
|
|
2025-12-02 03:16:34 -09:00
|
|
|
The document will be scanned for `+translate+` tags and they will be
|
2025-12-02 03:08:41 -09:00
|
|
|
translated according to the strings in loaded locale data. One or more
|
2025-12-02 03:16:34 -09:00
|
|
|
`+element+` arguments may be specified. These are DOM elements for
|
|
|
|
|
specific parts of the document to be translated. If no `+element+` is
|
2025-12-02 03:08:41 -09:00
|
|
|
specified then the entire document is translated.
|
|
|
|
|
|
2025-12-02 03:16:34 -09:00
|
|
|
If an array or array-like object is passed as a `+selection+` then all
|
|
|
|
|
DOM elements in the array will be treated as parts of the document to be
|
2025-12-02 03:08:41 -09:00
|
|
|
translated.
|