3

We are developing a new app which will be served online in saas model. User will get access to certain tools and some tutorials on how to use it.

My question is what will be the best approach to make both (interface and content) multilingual. To give you an example - imagine simple navigation with following links:

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

Each page includes obviously certain labels, titles, buttons etc.

I've been searching for a while and the closest answer I found was here: Schema for a multilanguage database however, it describes approach regarding a relational database.

After studying other questions it look like the best approach would be to store each name of section/page/label/title/button as an object with ID keep translation in a separate table including the corresponding ID of the object, language and content. Within SQL world simple join would do the work, however since we are using Mongo I guess there is the better way to do that so.

Community
  • 1
  • 1
Dawid Adach
  • 749
  • 5
  • 27

1 Answers1

1

I would propose to store all translations in your mongoDb database, and then, using an Express API, perform request from the front page about the translations.

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

[FRONT] <---------- [Translations] <---- [EXPRESS API] <=== [MONGOOSE]

Mongoose Schema

       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',
                }],
        });

Example of data (MongoDB Document)

       [{
             _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: [],
       }]

Example of Request: Retrieve all data about one random section

   Content.find({
       contentType: 'section',
   })
   .then((ret) => {
       if (!ret) return [];

       // Here recursively get childs and childs and childs
       // until you got everything
   })
   .then(() => {})
   .catch(err => {});

Tools

Mongoose

----> Mongoose Queries

----> Mongoose Populate

Express

PS: Here is a github project that explains how to start a web/node project from scratch (tools to install ...etc) [Babel, Gulp, Webpack, ES6...]

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69