Skip to content

Commit

Permalink
Support false for separator options (closes #81)
Browse files Browse the repository at this point in the history
  • Loading branch information
karellm committed Mar 27, 2018
1 parent cbbe606 commit c99ee00
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
49 changes: 25 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,35 @@ gulp.task('i18next', function() {

## Options

Option | Description | Default
Option | Description | Default
---------------------- | ----------------------------------------------------- | ---
**contextSeparator** | Key separator used in your translation keys | `_`
**createOldCatalogs** | Save the \_old files | `true`
**defaultNamespace** | Default namespace used in your i18next config | `translation`
**defaultValue** | Default value to give to empty keys | `''`
**extension** | Edit the extension of the locale files | `.json`
**filename** | Edit the filename of the locale files | `'$NAMESPACE'`
**indentation** | Indentation of the catalog files | `2`
**keepRemoved** | Keep keys from the catalog that are no longer in code | `false`
**keySeparator** | Key separator used in your translation keys | `.`
**lexers** | See below for details | `{}`
**lineEnding** | Control the line ending. See options at [eol](https://github.com/ryanve/eol) | `auto`
**locales** | An array of the locales in your applications | `['en','fr']`
**namespaceSeparator** | Namespace separator used in your translation keys | `:`
**output** | Where to write the locale files relative to the base | `locales`
**reactNamespace** | For react file, extract the [defaultNamespace](https://react.i18next.com/components/translate-hoc.html) | `false` (`true` for `.jsx` files)
**sort** | Whether or not to sort the catalog | `false`

### Catalog filenames

Both `filename` and `extension` options support injection of `$LOCALE` and `$NAMESPACE` variables.
**contextSeparator** | Key separator used in your translation keys | `_`
**createOldCatalogs** | Save the \_old files | `true`
**defaultNamespace** | Default namespace used in your i18next config | `translation`
**defaultValue** | Default value to give to empty keys | `''`
**extension** <sup>1<sup>| Extenstion of the catalogs | `.json`
**filename** <sup>1<sup>| Filename of the catalogs | `'$NAMESPACE'`
**indentation** | Indentation of the catalog files | `2`
**keepRemoved** | Keep keys from the catalog that are no longer in code | `false`
**keySeparator** <sup>2<sup>| Key separator used in your translation keys | `.`
**lexers** | See below for details | `{}`
**lineEnding** | Control the line ending. See options at [eol](https://github.com/ryanve/eol) | `auto`
**locales** | An array of the locales in your applications | `['en','fr']`
**namespaceSeparator** <sup>2<sup>| Namespace separator used in your translation keys | `:`
**output** | Where to write the locale files relative to the base | `locales`
**reactNamespace** <sup>3<sup>| For react file, extract the [defaultNamespace](https://react.i18next.com/components/translate-hoc.html) | `false`
**sort** | Whether or not to sort the catalog | `false`

1. Both `filename` and `extension` options support injection of `$LOCALE` and `$NAMESPACE` variables. The file output is JSON by default, if you want YAML, the `extension` must end with `yml`.
2. If you want to use plain english keys, separators such as `.` and `:` will conflict. You might want to set `keySeparator: false` and `namespaceSeparator: false`. That way, `t('Status: Loading...')` will not think that there are a namespace and three separator dots for instance.
3. If the file being parsed has a `.jsx` extension, this option is ignored and the namespace is being extracted.


### Lexers

The `lexers` option let you configure which Lexer to use for which extension. Here is the default:

```
```js
{
lexers: {
hbs: ['HandlebarsLexer'],
Expand All @@ -118,7 +119,7 @@ The `lexers` option let you configure which Lexer to use for which extension. He

Note the presence of a `default` which will catch any extension that is not listed. There are 3 lexers available: `HandlebarsLexer`, `HTMLLexer` and `JavascriptLexer`. Each has configurations of its own. If you need to change the defaults, you can do it like so:

```
```js
{
lexers: {
hbs: [
Expand All @@ -127,7 +128,7 @@ Note the presence of a `default` which will catch any extension that is not list
functions: ['translate', '__']
}
],
...
// ...
}
}
```
Expand Down
6 changes: 6 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ i18nTransform = function (_Transform) {_inherits(i18nTransform, _Transform);


_this.options = _extends({}, _this.defaults, options);
if (_this.options.keySeparator === false) {
_this.options.keySeparator = '__!NO_KEY_SEPARATOR!__';
}
if (_this.options.namespaceSeparator === false) {
_this.options.namespaceSeparator = '__!NO_NAMESPACE_SEPARATOR!__';
}
_this.entries = [];

_this.parser = new _parser2.default(_this.options);
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export default class i18nTransform extends Transform {
}

this.options = { ...this.defaults, ...options }
if (this.options.keySeparator === false) {
this.options.keySeparator = '__!NO_KEY_SEPARATOR!__'
}
if (this.options.namespaceSeparator === false) {
this.options.namespaceSeparator = '__!NO_NAMESPACE_SEPARATOR!__'
}
this.entries = []

this.parser = new Parser(this.options)
Expand Down
24 changes: 24 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,30 @@ describe('parser', () => {
i18nextParser.end(fakeFile)
})

it('handles disabling namespace and key separators', (done) => {
let result
const i18nextParser = new i18nTransform({
namespaceSeparator: false,
keySeparator: false
})
const fakeFile = new Vinyl({
contents: Buffer.from("asd t('Status: loading...')"),
path: 'file.js'
})

i18nextParser.on('data', file => {
if (file.relative.endsWith(enLibraryPath)) {
result = JSON.parse(file.contents)
}
})
i18nextParser.once('end', () => {
assert.deepEqual(result, { 'Status: loading...': '' })
done()
})

i18nextParser.end(fakeFile)
})

it('supports a defaultValue', (done) => {
let result
const i18nextParser = new i18nTransform({
Expand Down

0 comments on commit c99ee00

Please sign in to comment.