Translate

Translate

The return functions of inlineTranslate and inlinePlural are parsed and replaced with translated texts in chunks sent to the browser at compile time

inlineTranslate

inlineTranslate returns a functions to get the translation using key-value pairs:

const t = inlineTranslate();

t('title@@Qwik Speak')

Value after @@ is the optional default value:

`Qwik Speak`

Params interpolation

t function accept params as well:

t('greeting@@Hi! I am {{name}}', { name: 'Qwik Speak' })

name param is replaced at runtime or during the inlining:

Hi! I am Qwik Speak

Array of keys

t function accepts array of keys:

and returns an array of translated values:

Arrays and objects as values

t function can get arrays and objects directly from files:

just pass to the function the type parameter:

You can also access by array position:

Finally, it is possible to set arrays and objects passing a valid stringified default value:

Html in translations

You can have Html in translations, like:

but you have to use dangerouslySetInnerHTML:

On the client the text is inlined during build, so there are no XSS risks

inlinePlural

inlinePlural returns a functions that uses Intl.PluralRules API:

When you run the extraction tool, it creates the Intl API plural rules for each language:

It is possible to set the default value passing a valid stringified json, keeping in mind that the counter is optionally interpolated with the value parameter:

Will result in:

It is rendered as:

Runtime translation

When you use a translation like this:

you are using a dynamic translation. It means that it is not possible to evaluate the translation at compile time but only at runtime based on the value that the key takes on.

To instruct Qwik Speak to use dynamic translations, create a file with the values that these translations can take:

i18n/[lang]/runtime.json

and add the runtime file to runtimeAssets in configuration or useSpeak provider.

QRL functions and lifecycle hooks

QRL functions $ and lifecycle hooks like useTask$ create js chunks that will be lazy loaded. So you need to re-invoke inlineTranslate inside them:

Server translation

inlineTranslate and inlinePlural work in component$, Inline components, QRL and functions if called by the components, but they might not work in functions invoked on the server, such as routeLoader$ and endpoints.

Functions like routeLoader$ live on the server, which knows nothing about the context of the app, and depending on the case they can be invoked before the app runs. To translate on the server you need:

  • make sure translations are available

  • let the server know the current language of the user

server$ function can satisfy both conditions, since the function is executed only when invoked, and accepts parameters:

You can also extract the language directly into the function, through the request (cookies, params), instead of passing it as a parameter.

Automatic key generation

If you don't want to handle the keys inside the translation functions, but only the default values, you can enable automatic key generation:

  • Extraction tool: add --autoKeys=true to the script

  • Inline Vite plugin: add autoKeys: true to the options

Note. You can enable this option, even if you use the syntax key@@[default value].

If you enable this option, you can pass only the default values to the translation functions:

If you run the extractor, you will get json files like this:

Then the Inline plugin will manage the self-assigned keys.

Localize

useFormatDate

useFormatDate returns a functions that uses Intl.DateTimeFormat API to format dates:

The second param in the signature is an Intl DateTimeFormatOptions object, which allows you to customize the format:

Optionally it uses the time zone set in timeZone property of the SpeakLocale.

useRelativeTime

useRelativeTime returns a functions that uses Intl.RelativeTimeFormat API to format relative times:

The second param in the signature is an Intl RelativeTimeFormatUnit string:

useFormatNumber

useFormatNumber returns a functions that uses Intl.NumberFormat API to format numbers:

Currency

To format as currency, you have to set the style property of the second param, an Intl NumberFormatOptions object:

It uses the currency code set in currency property of the SpeakLocale.

Unit

To format as unit, you have to set the style and unit properties of the second param:

It uses the unit set in optional units property of the SpeakLocale:

useDisplayName

useDisplayName returns a functions that uses Intl.DisplayNames API to translate language, region, script or currency display names:

The locale used by useFormatDate, useRelativeTime, useFormatNumber and useDisplayName is primarily the extension property of the SpeakLocale if provided, otherwise the lang property. extension is the language with Intl extensions, in the format language[-script][-region][-extensions] like en-US-u-ca-gregory-nu-latn

Multilingual

Each of the translation and localization functions accepts a different language other than the current one as its last argument:

For the translation to occur in the language passed as an argument, you need to set the additional language to useQwikSpeak or useSpeak providers:

Last updated