Qwik Speak Extract
Extract translations directly from the components
Usage
Command
Get the code ready
Optionally, you can use a default value for the keys. The syntax is key@@[default value]:
<p>{t('title@@Qwik Speak'}</p>
<p>{t('greeting@@Hi! I am {{name}}', { name: 'Qwik Speak' })}</p>
When you use a default value, it will be used as initial value for the key in every translation.
Note that it is not necessary to provide the default value of a key every time: it is sufficient and not mandatory to provide it once in the app
Naming conventions
If you use scoped translations, the first property will be used as filename:
<p>{t('app.title)}</p>
<p>{t('home.greeting)}</p>will generate two files for each language:
i18n/
│
└───en-US/
│ app.json
│ home.json
└───it-IT/
app.json
home.jsonNot scoped translations will be placed in a single file, called app.json
Configuration
Add the command in package.json, and provide at least the supported languages:
"scripts": {
"qwik-speak-extract": "qwik-speak-extract --supportedLangs=en-US,it-IT --assetsPath=i18n"
}Available options:
supportedLangsSupported langs. RequiredbasePathThe base path. Default to'./'sourceFilesPathsPaths to files to search for translations. Default to'src'excludedPathsPaths to excludeassetsPathPath to translation files:[basePath]/[assetsPath]/[lang]/*.json. Default to'i18n'formatThe format of the translation files. Default to'json'filenameFilename for not scoped translations. Default is'app'fallbackOptional function to implement a fallback strategyautoKeysAutomatically handle keys for each string. Default is falseunusedKeysAutomatically remove unused keys from assets, except in runtime assetsruntimeAssetsComma-separated list of runtime assets to preservekeySeparatorSeparator of nested keys. Default is'.'keyValueSeparatorKey-value separator. Default is'@@'
Note. Currently, only
jsonis supported as format
Running
npm run qwik-speak-extractUpdating
If you add new translations in the components, or a new language, they will be merged into the existing files without losing the translations already made.
Using it programmatically
Rather than using the command, you can invoke qwikSpeakExtract function:
import { qwikSpeakExtract } from 'qwik-speak/extract';
await qwikSpeakExtract({
supportedLangs: ['en-US', 'it-IT']
});Translations fallback
By default, the extract command uses the default value of translations as the initial value when provided. You can extend this behavior, to complete the missing translations by taking them from another language, by implementing a custom function with this signature:
(translation: Translation) => Translationand pass the function to fallback option:
await qwikSpeakExtract({
supportedLangs: supportedLangs,
fallback: fallback
});For example, if you want to use the default language translations for missing values in other languages:
import { qwikSpeakExtract, deepMergeMissing } from 'qwik-speak/extract';
const defaultLang = 'en-US';
const supportedLangs = ['en-US', 'it-IT', 'de-DE'];
/**
* Fallback missing values to default lang
*/
const fallback = (translation) => {
const defaultTranslation = translation[defaultLang];
for (const lang of supportedLangs) {
if (lang !== defaultLang) {
deepMergeMissing(translation[lang], defaultTranslation);
}
}
return translation;
};Automatic removal of unused keys
To remove unused keys from json files, you need to enable the option and provide a comma-separated list of runtime file names:
"scripts": {
"qwik-speak-extract": "qwik-speak-extract --supportedLangs=en-US,it-IT --unusedKeys=true --runtimeAssets=runtime"
}Last updated