Skip to content

Commit

Permalink
Adds YAML to dev dependencies and improves README and test changes pe…
Browse files Browse the repository at this point in the history
…r review comment
  • Loading branch information
mathiashsteffensen committed Jan 23, 2022
1 parent b262d7d commit e756994
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,9 @@ i18n.configure({
disable: false
},

// Parse file contents with a YAML parser
// The parser can be any object that responds to .parse & .stringify
parser: YAML
// Parser can be any object that responds to .parse & .stringify
parser: JSON
})

```

The locale itself is gathered directly from the browser by header, cookie or query parameter depending on your setup.
Expand Down Expand Up @@ -380,6 +378,18 @@ i18n.configure({

**NOTE:** Enabling `staticCatalog` disables all other fs realated options such as `updateFiles`, `autoReload` and `syncFiles`

#### Some words on `parser` option

Instead of parsing all file contents as JSON, you can parse them as YAML or any other format you like
```js
const YAML = require('yaml')

i18n.configure({
extension: '.yml',
parser: YAML
})
```

### i18n.init()

When used as middleware in frameworks like express to setup the current environment for each loop. In contrast to configure the `i18n.init()` should be called within each request-response-cycle.
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"prettier": "^2.5.1",
"should": "^13.2.3",
"sinon": "^12.0.1",
"yaml": "^1.10.2",
"zombie": "^6.1.4"
},
"engines": {
Expand Down
13 changes: 6 additions & 7 deletions test/i18n.configureParser.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
const i18n = require('..')
const should = require('should')
const { I18n } = require('..')
const YAML = require('yaml')
require('should')

describe('configure parser', function () {
context('with YAML parser', () => {
i18n.configure({
context('with YAML parser', function () {
const i18n = new I18n({
locales: ['en', 'de'],
parser: YAML
})

it('should parse the locale files with the YAML parser', function () {
should.equal(i18n.__('Hello'), 'Hello')
i18n.__('Hello').should.equal('Hello')
})

it('should write unknown keys to the catalog', function () {
i18n.__('does.not.exist')

var catalog = i18n.getCatalog()
catalog.should.have.property('does.not.exist', 'does.not.exist')
i18n.getCatalog().should.have.property('does.not.exist', 'does.not.exist')
})
})
})

0 comments on commit e756994

Please sign in to comment.