Members
# constant lookForMissingKeys
Searches for missing translations. Prints an warning if there is a translation for a language missing.
- Deprecated:
- Naming changed to reportMissing.
# constant reportMissing
Searches for missing translations. Prints an warning if there is a translation for a language missing.
# constant tryParseLocale
Tries parsing locale from a string. (eg. de-DE or de => de_DE)
Methods
# LitteraProvider(initialLocale, preset, setLocale, pattern)
Context Provider for Littera
Parameters:
Name | Type | Description |
---|---|---|
initialLocale |
Initial active locale. | |
preset |
Set of predefined translations. | |
setLocale |
Callback called when the locale changes. | |
pattern |
Locale pattern. |
Example
// Setting up Littera provider.
const App = () => {
return <LitteraProvider locales={["en_US", "de_DE"]}>
...
</LitteraProvider>
}
# translate(translations, locale) → {ITranslated}
Returns object with translated values based on locale.
Parameters:
Name | Type | Description |
---|---|---|
translations |
ITranslations | |
locale |
string |
Example
// Example of using translations.
const translations = {
example: {
en_US: "Example",
de_DE: "Beispiel"
},
hello: (name: string) => ({
en_US: `Hello ${name}`,
de_DE: `Hallo ${name}`
})
}
const translated = translate(translations, "de_DE");
translated.example // => "Beispiel"
translated.hello("Mike") // => "Hallo Mike"
# translateSingle(translation, locale) → {ISingleTranslated}
Returns translated string based on locale.
Parameters:
Name | Type | Description |
---|---|---|
translation |
ITranslation | |
locale |
string |
ISingleTranslated
Example
// Example of utilizing a single translation.
const translations = {
example: {
en_US: "Example",
de_DE: "Beispiel"
},
hello: (name: string) => ({
en_US: `Hello ${name}`,
de_DE: `Hallo ${name}`
})
}
const translatedExample = translateSingle(translations.example, "de_DE");
const translatedExample = translateSingle(translations.hello("Mike"), "de_DE");
translatedExample // => "Beispiel"
translatedHello("Mike") // => "Hallo Mike"
# useLittera(translations, locale) → {ITranslated}
Hook returns translations for the active locale.
Parameters:
Name | Type | Description |
---|---|---|
translations |
ITranslations | Translations |
locale |
string | Locale in case you need translations for a not active locale. |
Example
// Example of using translations in a function component.
const translations = {
example: {
en_US: "Example",
de_DE: "Beispiel"
},
hello: (name: string) => ({
en_US: `Hello ${name}`,
de_DE: `Hallo ${name}`
})
}
const YourComponent = () => {
const translated = useLittera(translations);
return <h2>{translated.example} - {translated.hello("Mike")}</h2>
}
# useLitteraMethods() → {Object|string|Array.<string>|function|function|ITranslations|function|function}
Hook exposes an object with global translation methods and variables.
methods
Object
methods.locale - active locale.
string
methods.locales - all locales.
Array.<string>
methods.setLocale - changes the active locale.
function
methods.validateLocale - method validates the locale format using a pattern.
function
methods.preset - global preset.
methods.translate - the core translations method.
function
methods.translateSingle - the core single translation method.
function
Example
// Example of accessing littera methods and variables in a function component.
const YourComponent = () => {
const { setLocale, locale } = useLittera(translations);
const handleClick = () => {
setLocale("de_DE");
}
return <h2 onClick={handleClick}>Current language: {locale}</h2>
}
# withLittera(translations) → {FunctionComponent}
HOC for managing translations.
Parameters:
Name | Type | Description |
---|---|---|
translations |
ITranslations | ITranslationsFunction |
- Deprecated:
- HOCs are out of support. Please consider switching to React hooks.
FunctionComponent
Type Definitions
Object
# ITranslated
Object containing already translated text.
Is being flatened from ITranslations.
Example
{
"example": "Beispiel"
}
Object
# ITranslations
Object containing all available translations.
Example
{
"example": {
"en_US": "Example",
"de_DE": "Beispiel"
}
}
Object
# ITranslationsFunction
Function returning all available translations. Preset is being passed as argument.
Example
const translations = (preset) => ({
"example": {
"en_US": "Example",
"de_DE": "Beispiel"
}
})