Skip to content

Commit

Permalink
feat: 更新多语言取值的回退机制
Browse files Browse the repository at this point in the history
从原来默认取空,更改为默认取源多语言的值;这样解决了之前主项目
新增多语言,如果不更新 tb-i18n-loader,值回退到 key 上的问题
  • Loading branch information
靳昌 committed Aug 19, 2019
1 parent 32e4bdc commit 1d9227e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function (content) {
if (lang === descriptionAs) {
result = util.translate(lang, description)
} else {
result = util.translateLocales(lang, Object.keys(description))
result = util.translateLocales(lang, description)
}
results.push(result)
}
Expand Down
51 changes: 29 additions & 22 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ describe('test tb-i18n-loader', function () {
"key1": "key1"
}
`
const parsedDesciption = JSON.parse(desciption)

var expects = [
'var i18n = require(\'tb-i18n\');',
'i18n.setLocales(\'en\', {',
` "FRI": "${localesEN['FRI'] || ''}",`,
` "MON": "${localesEN['MON'] || ''}",`,
` "key1": "${localesEN['key1'] || ''}"`,
` "FRI": "${localesEN['FRI'] || parsedDesciption['FRI']}",`,
` "MON": "${localesEN['MON'] || parsedDesciption['MON']}",`,
` "key1": "${localesEN['key1'] || parsedDesciption['key1']}"`,
'});',
'module.exports = i18n;'
]
Expand All @@ -45,32 +47,34 @@ describe('test tb-i18n-loader', function () {
"key1": "key1"
}
`
const parsedDesciption = JSON.parse(desciption)

var expects = [
'var i18n = require(\'tb-i18n\');',
'i18n.setLocales(\'zh\', {',
` "FRI": "${localesZH['FRI'] || ''}",`,
` "MON": "${localesZH['MON'] || ''}",`,
` "key1": "${localesZH['key1'] || ''}"`,
` "FRI": "${localesZH['FRI'] || parsedDesciption['FRI']}",`,
` "MON": "${localesZH['MON'] || parsedDesciption['MON']}",`,
` "key1": "${localesZH['key1'] || parsedDesciption['key1']}"`,
'});',
'i18n.setLocales(\'zh_tw\', {',
` "FRI": "${localesZH_TW['FRI'] || ''}",`,
` "MON": "${localesZH_TW['MON'] || ''}",`,
` "key1": "${localesZH_TW['key1'] || ''}"`,
` "FRI": "${localesZH_TW['FRI'] || parsedDesciption['FRI']}",`,
` "MON": "${localesZH_TW['MON'] || parsedDesciption['MON']}",`,
` "key1": "${localesZH_TW['key1'] || parsedDesciption['key1']}"`,
'});',
'i18n.setLocales(\'en\', {',
` "FRI": "${localesEN['FRI'] || ''}",`,
` "MON": "${localesEN['MON'] || ''}",`,
` "key1": "${localesEN['key1'] || ''}"`,
` "FRI": "${localesEN['FRI'] || parsedDesciption['FRI']}",`,
` "MON": "${localesEN['MON'] || parsedDesciption['MON']}",`,
` "key1": "${localesEN['key1'] || parsedDesciption['key1']}"`,
'});',
'i18n.setLocales(\'ja\', {',
` "FRI": "${localesJA['FRI'] || ''}",`,
` "MON": "${localesJA['MON'] || ''}",`,
` "key1": "${localesJA['key1'] || ''}"`,
` "FRI": "${localesJA['FRI'] || parsedDesciption['FRI']}",`,
` "MON": "${localesJA['MON'] || parsedDesciption['MON']}",`,
` "key1": "${localesJA['key1'] || parsedDesciption['key1']}"`,
'});',
'i18n.setLocales(\'ko\', {',
` "FRI": "${localesKO['FRI'] || ''}",`,
` "MON": "${localesKO['MON'] || ''}",`,
` "key1": "${localesKO['key1'] || ''}"`,
` "FRI": "${localesKO['FRI'] || parsedDesciption['FRI']}",`,
` "MON": "${localesKO['MON'] || parsedDesciption['MON']}",`,
` "key1": "${localesKO['key1'] || parsedDesciption['key1']}"`,
'});',
'module.exports = i18n;'
]
Expand All @@ -81,20 +85,23 @@ describe('test tb-i18n-loader', function () {
var query = '?languages[]=en'
var contextTranslate = translate.bind({query: query})

const namespace = '@namespace: all.'
var desciption = `
@namespace: all.
${namespace}
{
"member": "member",
"project": "project",
"key1": "key1"
}
`
const parsedDesciption = JSON.parse(desciption.replace(namespace, ''))

var expects = [
'var i18n = require(\'tb-i18n\');',
'i18n.setLocales(\'en\', {',
` "all.member": "${localesEN['all.member'] || ''}",`,
` "all.project": "${localesEN['all.project'] || ''}",`,
` "all.key1": "${localesEN['all.key1'] || ''}"`,
` "all.member": "${localesEN['all.member'] || parsedDesciption['member']}",`,
` "all.project": "${localesEN['all.project'] || parsedDesciption['project']}",`,
` "all.key1": "${localesEN['all.key1'] || parsedDesciption['key1']}"`,
'});',
'module.exports = i18n;'
]
Expand Down
8 changes: 4 additions & 4 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ function getLocales (language) {
}

exports.translateLocales = translateLocales
function translateLocales (language, keys) {
function translateLocales (language, description) {
var originLocales = getLocales(language)
var locales = {}
keys.forEach(function (key) {
locales[key] = originLocales[key] || ''
})
for (var key in description) {
locales[key] = originLocales[key] || description[key]
}
return translate(language, locales)
}

Expand Down

0 comments on commit 1d9227e

Please sign in to comment.