This is an extension library that only has a peer dependency to piral-core
. What piral-translate
brings to the table is a set of API extensions that is used by piral
. The set features a simple yet sufficient translation system.
Documentation
The following functions are brought to the Pilet API.
(tbd)
Setup and Bootstrapping
The provided library only brings API extensions for pilets to a Piral instance.
For the setup of the library itself you'll need to import createLocaleApi
from the piral-translate
package.
import { createLocaleApi } from 'piral-translate';
The integration looks like:
const instance = createInstance({
extendApi: [createLocaleApi()],
});
Via the options the available languages, translations, as well as the currently selected language can be chosen.
For example:
const localizer = setupLocalizer({
language: 'en',
messages: {
en: {
'greeting': 'Hello',
},
de: {
'greeting': 'Hallo',
},
},
});
const instance = createInstance({
extendApi: [createLocaleApi(localizer)],
});
Alternatively, the current language can also be inferred via a function.
const localizer = setupLocalizer({
language: getUserLocale,
messages: {
en: {
'greeting': 'Hello',
},
de: {
'greeting': 'Hallo',
},
},
});
The function getUserLocale
retrieves either the cookie with name _culture
or local storage value for key locale
. In any case either the first found language or ultimately en
is used as a fallback.
License
Piral is released using the MIT license. For more information see the license file.