Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support nested object #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const fs = require('fs');
const path = require('path');
const ms = require('humanize-ms');
const assign = require('object-assign');
const result = require('lodash.result');

const DEFAULT_OPTIONS = {
defaultLocale: 'en-US',
Expand Down Expand Up @@ -228,7 +229,7 @@ function formatWithArray(text, values) {
const Object_INDEX_RE = /\{(.+?)\}/g;
function formatWithObject(text, values) {
return text.replace(Object_INDEX_RE, function (orignal, matched) {
const value = values[matched];
const value = result(values, matched);
if (value) {
return value;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"debug": "^2.6.0",
"humanize-ms": "^1.2.0",
"ini": "^1.3.4",
"lodash.result": "^4.5.2",
"object-assign": "^4.1.0"
},
"devDependencies": {
Expand Down
18 changes: 18 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
Expand Down Expand Up @@ -84,6 +85,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
Expand All @@ -110,6 +112,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': '性别',
'name': '姓名',
})
Expand All @@ -135,6 +138,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
Expand All @@ -161,6 +165,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': '性别',
'name': '姓名',
})
Expand All @@ -186,6 +191,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
Expand All @@ -211,6 +217,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
Expand All @@ -236,6 +243,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': '性别',
'name': '姓名',
})
Expand Down Expand Up @@ -269,6 +277,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': '性别',
'name': '姓名',
})
Expand Down Expand Up @@ -301,6 +310,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': '性别',
'name': '姓名',
})
Expand All @@ -325,6 +335,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': '性别',
'name': '姓名',
})
Expand All @@ -349,6 +360,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': '性别',
'name': '姓名',
})
Expand All @@ -375,6 +387,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
Expand All @@ -401,6 +414,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
Expand All @@ -427,6 +441,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
Expand Down Expand Up @@ -455,6 +470,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': '性別',
'name': '姓名',
})
Expand Down Expand Up @@ -483,6 +499,7 @@ describe('koa-locales.test.js', function () {
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
nestedObject: 'foo bar',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
Expand Down Expand Up @@ -525,6 +542,7 @@ function createApp(options) {
arguments6: this[fname]('%s %s %s %s %s.', 1, 2, 3, 4, 5, 6),
values: this[fname]('{0} {1} {0} {1} {2} {100}', ['foo', 'bar']),
object: this[fname]('{foo} {bar} {foo} {bar} {z}', {foo: 'foo', bar: 'bar'}),
nestedObject: this[fname]('{foo.bar} {bar}', {foo: { bar: 'foo'}, bar: 'bar'}),
};
});

Expand Down