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

feat: 更新多语言取值的回退机制 #12

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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AmosJin 这里我们有特别要用 in 的原因吗?会不会可以像上边一样用 Object.keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有什么特别的区别吗?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AmosJin 我们只要 own property 的遍历,我个人觉得 Object.keys 准确一些 :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得都 ok,LGTM

locales[key] = originLocales[key] || description[key]
}
return translate(language, locales)
}

Expand Down