多语言界面和mongo /节点的内容

我们正在开发一个新的应用程序,将在网上saas模式。 用户将可以访问某些工具和一些教程如何使用它。

我的问题是什么是使多种语言(界面和内容)的最佳方法。 给你一个例子 – 想象下面的链接简单的导航:

– 第一节
| -page a
| – 页面b
– 第2节
– 第3节
| – 页面

每个页面都包含明显的标签,标题,button等

我一直在寻找一段时间,我发现最接近的答案在这里: Schema为多语言数据库,但是,它描述了关于关系数据库的方法。

在研究其他问题后,看起来最好的方法是将每个section / page / label / title / button的名称作为一个ID保存在一个单独的表中,包括对象的ID,语言和内容。 在SQL世界里,简单的连接可以完成这个工作,但是因为我们使用的是Mongo,所以我猜这样做有更好的方法。

我build议将所有翻译存储在您的mongoDb数据库中,然后使用Express API从头版执行关于翻译的请求。

[FRONT] ----> [Send mongoDB _id(s)] ---> [EXPRESS API] ===> [MONGOOSE] [FRONT] <---------- [Translations] <---- [EXPRESS API] <=== [MONGOOSE] 

mongoose纲要

  const Content = Schema({ // Type of item you are dealing with contentType: { type: String, enum: ['link', 'title', 'section', 'page'], }, // Value of translations translations: [{ languageName: String, value: String, }], // All Contents that are inside childs: [{ type: Schema.Types.ObjectId, ref: 'Content', }], }); 

数据示例(MongoDB文档)

  [{ _id: '00000000000000000000001', contentType: 'section', translations: [{ languageName: 'French', value: 'Section Enfant', }, { languageName: 'English', value: 'Child Section', }], dads: ['00000000000000000000002'], }, { _id: '00000000000000000000002', contentType: 'page', translations: [{ languageName: 'French', value: 'Page Baguette', }, { languageName: 'English', value: 'Tee Page', }], childs: [], }] 

请求示例:检索关于一个随机部分的所有数据

  Content.find({ contentType: 'section', }) .then((ret) => { if (!ret) return []; // Here recursively get childs and childs and childs // until you got everything }) .then(() => {}) .catch(err => {}); 

工具

mongoose

—-> mongoose查询

—-> mongoose填充

performance

PS : 这是一个github项目,解释了如何从头开始一个web / node项目(工具来安装…等) [巴别卡,Gulp,Webpack,ES6 …]