我有一个node.js express 4网络应用程序,我需要翻译和显示几种语言。我正在学习在车把中使用i18n-node包。一些本地化的文本数据来自db(动态数据),一些来自使用i18n-node的静态文本json文件。作为参考,我将使用以下句子:“嘿{Londoners}!从{3月29日}开始的下一个春季假期去{Paris},结识一些{parisians}并学习如何做{parisian }甜点?” 我的问题是:您如何处理各种语言的不同语法,这些语法会同时改变静态词和动态注入词的顺序?
一个好的本地化库应该支持这一点,通常,您应该能够配置/传递一些东西:键,值,变量等等(复数,上下文等)。
键在所有不同的语言文件中应保持一致,但是值可以是您想要的任何值。
语言文件:
en.js:
{"Hello {name}, you have {points} points": "Hello {name}, you have {points} points"}
{"HELLO_MSG": "Hello {name}, you have {points} points"}
other-lang.js:
{"Hello {name}, you have {points} points": "{points}, xxx, {name} yyyy yyy"}
{"HELLO_MSG": "{points}, xxx, {name} yyyy yyy"}
然后,在您的代码中,您应该执行以下操作:
en:
var hello1 = translate("Hello {name}, you have {points} points", {name: "John", points: 3}; // Hello John, you have 3 points
var hello2 = translate("HELLO_MSG", {name: "John", points: 3}; // Hello John, you have 3 points
other-lang:
var hello1 = translate("Hello {name}, you have {points} points", {name: "John", points: 3}; // 3, xxx, John yyyy yyy
var hello2 = translate("HELLO_MSG", {name: "John", points: 3}; // 3, xxx, John yyyy yyy
您可以在此处看到一个很好的示例:http : //i18next.com/pages/doc_features.html#interpolation
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句