Skip to content

Commit

Permalink
📝 Add documentation about nested key usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vthibault committed Apr 18, 2019
1 parent 3517a9e commit 32dbd0f
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Or install using [npm](https://npmjs.org):
- [frenchkiss.fallback()](#frenchkissfallbacklanguage-string-string)
- [frenchkiss.onMissingKey()](#frenchkissonMissingKeyfn-Function)
- [frenchkiss.onMissingVariable()](#frenchkissonMissingVariablefn-Function)
- [Nested keys](#nested-keys)
- [SELECT expression](#select-expression)
- [PLURAL expression](#plural-expression)
- [Plural category](#plural-category)
Expand Down Expand Up @@ -249,6 +250,54 @@ frenchkiss.t('hello'); // => 'Hello [missing:name] !'

---

### Nested keys

Under the hood, frenchkiss allows you to handle nested keys, by using `'.'` inside key names.

```js
frenchkiss.set('en', {
fruits: {
apple: 'An apple',
banana: 'A banana'
},
vegetables: {
carrot: 'A carrot',
daikon: 'A daikon'
}
});

frenchkiss.t('fruits.apple') // => 'An apple'
```

Accessing an object directly will result on the `onMissingKey` method to be called:

```js
frenchkiss.set('en', {
fruits: {
apple: 'An apple',
banana: 'A banana'
}
});

frenchkiss.onMissingKey(key => `[notfound:${key}]`);
frenchkiss.t('fruits'); // => '[notfound:fruits]'
```

In case of duplicate names on key and objects, the result will always prioritize the key value.

```js
frenchkiss.set('en', {
'fruits.apple': '(linear) apple'
fruits: {
apple: '(nested) apple'
}
});

frenchkiss.t('fruits.apple'); // => '(linear) apple'
```

---

### SELECT expression

If you need to display different text messages depending on the value of a variable, you need to translate all of those text messages... or you can handle this with a select ICU expression.
Expand Down

0 comments on commit 32dbd0f

Please sign in to comment.