Simple translation management
Provides 4 functions to translate the texts:
- txt
- txtTitleCase
- txtUpperCase
- txtLowerCase
Note:
- You can also merge data directly into the translated text using a "merge" object (see below).
- Any translation which is not found will fallback to the text passed to the function.
- Any unfound text will be automatically stored into the "missingTexts" variable to ease the debug phase
Members
# static texts
All the static texts used in the UI components
Methods
# static get() → {string}
Get the current language
- first check the url parameter "language" (for example: &language=fr)
- if no parameter, tries to get the browser language
- defaults to "en" (English)
The current language. Examples: "en", "fr", "de", "it"...
string
# static init()
-
- Set the language from the browser settings, or system locales, or localStorage
-
- Consolidate the library's texts and the specific application's texts into a single object to lookup
# static select()
Open a window to switch the language
# static set(newLanguage)
Switch to another language
Parameters:
Name | Type | Description |
---|---|---|
newLanguage |
*
|
# static setAvailable(languages)
Set the available languages
Parameters:
Name | Type | Description |
---|---|---|
languages |
Array.<object>
|
Passeed as an array of objects with a "code" and a "name" property |
this
Example
kiss.language.setAvailable([
{
code: "en",
name: "English"
},
{
code: "fr",
name: "Français"
},
{
code: "es",
name: "Español"
}
])
# static showMissingTexts() → {string}
Show all missing texts in the console
- All the missing texts
string
# static showTranslationWindow()
Show a window to translate texts without translation
# static translate(key, customSourceTextsopt, merge) → {string}
Return a localized text from a key, or a fallback text If a translation is missing, the text key is stored into the "kiss.language.missingTexts" array, in order to ease the debug phase. When a merge object is passed as a parameter, the values are merged into the final string.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string
|
The text key, which should be in lowercase by convention (txtTitleCase, txtUpperCase, and txtLowerCase handle the case) |
|
customSourceTexts |
object
|
<optional> |
Custom source texts. Must be provided in the same format as default source texts. See example. |
merge |
object
|
Contextual data that must be merged into the text |
- The localized text, or the key passed to the function if not found
string
Example
txt("hello") // bonjour
txtTitleCase("hello") // Bonjour
txt("hello %firstName %lastName", null, {firstName: "Bob", lastName: "Wilson"}) // bonjour Bob Wilson
txt("Devil number: %num%num%num", null, {num: 6}) // Devil number: 666
let customSource = {
"apple": {
fr: "pomme"
},
"banana": {
fr: "banana"
},
"fruits": {
en: "choose between those fruits...",
fr: "choisissez parmi ces fruits..."
}
}
kiss.language.set("en")
txtTitleCase("apple", customSource) // Apple
txtUpperCase("banana", customSource) // BANANA
txtTitleCase("fruits", customSource) // Choose between those fruits...
kiss.language.set("fr")
txtUpperCase("fruits", customSource) // CHOISISSEZ PARMI CES FRUITS...