Keys Extractor
The Keys Extractor is a core feature of Transloco Keys Manager designed to identify all the translation keys in your project and generate corresponding entries in your translation files. It helps streamline the translation workflow by automating tedious tasks and ensuring consistency across the codebase.
CLI Usage
To use the Keys Extractor via CLI:
Add a script to your package.json:
"scripts": {
"extract": "transloco-keys-manager extract"
}Run the extractor:
pnpm extractThis command scans your project for translation keys and updates your translation files accordingly.
Scopes Support
The extractor supports scopes out of the box. When you define a new scope in the providers array:
import { TRANSLOCO_SCOPE, provideTranslocoScope } from '@jsverse/transloco';
@Component({
templateUrl: './admin-page.component.html',
providers: [{ provide: TRANSLOCO_SCOPE, useValue: 'admin' }, provideTranslocoScope('todo'), provideTranslocoScope(['another', { scope: 'reallyLong', alias: 'rl' }])],
})
export class AdminPageComponent {}<ng-container *transloco="let t">{{ t('admin.title') }}</ng-container>It'll extract the scope (admin in our case) keys into the relevant folder:
📦 assets
┗ 📂 i18n
┃ ┣ 📂 admin
┃ ┃ ┣ en.json
┃ ┃ ┗ es.json
┃ ┣ en.json
┃ ┗ es.jsonInline Loaders
Let's say that we're using the following inline loader:
We can add it to the scopePathMap key in the transloco.config.ts file:
Now, it'll create the files in the provided folder.
Dynamic Keys
There are times when we need to extract keys with values that may change during runtime. One example can be when you need to use a dynamic expression:
To support such cases, you can add a special comment to your code, which tells the CLI to extract it. It can be added to Typescript files:
Or to templates:
When using comments in the templates they will also inherit the prefix input value (if exists), and will be prefixed with it:
The extracted keys for the code above will be:
Marker Function
If you want to extract some standalone strings that are not part of any translation call (via the template or service) you can wrap them with the marker function to tell the keys manager to extract them:
The marker function will return the string which was passed to it. You can alias the marker function if needed:
Extra Support
The prefix input:
prefix input:The extracted keys for the code above will be:
Static ternary operators
Supports params:
Last updated
Was this helpful?

