如何在Sails.js中转换模型? [英] How to translate models in Sails.js?

查看:95
本文介绍了如何在Sails.js中转换模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的应用程序,其中包含一些需要具有多种语言属性的模型.例如,具有"title"字符串属性的模型"Article"应具有英语和法语的翻译.我知道Sails.js随附I18n节点模块,但这似乎仅处理硬编码的字符串翻译.有没有人有对此经验或示例代码的经验指向我?如果可能的话,我正在这里寻找最佳实践.

I'm working on a simple app with a few models which need to have multilingual attributes. E.g., a model "Article" with a "title" string attribute should have translation for English and French. I'm aware that Sails.js ships with I18n node module, but that seems to handle hardcoded string translations only. Does anyone have any experience with this or sample code to point me to? I'm looking for a best practice here, if possible.

推荐答案

您可以通过2种方式来实现:

You can do it 2 ways:

1.)复制模型中的每种语言的字段,例如:

1.) Duplicate the fields in you model for each language like:

{
  title_en: "string",
  title_fr: "string", 
}

2.)您可以在文章中添加语言"属性(因此您可以选择find().where({lang:'en'}):

2.) You can add a "language"-attribute to your Articles (so you can select find().where({lang: 'en'}):

{
  title: "string",
  lang: {
     type: "string",
     enum: ["en","fr"]
  }
}

选择哪种方式取决于您的用例.

What way to choose depends on your usecase.

这篇关于如何在Sails.js中转换模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆