How to translate models in Sails.js?

2020-07-30 04:00发布

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.

1条回答
再贱就再见
2楼-- · 2020-07-30 04:33

You can do it 2 ways:

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

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

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.

查看更多
登录 后发表回答