为了账号安全,请及时绑定邮箱和手机立即绑定

能够将一个或多个词从一种语言映射到另一种语言的可靠国际化方法是什么样子的?

能够将一个或多个词从一种语言映射到另一种语言的可靠国际化方法是什么样子的?

DIEA 2023-06-09 15:27:26
我有一系列工作日 [“星期一”,“星期二”..等],我需要将每一天翻译成另一种语言。let weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']; translatedArray = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi'];最好的方法是什么?我需要输出一个带有翻译数组的新数组
查看完整描述

5 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

您可以使用Array reduce方法来制作结果对象。


let weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];


let translatedArray = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi"];


let ret = weekdays.reduce((p, c, i) => {

  p[c] = translatedArray[i];

  return p;

}, {});

console.log(ret);

console.log(ret["Tuesday"]); 


查看完整回答
反对 回复 2023-06-09
?
开满天机

TA贡献1786条经验 获得超13个赞

我的简单方法是在这样的对象中维护一个 json


let weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];

let anotherLanguageDays = {

   "Monday" : "星期一",

    "Tuesday" : "星期一",

    ... and so on

};

现在当你遍历数组时你可以做这样的事情


weekdays.forEach(d => {

   console.log(anotherLanguageDays[d]);

})

您的代码的完整示例...


let weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];



let anotherLangauge = {

  Monday : "Lundi",

  Tuesday : "Mardi",

  Wednesday : "Mercredi",

  Thursday : "Jeudi",

  Friday : "Vendredi"

};



weekdays.forEach(d => {

  console.log(anotherLangauge[d]);

})


查看完整回答
反对 回复 2023-06-09
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

使用代表语言代码的键创建一个对象。每种语言代码都有另一个对象,其中包含单词和该语言中单词的翻译。


编写一个函数,该函数接受一个字符串数组作为您要翻译的单词,以及一个指示从哪种语言获取翻译的语言代码。


确保将所有键都小写以保持统一。这些值没有小写。


按照这种方法,您可以通过添加更多语言和单词来轻松扩展。


const translations = {

  'en-US': {

    monday: 'Monday',

    tuesday: 'Tuesday',

    wednesday: 'Wednesday', 

    thursday: 'Thursday', 

    friday: 'Friday',

    saturday: 'Saturday',

    sunday: 'Sunday'

  },

  'fr-FR': {

    monday: 'Lundi',

    tuesday: 'Mardi',

    wednesday: 'Mecredi', 

    thursday: 'Jeudi', 

    friday: 'Vendredi',

    saturday: 'Samedi',

    sunday: 'Dimanche'

  },

  'nl-NL': {

    monday: 'Maandag',

    tuesday: 'Dinsdag',

    wednesday: 'Woensdag', 

    thursday: 'Donderdag', 

    friday: 'Vrijdag',

    saturday: 'Zaterdag',

    sunday: 'Zondag'

  }

};


const translate = (strings, language) => {

  if (!translations.hasOwnProperty(language)) {

    throw new Error(`Language ${language} does not exist in the translations object.`);

  }

  const set = translations[language];

  return Object.entries(set)

    .filter(([ key, value ]) => strings.includes(key))

    .map(([ key, value ]) => value);

};


let weekdays = [

  'monday', 

  'tuesday', 

  'wednesday', 

  'thursday', 

  'friday'

];


const frenchWeekdays = translate(weekdays, 'fr-FR');

const englishWeekdays = translate(weekdays, 'en-US');

const dutchWeekdays = translate(weekdays, 'nl-NL');


console.log(frenchWeekdays);

console.log(englishWeekdays);

console.log(dutchWeekdays);


// Example with non existent translations.

translate(weekdays, 'de-DE');


查看完整回答
反对 回复 2023-06-09
?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

它主要归结为选择最方便的mapping 方法。一种可能的方法可能类似于下一个提供的示例代码......

const i18nMap = {

  "en-GB": {

    weekdays: {

      monday: "Monday",

      tuesday: "Tuesday",

      wednesday: "Wednesday",

      thursday: "Thursday",

      friday: "Friday",

    },

  },

  "fr-FR": {

    weekdays: {

      monday: "Lundi",

      tuesday: "Mardi",

      wednesday: "Mercredi",

      thursday: "Jeudi",

      friday: "Vendredi",

    },

  },

  "de-DE": {

    weekdays: {

      monday: "Montag",

      tuesday: "Dienstag",

      wednesday: "Mittwoch",

      thursday: "Donnerstag",

      friday: "Freitag",

    },

  },

};


function getItemFromBoundLanguageMap(item) {

  return this[item];

}


// let weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];

const weekdaysKeyList = Object.keys(i18nMap["en-GB"].weekdays);


const weekdaysEnGB = weekdaysKeyList.map(

  getItemFromBoundLanguageMap,

  i18nMap["en-GB"].weekdays

);

const weekdaysFrFR = weekdaysKeyList.map(

  getItemFromBoundLanguageMap,

  i18nMap["fr-FR"].weekdays

);

const weekdaysDeDE = weekdaysKeyList.map(

  getItemFromBoundLanguageMap,

  i18nMap["de-DE"].weekdays

);


console.log("weekdaysKeyList :", weekdaysKeyList);


console.log("weekdaysEnGB :", weekdaysEnGB);

console.log("weekdaysFrFR :", weekdaysFrFR);

console.log("weekdaysDeDE :", weekdaysDeDE);

.as-console-wrapper { min-height: 100%!important; top: 0; }



查看完整回答
反对 回复 2023-06-09
?
12345678_0001

TA贡献1802条经验 获得超5个赞

您可以执行以下操作:


let arr = ["Mon", "Tue"]

let mapping = {

  "Mon":"aaa",

  "Tue":"sss"

}

newArr = arr.map((str) => {

  return mapping[str];

});

console.log(newArr) //["aaa","sss"]


查看完整回答
反对 回复 2023-06-09
  • 5 回答
  • 0 关注
  • 317 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信