2019-07-31 14:06:09 +03:00
If this tool saves you time, please consider making a donation towards the continued maintainence and development: https://donate.biesbjerg.com
[![Donate ](images/donate-badge.png )](https://donate.biesbjerg.com)
2019-07-31 11:28:09 +03:00
# Usage
2017-01-28 17:32:16 +03:00
2019-07-31 11:28:09 +03:00
## ngx-translate-extract
2017-03-07 17:56:26 +03:00
Extract translatable (ngx-translate) strings and save as a JSON or Gettext pot file.
2016-12-20 18:02:56 +03:00
Merges with existing strings if the output file already exists.
2016-12-03 17:09:39 +03:00
2019-07-31 11:28:09 +03:00
### Usage
2016-12-20 18:02:56 +03:00
Install the package in your project:
2016-12-03 17:09:39 +03:00
2017-03-07 17:56:26 +03:00
`npm install @biesbjerg/ngx-translate-extract --save-dev`
2016-12-05 01:14:00 +03:00
2019-07-31 11:28:09 +03:00
Add a script to your project's `package.json` :
```json
...
2016-12-05 01:14:00 +03:00
"scripts": {
2019-07-31 11:28:09 +03:00
"extract-i18n": "ngx-translate-extract --input ./src --output ./src/assets/i18n/ --clean --sort --format namespaced-json"
2016-12-05 01:14:00 +03:00
}
2019-07-31 11:28:09 +03:00
...
2016-12-05 01:14:00 +03:00
```
2019-07-31 11:28:09 +03:00
You can now run `npm run extract-i18n` and it will extract strings from your project.
2017-03-21 17:59:18 +03:00
2019-07-31 11:28:09 +03:00
### Examples
2017-03-21 17:59:18 +03:00
**Extract from dir and save to file**
2019-07-31 11:28:09 +03:00
`ngx-translate-extract --input ./src --output ./src/assets/i18n/template.json`
2017-03-21 17:59:18 +03:00
**Extract from multiple dirs**
2019-07-31 11:28:09 +03:00
`ngx-translate-extract --input ./src-a ./src-b --output ./src/assets/i18n/template.json`
2017-03-21 17:59:18 +03:00
2019-07-31 11:28:09 +03:00
**Extract and save to multiple files using path expansion**
2017-03-21 17:59:18 +03:00
2019-07-31 11:28:09 +03:00
_Note: This method does not work on Windows!_
2017-03-21 17:59:18 +03:00
2019-07-31 11:28:09 +03:00
`ngx-translate-extract --input ./src --output ./src/i18n/{da,en}.json`
2017-03-21 17:59:18 +03:00
2019-07-31 11:28:09 +03:00
On Windows you must specify each output destination individually:
2017-03-21 17:59:18 +03:00
2019-07-31 11:28:09 +03:00
`ngx-translate-extract --input ./src --output ./src/i18n/da.json ./src/i18n/en.json`
2017-03-29 15:20:01 +03:00
2019-07-31 11:28:09 +03:00
### JSON indentation
Tabs are used by default for indentation when saving extracted strings in json formats:
2017-03-29 15:20:01 +03:00
If you want to use spaces instead, you can do the following:
2019-07-31 11:28:09 +03:00
`ngx-translate-extract --input ./src --output ./src/i18n/en.json --format-indentation ' '`
2017-03-29 15:20:01 +03:00
2017-03-30 15:37:30 +03:00
## Mark strings for extraction using a marker function
2019-09-16 17:40:37 +03:00
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.
2019-07-31 11:28:09 +03:00
Install marker function:
`npm install @biesbjerg/ngx-translate-extract-marker`
2017-03-30 15:37:30 +03:00
```ts
2019-07-31 11:28:09 +03:00
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
2017-03-30 15:37:30 +03:00
2019-07-31 11:28:09 +03:00
marker('Extract me');
2017-03-30 15:37:30 +03:00
```
Add the `marker` argument when running the extract script:
2019-07-31 11:28:09 +03:00
`ngx-translate-extract ... -m extract`
You can alias the marker function if needed:
2016-12-05 01:14:00 +03:00
2019-07-31 11:28:09 +03:00
```ts
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
_('Extract me');
```
`ngx-translate-extract ... -m _`
2016-12-05 01:14:00 +03:00
## Commandline arguments
2019-07-31 14:06:09 +03:00
```shell
2016-12-05 01:14:00 +03:00
Usage:
2017-03-21 17:59:18 +03:00
ngx-translate-extract [options]
2016-12-05 01:14:00 +03:00
Options:
2017-03-29 15:20:01 +03:00
--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
2019-07-31 11:28:09 +03:00
[array] [default: current working path]
2017-03-29 15:20:01 +03:00
--patterns, -p Extract strings from the following file patterns
2017-03-21 17:59:18 +03:00
[array] [default: ["/**/*.html","/**/*.ts"]]
2017-03-29 15:20:01 +03:00
--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
2017-03-21 17:59:18 +03:00
[string] [choices: "json", "namespaced-json", "pot"] [default: "json"]
2019-07-31 11:28:09 +03:00
--format-indentation, --fi Output format indentation [string] [default: " "]
2017-03-29 15:20:01 +03:00
--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
2017-03-21 17:59:18 +03:00
[boolean] [default: false]
2019-09-16 18:52:41 +03:00
--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