简体   繁体   中英

How does a reliable internationalization approach look like which is capable of mapping one or many words from one language to another?

I have an array of week days ["Monday","Tuesday"..etc] and i need to translate each day to another language.

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

translatedArray = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi'];

What's the best approach to this? I need to output a new array with translated array

My simple way is that maintain a json in an object like this

let weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
let anotherLanguageDays = {
   "Monday" : "星期一",
    "Tuesday" : "星期一",
    ... and so on
};

and now when you iterate over the array you can do something like this

weekdays.forEach(d => {
   console.log(anotherLanguageDays[d]);
})

Complete example of your code...

 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]); })

You can do the following:

let arr = ["Mon", "Tue"]
let mapping = {
  "Mon":"aaa",
  "Tue":"sss"
}
newArr = arr.map((str) => {
  return mapping[str];
});
console.log(newArr) //["aaa","sss"]

You can use Array reduce method to make the result object.

 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"]);

It mainly boils down to choosing the most convenient map ping approach. One possible approach might look similar to the next provided example code ...

 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; }

Create an object with keys that represent languages codes. Each language codes have another object with the words and the translation of the words in that language.

Write a function which accepts an array of strings as the words that you'd like to translate and a language code that indicates which language to get the translations from.

Make sure to make all your keys lowercased for uniformity. The values don't have be lowercased.

Following this method you can easily scale up by adding more languages and words to use.

 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM