diff --git a/index.js b/index.js index 4ead61e..cf70cbe 100644 --- a/index.js +++ b/index.js @@ -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', @@ -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; } diff --git a/package.json b/package.json index 28e2c85..06f6ecf 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/index.test.js b/test/index.test.js index fee29e4..c8371aa 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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', }) @@ -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', }) @@ -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': '姓名', }) @@ -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', }) @@ -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': '姓名', }) @@ -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', }) @@ -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', }) @@ -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': '姓名', }) @@ -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': '姓名', }) @@ -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': '姓名', }) @@ -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': '姓名', }) @@ -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': '姓名', }) @@ -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', }) @@ -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', }) @@ -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', }) @@ -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': '姓名', }) @@ -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', }) @@ -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'}), }; });