Skip to content

Latest commit

 

History

History
95 lines (61 loc) · 5.04 KB

README.md

File metadata and controls

95 lines (61 loc) · 5.04 KB

API Docs

Config
Properties and methods
Parsers

Config

translations?: Translations.T

This property defines translations, which should be in place before loaders will trigger. It's useful for synchronous translations (e.g. locally defined language names which are same for all language mutations).

You can use loaders to define your asyncronous translation load. All loaded data are stored so loader is triggered only once – in case there is no previous version of the translation. It can get refreshed according to config.cache.
Each loader can include:

locale: string – locale (e.g. en, de) which is this loader for.

key: string – represents the translation namespace. This key is used as a translation prefix so it should be module-unique. You can access your translation later using $t('key.yourTranslation'). It shouldn't include . (dot) character.

loader:() => Promise<Record<any, any>> – is a function returning a Promise with translation data. You can use it to load files locally, fetch it from your API etc...

routes?: Array<string | RegExp> – can define routes this loader should be triggered for. You can use Regular expressions too. For example [/\/.ome/] will be triggered for /home and /rome route as well (but still only once). Leave this undefined in case you want to load this module with any route (useful for common translations).

preprocess?: 'full' | 'preserveArrays' | 'none' | (input: Translations.Input) => any

Defines a preprocess strategy or a custom preprocess function. Preprocessor runs immediately after the translation data load. This is set to 'full' by default.

Examples for input:

{"a": {"b": [{"c": {"d": 1}}, {"c": {"d": 2}}]}}

'full' (default) setting will result in:

{"a.b.0.c.d": 1, "a.b.1.c.d": 2}

'preserveArrays' in:

{"a.b": [{"c.d": 1}, {"c.d": 2}]}

'none' (nothing's changed):

{"a": {"b": [{"c": {"d": 1}}, {"c": {"d": 2}}]}}

Custom preprocess function (input) => JSON.parse(JSON.stringify(input).replace('1', '"🦄"')) will output:

{"a": {"b": [{"c": {"d": "🦄"}}, {"c": {"d": 2}}]}}

initLocale?: string

If you set this property, translations will be initialized immediately using this locale.

fallbackLocale?: string

If you set this property, translations are automatically loaded not for current $locale only, but for this locale as well. In case there is no translation for current $locale, fallback locale translation is used instead of translation key placeholder. This is also used as a fallback when unknown locale is set.

Note that it's not recommended to use this property if you don't really need it. It may affect your data load.

fallbackValue?: any

By default, translation key is returned in case no translation is found for given translation key. For example, $t('unknown.key') will result in 'unknown.key' output. You can set this output value using this config prop.

cache?: number

When you are serving your app, translations are loaded only once on server. This property allows you to setup a refresh period in milliseconds when your translations are refetched on the server. The default value is 86400000 (24 hours).

Tip: You can set to Number.POSITIVE_INFINITY to disable server-side refreshing.

log.level?: 'error' | 'warn' | 'debug'

You can manage log level using this property (default: 'warn').

log.prefix?: string

You can prefix output logs using this property (default: '[i18n]: ').

log.logger?: Logger.T

You can setup your custom logger using this property (default: console).

parserOptions?: Parser.Options

This property includes configuration related to @sveltekit-i18n/parser-default.

Read more about parserOptions here.

Instance properties and methods

Each sveltekit-i18n instance includes all @sveltekit-i18n/base properties and methods described here.

Parsers

sveltekit-i18n library uses @sveltekit-i18n/parser-default by default. You can find more here, including the message format syntax.

In case @sveltekit-i18n/parser-default syntax does not fit your needs, feel free to use standalone @sveltekit-i18n/base together with a parser of your choice (or create your own!).

See parsers in Examples for more information.