isl*_*hul 7 javascript api json translation
我想用英语将语言添加到当前的API响应中.当前的API响应如下所示:
{
status : "success",
data : {
query: "contains search query",
queryType: "search"
results: [
{
title: "Marvel Captain America T-shirt",
price: 624,
category: "t-shirt",
gender: "men",
},
.....
],
language: "english",
currency: "rupee",
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过两种方式开发API,一种方法是使用不同的内核进行API响应,并根据应用程序中的语言首选项进行调用,但我的应用程序还需要保留某些英语元素.另一种方法是将所有语言放在具有特定结构的响应中以区分每个语言.在任何一种情况下,我都希望保留英语元素.
所以,如果性别:'男人',但语言是法语,所以我想要一个像gender_display这样的额外字段:'hommes'或gender_fr:'hommes'
增加:搜索结果来自Solr
当谈到:[国际化| 全球化| 本地化] 最流行的标准之一是i18n。从编码的角度来看,就是创建一个“资源字典”。
\n\n不同的图书馆已经实施了这一标准,其原则涵盖了货币、日期和数字等内容的翻译和转换。
\n\n例如i18n-js建议使用以下结构:
\n\nI18n.translations = {\n en: {\n hello: "Hello World!" \n },\n "pt-BR": {\n hello: "Ol\xc3\xa1 Mundo!"\n },\n "de": {\n hello: "Hallo Welt!"\n },\n "nb": {\n hello: "Hei Verden!"\n }\n };\n\n I18n.t("hello", {locale: "en"}); //returns "Hello World!"\n I18n.t("hello", {locale: "pt-BR"}); //returns "Ol\xc3\xa1 Mundo!"\n
Run Code Online (Sandbox Code Playgroud)\n\n有一个JQuery 插件也可能有帮助:
\n\n$.i18n.load({ a_key: \'translated string %2$s - %1$s\' });\n$.i18n._(\'a_key\', [\'order\', \'in\'])); //returns \'translated string in - order\'\n
Run Code Online (Sandbox Code Playgroud)\n\n结论
\n\n要定义 JSON 的结构,建议首先选择更适合您需求的框架,并遵循社区的最佳实践。这将使决策过程在社区的经验中得到更多的指导和支持。
\n 归档时间: |
|
查看次数: |
1100 次 |
最近记录: |