Skip to content

Extract translatable (using ngx-translate) strings and save as a JSON or Gettext pot file

License

Notifications You must be signed in to change notification settings

bartholomej/ngx-translate-extract

This branch is 36 commits ahead of biesbjerg/ngx-translate-extract:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

daf7b83 · Sep 21, 2022
Sep 20, 2022
Sep 21, 2022
Nov 15, 2021
Jul 31, 2019
Sep 20, 2022
Nov 23, 2021
Nov 15, 2021
Nov 15, 2021
Mar 25, 2020
Nov 23, 2021
Jul 8, 2017
Sep 20, 2022
Sep 21, 2022
Nov 15, 2021
Nov 15, 2021
Sep 18, 2019
Apr 1, 2020
Sep 21, 2022

Repository files navigation

This is a fork of a great library by Biesbjerg. (But the original Beisbjerg's library is unfortunately no longer being developed)

This library adds compatibility for Angular 13 and Angular 14...

ngx-translate-extract

Angular translations extractor (plugin for @ngx-translate)

Angular 14, Ivy and Angular Universal (SSR) compatible

Extract translatable (ngx-translate) strings and save as a JSON or Gettext pot file. Merges with existing strings if the output file already exists.

Install

Install the package in your project:

npm install @bartholomej/ngx-translate-extract --save-dev
# yarn add @bartholomej/ngx-translate-extract --dev

Choose the version corresponding to your Angular version:

Angular ngx-translate-extract
14 8.x+
13 8.x+
8.x – 12.x @biesbjerg/ngx-translate-extract 7.x

Add a script to your project's package.json:

...
"scripts": {
  "i18n:init": "ngx-translate-extract --input ./src --output ./src/assets/i18n/template.json --key-as-default-value --replace --format json",
  "i18n:extract": "ngx-translate-extract --input ./src --output ./src/assets/i18n/{en,da,de,fi,nb,nl,sv}.json --clean --format json"
}
...

You can now run npm run i18n:extract and it will extract strings from your project.

Usage

Extract from dir and save to file

ngx-translate-extract --input ./src --output ./src/assets/i18n/strings.json

Extract from multiple dirs

ngx-translate-extract --input ./src-a ./src-b --output ./src/assets/i18n/strings.json

Extract and save to multiple files using path expansion

ngx-translate-extract --input ./src --output ./src/i18n/{da,en}.json

JSON indentation

Tabs are used by default for indentation when saving extracted strings in json formats:

If you want to use spaces instead, you can do the following:

ngx-translate-extract --input ./src --output ./src/i18n/en.json --format-indentation ' '

Marker function

If you want to extract strings that are not passed directly to TranslateService's get()/instant()/stream() methods, you can wrap them in a marker function to let ngx-translate-extract know you want to extract them.

Install marker function:

npm install @biesbjerg/ngx-translate-extract-marker
import { marker } from '@biesbjerg/ngx-translate-extract-marker';

marker('Extract me');

You can alias the marker function if needed:

import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';

_('Extract me');

Note: ngx-translate-extract will automatically detect the import name

Commandline arguments

Usage:
ngx-translate-extract [options]

Output
  --format, -f                Format    [string] [choices: "json", "namespaced-json", "pot"] [default: "json"]
  --format-indentation, --fi  Format indentation (JSON/Namedspaced JSON)                [string] [default: "	"]
  --sort, -s                  Sort strings in alphabetical order                                     [boolean]
  --clean, -c                 Remove obsolete strings after merge                                    [boolean]
  --replace, -r               Replace the contents of output file if it exists (Merges by default)   [boolean]

Extracted key value (defaults to empty string)
  --key-as-default-value, -k     Use key as default value                                            [boolean]
  --null-as-default-value, -n    Use null as default value                                           [boolean]
  --string-as-default-value, -d  Use string as default value                                          [string]

Options:
  --version, -v  Show version number                                                                 [boolean]
  --help, -h     Show help                                                                           [boolean]
  --input, -i    Paths you would like to extract strings from. You can use path expansion, glob patterns and
                 multiple paths        [array] [required] [default: ["/Users/kim/apps/ngx-translate-extract"]]
  --output, -o   Paths where you would like to save extracted strings. You can use path expansion, glob
                 patterns and multiple paths                                                [array] [required]

Examples:
  ngx-translate-extract -i ./src-a/ -i ./src-b/ -o strings.json           Extract (ts, html) from multiple paths
  ngx-translate-extract -i './{src-a,src-b}/' -o strings.json             Extract (ts, html) from multiple paths using brace
                                                           expansion
  ngx-translate-extract -i ./src/ -o ./i18n/da.json -o ./i18n/en.json     Extract (ts, html) and save to da.json and en.json
  ngx-translate-extract -i ./src/ -o './i18n/{en,da}.json'                Extract (ts, html) and save to da.json and en.json
                                                           using brace expansion
  ngx-translate-extract -i './src/**/*.{ts,tsx,html}' -o strings.json     Extract from ts, tsx and html
  ngx-translate-extract -i './src/**/!(*.spec).{ts,html}' -o              Extract from ts, html, excluding files with ".spec"
  strings.json

Note for GetText users

Please pay attention of which version of gettext-parser you actually use in your project. For instance, gettext-parser:1.2.2 does not support HTML tags in translation keys.

Credits

About

Extract translatable (using ngx-translate) strings and save as a JSON or Gettext pot file

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.8%
  • Other 0.2%