Internationalization (i18n) library to translate texts, dates and numbers in Qwik apps
npm install qwik-speak --save-dev- Quick Start
- Tutorial: localized routing
- Translate
- Translation functions
- Qwik Speak and Adapters
- Testing
Live example on Cloudflare pages and playground on StackBlitz
import { $translate as t } from 'qwik-speak';
export default component$(() => {
return (
<>
<h1>{t('app.title@@Qwik Speak')}</h1> {/* Qwik Speak */}
<p>{t('home.greeting@@Hi! I am {{name}}', { name: 'Qwik Speak' })}</p> {/* Hi! I am Qwik Speak */}
</>
);
});import { formatDate as fd, relativeTime as rt, formatNumber as fn } from 'qwik-speak';
export default component$(() => {
return (
<>
<p>{fd(Date.now(), { dateStyle: 'full', timeStyle: 'short' })}</p> {/* Wednesday, July 20, 2022 at 7:09 AM */}
<p>{rt(-1, 'second')}</p> {/* 1 second ago */}
<p>{fn(1000000, { style: 'currency' })}</p> {/* $1,000,000.00 */}
</>
);
});To extract translations directly from the components, a command is available that automatically generates the files with the keys and default values.
See Qwik Speak Extract for more information on how to use it.
Using Qwik Speak Inline Vite plugin, translations are loaded and inlined during the build.
See Qwik Speak Inline Vite plugin for more information on how it works and how to use it.
stateDiagram-v2
State1: SpeakState
State2: SpeakConfig
State3: SpeakLocale
State4: TranslationFn
State5: Translation
State1 --> State2
State1 --> State3
State1 --> State4
State1 --> State5
note right of State2: configuration
note right of State3
- lang
- extension (Intl)
- currency
- timezone
- unit
end note
note right of State4
- loadTranslation$
end note
note right of State5
key-value pairs
of translation data
end note
SpeakStateis immutable: it cannot be updated after it is created and is not reactive.
useSpeakContext()Returns the Speak stateuseSpeakConfig()Returns the configuration in Speak contextuseSpeakLocale()Returns the locale in Speak context
defaultLocaleThe default locale to use as fallbacksupportedLocalesList of locales supported by the appassetsAn array of strings: each asset is passed to theloadTranslation$function to obtain data according to the languageruntimeAssetsAssets available at runtimekeySeparatorSeparator of nested keys. Default is.keyValueSeparatorKey-value separator. Default is@@
The SpeakLocale object contains the lang, in the format language[-script][-region], where:
languageISO 639 two-letter or three-letter codescriptISO 15924 four-letter script coderegionISO 3166 two-letter, uppercase code
and optionally contains:
extensionLanguage with Intl extensions, in the formatlanguage[-script][-region][-extensions]likeen-US-u-ca-gregory-nu-latnto format dates and numberscurrencyISO 4217 three-letter codetimeZoneFrom the IANA time zone databaseunitsKey value pairs of unit identifiers
TranslationFn interface can be implemented to change the behavior of the library:
loadTranslation$Function to load translation data
QwikSpeakProvider component provides the Speak context to the app. Props:
configSpeak configtranslationFnOptional functions to uselocaleOptional locale to uselangsOptional additional languages to preload data for (multilingual)
Speak component can be used for scoped translations. Props:
assetsAssets to loadruntimeAssetsAssets to load available at runtimelangsOptional additional languages to preload data for (multilingual)
-
$translate(keys: string | string[], params?: any, lang?: string)Translates a key or an array of keys. The syntax of the string iskey@@[default value] -
$inlineTranslate(keys: string | string[], ctx: SpeakState, params?: any, lang?: string)Translates a key or an array of keys outside the component$. The syntax of the string iskey@@[default value] -
useTranslate$()Returns the translate functions as QRL -
$plural(value: number | string, key?: string, params?: any, options?: Intl.PluralRulesOptions, lang?: string)Gets the plural by a number using Intl.PluralRules API
-
formatDate(value: Date | number | string, options?: Intl.DateTimeFormatOptions, lang?: string, timeZone?: string)Formats a date using Intl.DateTimeFormat API -
relativeTime(value: number | string, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions, lang?: string)Formats a relative time using Intl.RelativeTimeFormat API -
formatNumber(value: number | string, options?: Intl.NumberFormatOptions, lang?: string, currency?: string)Formats a number using Intl.NumberFormat API -
displayName(code: string, options: Intl.DisplayNamesOptions, lang?: string)Returns the translation of language, region, script or currency display names using Intl.DisplayNames API
cd packages/qwik-speak
npm run buildnpm testnpm startnpm run previewnpm test
npm run test.e2eMIT