Search code examples
firebasegoogle-cloud-platformgoogle-cloud-firestorenosql

How to setup / handle a products firestore collection, where product names and descriptions need to be in two different languages i.e. Latin and Greek


I have a Firestore products collection as follows:

+ products
     * dsdrerjESdwe8EWQs
       - name - string
       - description - string
       - photoUrls - Array<string>
       - ...

The name and description need to be in two different languages, English and Greek

What do you think might be the best approach for creating and handling (mainly searching) products? Shall the name and description contain both the English and Greek values in the same field? Or shall the name description types change from a string to an array of strings, with each array element holding the value in a specific language? Any other ideas, suggestions?


Solution

  • If you need to have the name and description in two different languages, English and Greek, then I recommend you to use the following structure:

    db
    |
    --- products (collection)
         |
         --- $productId (document)
               |
               --- name (map)
               |    |
               |    --- en: "English Name"
               |    |
               |    --- gr: "Greek Name"
               |
               --- description (map)
               |    |
               |    --- en: "English Name"
               |    |
               |    --- gr: "Greek Name"
               |
               --- photoUrls  (array)
                    |
                    --- 0: "image.jpg"
                    |
                    --- 1: "image.jpg"
    

    Using such a structure for your document, you'll always be able to get the name and the description in the language that is selected by the user very easily. For instance, if you want to display a product, then you have to perform a get() call, read the document, check the selected language, dig into the name and description map to get the corresponding data, and then display it in the UI.