diff --git a/index.js b/index.js index 8f48034..f6dcfb5 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,4 @@ -/**! - * koa-locales - index.js - * +/** * Copyright(c) koajs and other contributors. * MIT Licensed * @@ -87,7 +85,11 @@ module.exports = function (app, options) { const locale = this.__getLocale(); const resource = resources[locale] || {}; - const text = resource[key] || key; + let text = resource[key]; + if (text === undefined) { + text = key; + } + debug('%s: %j => %j', locale, key, text); if (!text) { return ''; diff --git a/test/index.test.js b/test/index.test.js index 7cc7c2c..cb664d4 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -36,6 +36,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: 'emptyValue', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -76,6 +77,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: 'emptyValue', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -101,6 +103,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: '', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -125,6 +128,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: 'emptyValue', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -150,6 +154,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: '', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -174,6 +179,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: 'emptyValue', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -201,6 +207,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: '', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -232,6 +239,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: '', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -255,6 +263,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: '', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -278,6 +287,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: '', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -303,6 +313,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: 'emptyValue', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -328,6 +339,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: 'emptyValue', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -355,6 +367,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: '', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -382,6 +395,7 @@ describe('koa-locales.test.js', function () { empty: '', notexists_key: 'key not exists', empty_string: '', + empty_value: 'emptyValue', novalue: 'key %s ok', arguments3: '1 2 3', arguments4: '1 2 3 4', @@ -423,6 +437,7 @@ function createApp(options) { empty: this[fname](), notexists_key: this[fname]('key not exists'), empty_string: this[fname](''), + empty_value: this[fname]('emptyValue'), novalue: this[fname]('key %s ok'), arguments3: this[fname]('%s %s %s', 1, 2, 3), arguments4: this[fname]('%s %s %s %s', 1, 2, 3, 4), @@ -435,3 +450,4 @@ function createApp(options) { return app; } + diff --git a/test/locales/zh-CN.js b/test/locales/zh-CN.js index edf2843..5ac3b8d 100644 --- a/test/locales/zh-CN.js +++ b/test/locales/zh-CN.js @@ -2,6 +2,7 @@ module.exports = { Email: '邮箱', + emptyValue: '', 'Hello %s, how are you today?': '%s,今天过得如何?', model: { user: { diff --git a/test/locales/zh_TW.json b/test/locales/zh_TW.json index d3484f0..1378c43 100644 --- a/test/locales/zh_TW.json +++ b/test/locales/zh_TW.json @@ -1,5 +1,6 @@ { "Email": "郵箱", + "emptyValue": "", "Hello %s, how are you today?": "%s,今天過得如何?", "model": { "user": {