具有多种语言条目的 javascript 个对象的本地化
Localization of a javascript objects that has entries in several languages
我正在为以下问题而苦恼:
我想使用后备策略本地化一个以多种语言提供内容的项目,您可以在下面代码段的伪代码中看到该策略:我将如何在 javascript 中进行此操作?
// item is a string similar to this below: where some languages are set others are not
// let item = {'en': 'Good morning', 'de': 'Guten Morgen', 'fr': '', 'it': 'Ciao'};
// locale is a language and in ['de', 'fr', 'en', 'it']
let itemLocalize = function (item, locale) {
// return item at locale if it is set
// otherwise return first available language in the order of the array above
}
console.log(itemLocalize(item, 'de')); // 'Guten Morgen'
console.log(itemLocalize(item, 'fr')); // 'Good morning'
console.log(itemLocalize(item, 'it')); // 'Ciao'
console.log(itemLocalize(item, 'en')); // 'Good morning'
你可以使用这样的东西。相应地进行更改
let itemLocalize = function (locale) {
let confirmMessage = {
'en':'Good Morning',
'it':'',
'de':'Guten Morgen',
'fr': ''
}
let arr = ['de', 'fr', 'en', 'it'].find((el) => {
if (el) {
return confirmMessage[el];
}
});
return confirmMessage[locale] || confirmMessage[arr];
}
console.log(itemLocalize('fr'));
console.log(itemLocalize('en'));
console.log(itemLocalize('it'));
console.log(itemLocalize('de'));
我正在为以下问题而苦恼:
我想使用后备策略本地化一个以多种语言提供内容的项目,您可以在下面代码段的伪代码中看到该策略:我将如何在 javascript 中进行此操作?
// item is a string similar to this below: where some languages are set others are not
// let item = {'en': 'Good morning', 'de': 'Guten Morgen', 'fr': '', 'it': 'Ciao'};
// locale is a language and in ['de', 'fr', 'en', 'it']
let itemLocalize = function (item, locale) {
// return item at locale if it is set
// otherwise return first available language in the order of the array above
}
console.log(itemLocalize(item, 'de')); // 'Guten Morgen'
console.log(itemLocalize(item, 'fr')); // 'Good morning'
console.log(itemLocalize(item, 'it')); // 'Ciao'
console.log(itemLocalize(item, 'en')); // 'Good morning'
你可以使用这样的东西。相应地进行更改
let itemLocalize = function (locale) {
let confirmMessage = {
'en':'Good Morning',
'it':'',
'de':'Guten Morgen',
'fr': ''
}
let arr = ['de', 'fr', 'en', 'it'].find((el) => {
if (el) {
return confirmMessage[el];
}
});
return confirmMessage[locale] || confirmMessage[arr];
}
console.log(itemLocalize('fr'));
console.log(itemLocalize('en'));
console.log(itemLocalize('it'));
console.log(itemLocalize('de'));