Extract translatable (using ngx-translate) strings and save as a JSON or Gettext pot file
Go to file
2019-09-16 17:52:41 +02:00
.github add donate info 2019-07-31 13:06:09 +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 (feat) add argument --null-as-default-value to use null as default value for extracted translations 2019-09-16 17:52:41 +02:00
tests (feat) add argument --null-as-default-value to use null as default value for extracted translations 2019-09-16 17:52:41 +02:00
.gitignore Fix crash when constructor parameter has no type. Closes #38 2017-05-05 11:31:30 +02:00
LICENSE Create LICENSE 2017-07-08 14:10:54 +02:00
package-lock.json - (chore) update packages 2019-09-16 16:40:37 +02:00
package.json (feat) add argument --null-as-default-value to use null as default value for extracted translations 2019-09-16 17:52:41 +02:00
README.md (feat) add argument --null-as-default-value to use null as default value for extracted translations 2019-09-16 17:52:41 +02:00
tsconfig.json (bugfix) extract strings encapsulated with backticks. Closes #139 2019-08-26 12:29:52 +02:00
tslint.json Add experimental AstServiceParser 2017-01-28 15:22:08 +01:00

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

Donate

Usage

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.

Usage

Install the package in your project:

npm install @biesbjerg/ngx-translate-extract --save-dev

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

...
"scripts": {
  "extract-i18n": "ngx-translate-extract --input ./src --output ./src/assets/i18n/ --clean --sort --format namespaced-json"
}
...

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

Examples

Extract from dir and save to file

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

Extract from multiple dirs

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

Extract and save to multiple files using path expansion

Note: This method does not work on Windows!

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

On Windows you must specify each output destination individually:

ngx-translate-extract --input ./src --output ./src/i18n/da.json ./src/i18n/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 ' '

Mark strings for extraction using a marker function

If, for some reason, you want to extract strings not passed directly to TranslateService's get() or instant() 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');

Add the marker argument when running the extract script:

ngx-translate-extract ... -m extract

You can alias the marker function if needed:

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

_('Extract me');

ngx-translate-extract ... -m _

Commandline arguments

Usage:
ngx-translate-extract [options]

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] [default: current working path]
  --patterns, -p              Extract strings from the following file patterns
                                    [array] [default: ["/**/*.html","/**/*.ts"]]
  --output, -o                Paths where you would like to save extracted
                              strings. You can use path expansion, glob patterns
                              and multiple paths              [array] [required]
  --format, -f                Output format
          [string] [choices: "json", "namespaced-json", "pot"] [default: "json"]
  --format-indentation, --fi  Output format indentation   [string] [default: "	"]
  --replace, -r               Replace the contents of output file if it exists
                              (Merges by default)     [boolean] [default: false]
  --sort, -s                  Sort strings in alphabetical order when saving
                                                      [boolean] [default: false]
  --clean, -c                 Remove obsolete strings when merging
                                                      [boolean] [default: false]
  --key-as-default-value, -k   Use key as default value for translations
                                                      [boolean] [default: false]
  --null-as-default-value, -n  Use null as default value for translations
                                                      [boolean] [default: false]

Arguments key-as-default-value and null-as-default-value are mutually exclusive