我们有一个刺痛I want to by {number} apples
。我们有接口:
支持条件的最佳方法是什么:
apples
可以有复数形式如何在JavaScript项目中解决这个问题?
答案 0 :(得分:1)
你应该创建一个数据结构来存储所有这些短语,并创建一个根据条件选择它们的功能(不是完美的解决方案,也不是完美的javascript,但可能会给你一个想法):
var translations =
{
"English":{
"I want to by %n apples":[
[0, "%n apples"],
[1, "%n apple"],
[2, "%n apples"]
]
}
},
{
"American":{
"I want to by %n apples":[
[0, "%n apples, dude"],
[1, "%n apple, dude"],
[2, "%n apples, dude"]
]
}
};
var languageToUse = "American";
var stringToReplace = "I want to by %n apples";
var numberOfItems = 1;
var newEnding = translations[languageToUse][stringToReplace][numberOfItems];
var res = stringToReplace.replace("%n apples", newEnding);
// res = "I want to by %n apple, dude"
http://i18njs.com/有很好的例子如何做到这一点。