Extract translatable (using ngx-translate) strings and save as a JSON or Gettext pot file
Go to file
dependabot[bot] 1c305ad81a
Bump minimist from 1.2.5 to 1.2.6
Bumps [minimist](https://github.com/substack/minimist) from 1.2.5 to 1.2.6.
- [Release notes](https://github.com/substack/minimist/releases)
- [Commits](https://github.com/substack/minimist/compare/1.2.5...1.2.6)

---
updated-dependencies:
- dependency-name: minimist
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-03-29 22:24:40 +00:00
.github Add username to GitHub sponsors 2020-04-16 15:00:34 +02:00
bin Rename cli script filename. Rename option 'dir' to 'input' 2017-03-20 15:51:38 +01:00
images add donate info 2019-07-31 13:06:09 +02:00
src enable piped argument on function calls (#233) 2021-04-14 10:00:44 +02:00
tests enable piped argument on function calls (#233) 2021-04-14 10:00:44 +02:00
.gitignore (feature) add support for expanding paths on Windows + added more usage examples to cli 2020-03-06 13:53:46 +01:00
.npmrc Require node >=v11.5.0 and make sure npm install fails if requirement is not met 2020-03-25 12:28:20 +01:00
LICENSE Create LICENSE 2017-07-08 14:10:54 +02:00
package-lock.json Bump minimist from 1.2.5 to 1.2.6 2022-03-29 22:24:40 +00:00
package.json Bump version 2021-04-14 10:10:21 +02:00
README.md fixes example call to generate i18n extracts (#225) 2021-04-14 10:03:06 +02:00
tsconfig.json Remove noUnusedLocals since they are removed by prettier on commit anyway 2020-04-16 09:06:40 +02:00
tslint.commit.json (chore) add husky, lint-staged and prettier 2019-09-18 14:11:12 +02:00
tslint.json add single quote rule 2020-04-01 10:21:11 +02:00

If this tool saves you time, please consider making a donation towards the continued maintainence and development: https://donate.biesbjerg.com

Donate

ngx-translate-extract

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 @biesbjerg/ngx-translate-extract --save-dev

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.