update README

This commit is contained in:
Kim Biesbjerg 2019-07-31 10:28:09 +02:00
parent 71cc6e6883
commit 8b014abf49

View File

@ -1,69 +1,77 @@
If you like this project please show your support with a GitHub star. Much appreciated!
# Usage
# ngx-translate-extract
## 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
### Usage
Install the package in your project:
`npm install @biesbjerg/ngx-translate-extract --save-dev`
Add an `extract` script to your project's `package.json`:
```
Add a script to your project's `package.json`:
```json
...
"scripts": {
"extract": "ngx-translate-extract --input ./src --output ./src/assets/i18n/ --clean --sort --format namespaced-json"
"extract-i18n": "ngx-translate-extract --input ./src --output ./src/assets/i18n/ --clean --sort --format namespaced-json"
}
...
```
You can now run `npm run extract` to extract strings.
You can now run `npm run extract-i18n` and it will extract strings from your project.
## Extract examples
### Examples
**Extract from dir and save to file**
`ngx-translate-extract -i ./src -o ./src/i18n/strings.json`
`ngx-translate-extract --input ./src --output ./src/assets/i18n/template.json`
**Extract from multiple dirs**
`ngx-translate-extract -i ./src/folder-a ./src/folder-b -o ./src/i18n/strings.json`
`ngx-translate-extract --input ./src-a ./src-b --output ./src/assets/i18n/template.json`
**Extract and save to multiple files**
`ngx-translate-extract -i ./src -o ./src/i18n/{da,en,fr}.json`
**Extract and save to multiple files using path expansion**
**or**
_Note: This method does not work on Windows!_
`ngx-translate-extract -i ./src -o ./src/i18n/da.json ./src/i18n/en.json ./src/i18n/fr.json`
`ngx-translate-extract --input ./src --output ./src/i18n/{da,en}.json`
**or (update only)**
On Windows you must specify each output destination individually:
`ngx-translate-extract -i ./src -o ./src/i18n/*.json`
`ngx-translate-extract --input ./src --output ./src/i18n/da.json ./src/i18n/en.json`
**or (update only)**
## Custom indentation
By default, tabs are used for indentation when writing extracted strings to json formats:
`ngx-translate-extract -i ./src -o ./src/i18n/en.json --format-indentation $'\t'`
### 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 -i ./src -o ./src/i18n/en.json --format-indentation ' '`
`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, you can wrap them in a custom 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 custom marker function to let `ngx-translate-extract` know you want to extract them.
Install marker function:
`npm install @biesbjerg/ngx-translate-extract-marker`
```ts
import { _ } from '@biesbjerg/ngx-translate-extract';
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
_('Extract me');
marker('Extract me');
```
Add the `marker` argument when running the extract script:
`ngx-translate-extract ... -m _`
`ngx-translate-extract ... -m extract`
Modify the scripts arguments as required.
You can alias the marker function if needed:
```ts
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
_('Extract me');
```
`ngx-translate-extract ... -m _`
## Commandline arguments
```
@ -76,7 +84,7 @@ Options:
--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]
[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
@ -86,12 +94,12 @@ Options:
[string] [default: false]
--format, -f Output format
[string] [choices: "json", "namespaced-json", "pot"] [default: "json"]
--format-indentation, --fi Output format indentation [string] [default: "\t"]
--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]
--verbose, -vb If true, prints all processed file paths to console
[boolean] [default: true]
--key-as-default-value, -k Use key as default value for translations
[boolean] [default: false]