私の答えはdevelopingoのものに基づいています。彼の答えはとても素晴らしいです。メッセージ キーコードで口ひげタグを使用する可能性を追加します。現在の口ひげの状態に応じて、またはループでメッセージを取得できるようにしたい場合は、本当に必要です
単純な二重レンダリングに基づいています
info.i18n = function(){
return function(text, render){
var code = render(text); //Render first to get all variable name codes set
var value = i18n.t(code)
return render(value); //then render the messages
}
}
このように、ヒゲは非常に小さな弦で動作するため、パフォーマンスはヒットしません。
ここにちょっとした例があります:
Jsonデータ:
array :
[
{ name : "banana"},
{ name : "cucomber" }
]
口ひげのテンプレート :
{{#array}}
{{#i18n}}description_{{name}}{{/i18n}}
{{/array}}
メッセージ
description_banana = "{{name}} is yellow"
description_cucomber = "{{name}} is green"
結果は次のとおりです。
banana is yellow
cucomber is green
複数形
[編集] : コメントで尋ねられたように、英語とフランス語の複数形処理の擬似コードの例に従います。非常に単純でテストされていない例ですが、ヒントが得られます。
description_banana = "{{#plurable}}a {{name}} is{{/plurable}} green" (Adjectives not getting "s" in plurals)
description_banana = "{{#plurable}}Une {{name}} est verte{{/plurable}}" (Adjectives getting an "s" in plural, so englobing the adjective as well)
info.plurable = function()
{
//Check if needs plural
//Parse each word with a space separation
//Add an s at the end of each word except ones from a map of common exceptions such as "a"=>"/*nothing*/", "is"=>"are" and for french "est"=>"sont", "une" => "des"
//This map/function is specific to each language and should be expanded at need.
}