node.js - 带有 mongo/node 的多语言界面和内容

标签 node.js mongodb schema data-modeling database

我们正在开发一个新的应用程序,它将以 saas 模型在线提供服务。用户将可以访问某些工具和一些关于如何使用它的教程。

我的问题是什么是使(界面和内容)多语言化的最佳方法。举个例子——想象一下带有以下链接的简单导航:

-section 1
|-page a
|-page b
-section 2
-section 3
|-page c

每个页面显然都包含某些标签、标题、按钮等。

我已经搜索了一段时间,我找到的最接近的答案在这里: Schema for a multilanguage database 但是,它描述了有关关系数据库的方法。

在研究了其他问题之后,看起来最好的方法是将部分/页面/标签/标题/按钮的每个名称存储为一个带有 ID 的对象,将翻译保存在一个单独的表中,包括对象的相应 ID、语言和内容。在 SQL 世界中,简单的连接就可以完成这项工作,但是由于我们使用的是 Mongo,我想有更好的方法可以做到这一点。

最佳答案

我建议将所有翻译存储在您的 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 Queries

----> Mongoose Populate

Express

附言:Here是一个 github 项目,解释了如何从头开始一个 web/node 项目(安装工具等)[Babel、Gulp、Webpack、ES6...]

关于node.js - 带有 mongo/node 的多语言界面和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40424163/

相关文章:

javascript - 测试 API 如何使用 node.js 处理无效的 JSON 语法请求正文

node.js - 使用mocha调试NodeJs程序时出错

javascript - 替换数组字段值

java - 针对 XSD 的 XML 验证 : cvc-complex-type. 2.4.a

sql - 在没有 pg_lang 条目的情况下在 Postgresql 中查找存储过程语言的策略

node.js - npm install 似乎没有获得所有依赖项

node.js - yarn 使用错误: how to use yarn on git bash

mongodb - 按正则表达式搜索字段名称

Mongodb:由对等方重置连接

MySQL information_schema.columns 额外列选项