Skip to content

Commit

Permalink
support attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jamuhl committed Sep 11, 2018
1 parent 59e6152 commit 358d252
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 0.0.2

- support accessing attributes on fluent segment using keys like `login.placeholder`

### 0.0.1

- initial version
8 changes: 6 additions & 2 deletions i18nextFluent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2176,12 +2176,16 @@

parse(res, options, lng, ns, key, info) {
const bundle = this.store.getBundle(lng, ns);
return bundle.format(res, options);
const isAttr = key.indexOf('.') > -1;
if (!res) return key;
const useRes = isAttr ? res.attrs[key.split('.')[1]] : res;
return bundle.format(useRes, options);
}

getResource(lng, ns, key, options) {
const bundle = this.store.getBundle(lng, ns);
return bundle.getMessage(key);
const useKey = key.indexOf('.') > -1 ? key.split('.')[0] : key;
return bundle.getMessage(useKey);
}

addLookupKeys(finalKeys, key, code, ns, options) {
Expand Down
2 changes: 1 addition & 1 deletion i18nextFluent.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "i18next-fluent",
"version": "0.0.1",
"version": "0.0.2",
"description": "i18nFormat plugin to use fluent format with i18next",
"main": "./index.js",
"jsnext:main": "dist/es/index.js",
Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,19 @@ class Fluent {

parse(res, options, lng, ns, key, info) {
const bundle = this.store.getBundle(lng, ns);
const isAttr = key.indexOf('.') > -1;

return bundle.format(res, options);
if (!res) return key;

const useRes = isAttr ? res.attrs[key.split('.')[1]] : res;
return bundle.format(useRes, options);
}

getResource(lng, ns, key, options) {
const bundle = this.store.getBundle(lng, ns);
const useKey = key.indexOf('.') > -1 ? key.split('.')[0] : key;

return bundle.getMessage(key);
return bundle.getMessage(useKey);
}

addLookupKeys(finalKeys, key, code, ns, options) {
Expand Down
10 changes: 7 additions & 3 deletions test/fuent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ describe('fluent format', () => {

it('should parse', () => {
const res = fluent.getResource('en', 'translations', 'logout')
expect(fluent.parse(res, {}, 'en', 'translations')).to.eql('Logout');
expect(fluent.parse(res, {}, 'en', 'translations', 'logout')).to.eql('Logout');

const res2 = fluent.getResource('en', 'translations', 'hello')
expect(fluent.parse(res2, { name: 'Jan' }, 'en', 'translations')).to.eql('Hello Jan.');
expect(fluent.parse(res2, { name: 'Jan' }, 'en', 'translations', 'hello')).to.eql('Hello Jan.');

const res3 = fluent.getResource('en', 'translations', 'restart-app')
expect(fluent.parse(res3, {}, 'en', 'translations')).to.eql('Zrestartuj Firefoxa.');
expect(fluent.parse(res3, {}, 'en', 'translations', 'restart-app')).to.eql('Zrestartuj Firefoxa.');

const res4 = fluent.getResource('en', 'translations', 'login.placeholder')
expect(fluent.parse(res4, {}, 'en', 'translations', 'login.placeholder')).to.eql('[email protected]');
});

});
Expand All @@ -67,6 +70,7 @@ describe('fluent format', () => {
expect(i18next.t('logout')).to.eql('Logout');
expect(i18next.t('hello', { name: 'Jan' })).to.eql('Hello Jan.');
expect(i18next.t('restart-app')).to.eql('Zrestartuj Firefoxa.');
expect(i18next.t('login.placeholder')).to.eql('[email protected]');
});

});
Expand Down

0 comments on commit 358d252

Please sign in to comment.