Skip to main content

Translate multilingual JSON - Tutorial

This page is a tutorial for configuring multilingual JSON files.

Prerequisites

This tutorial uses JSON configurations with:

  • Make sure to choose “Version 2” in the configuration screen.

  • Choose the multilingual option.

Useful links for understanding JSON paths are:

Example 1 - Translate all content

Let’s start with the simple example below. Our objective is to translate the English content and then insert translation back with the respective language code.

In the example below “en” contains the source texts and “fr”, “es” and “zh” shall be populated with the French, Spanish and Chinese translations once these are done.

CODE
{
	"en": {
		"text": "This is an example",
		"title": "A title to translate"
	},
	"fr": {
		"text": Ceci est un exemple"
	}
}

Then we will translate the texts into French and Spanish. Note that the original file already contains one French translation, it will be read and used as a pre-translation.

When we later build the translated file we expect it to contain all the translations:

CODE
{
	"en": {
		"text": "This is an example",
		"title": "A title to translate"
	},
	"fr": {
		"text": "Ceci est un exemple",
		"title": "Un titre à traduire"
	},
	"es": {
		"text": "(The Spanish translation)",
		"title": "(The Spanish translation)"
	}
}

The configuration to use is:

The JSON path is # which is replaced by the language code. Like with monolingual configurations it will be replaced by en, fr, es and other project languages you translate. The second option says that # will be substituted by the source language code and each of the target language codes.

Example 2 - Customizing language codes

The previous example was very simple. Luckily, the JSON file contained the exact language codes we are using in our Wordbee project.

Obviously, this is not always the case in the real-world!

CODE
{
	"English": {
		"text": "This is an example",
		"title": "A title to translate"
	},
	"French": {
		"text": Ceci est un exemple"
	}
}

The configuration needs to specify how Wordbee project locales are “mapped” to the language titles in the JSON file:

If you tick the option below the box then any project locales that are not listed will be mapped unchanged. In our example we do not want this.

Mapping language titles in a json file to project locales is done using the syntax:

{valid project locale} : "{name of node in json}"

For example: en-GB: "English Great Britain", fr: "French", es: "Spanish"

The locale must be a valid locale (ISO) in Wordbee Translator.

Example 3 - Translate array elements with language indicator

Check out the file below and how the language is saved in a property within array elements:

CODE
[
  {
    "language": "en-US",
    "text": "This is an example",
    "description": "My description"
  },
  {
    "language": "en-US",
    "text": "This is another example",
    "description": "Another description"
  },
]

If we translate this into French, we want that result:

CODE
[
  {
    "language": "en-US",
    "text": "This is an example",
    "description": "My description"
  },
  {
    "language": "en-US",
    "text": "This is another example",
    "description": "Another description"
  },
  {
    "language": "fr-FR",
    "text": "Ceci est un exemple",
    "description": "Ma description"
  },
  {
    "language": "fr-FR",
    "text": "Un autre exemple",
    "description": "Une autre description"
  }
]

The configuration will look like this:

How it works:

  • The “language” node will contain the language as per the JSON path and the '#' placeholder.

    • # is replaced by the source or target language codes in the Wordbee project.

  • The JSON path selects all array elements by language. It uses a JSON path condition: [?( … condition …)]

  • When the filter looks for the English content, it selects the array elements using [?(@.language == ‘en-US')] - assuming that this is the file’s source language.

    • Translations are inserted back by cloning each source array elements.

Important (!!):

  • You also need to add “language” to the list of “Translate nodes”. Otherwise, the system will not localize the language-nodes and save back the target locales.

    • If you tick the option to translate all child nodes then you do not think about this detail.

Example 4 - Translate array elements with language indicator, no 2

Let’s push the envelope further. Our latest file uses custom language names instead of locales. And we only want to translate the “text” properties and nothing else:

CODE
{
  "products": [
    {
      "language": "English",
      "id": 1002,
      "text": "This is an example",
      "notranslate": "Something not to translate"
    },
    ...
  ]
}

The configuration now is $.products[….]. The array elements are indeed located under the products property. With JSON paths, the $ sign is the root of the document and writing $.products locates the product node at the root. We further instructed to only translate the “text” properties but not any of the other ones.

Important (!!):

  • You also need to add “language” to the list of “Translate nodes”. Otherwise, the system will not localize the language-nodes and save back the target locales.

    • If you tick the option to translate all child nodes then you do not think about this detail.

When the system inserts back the translations, it will clone the source language node or array element for each target language. If the target objects/elements already exist fully or partially in the source file, the system will keep any existing properties and replace only those that we are translating.

Example 5 - Arrays in arrays in arrays

All the examples up to here are simple and you may come across more advanced files.

Keep in mind that you can configure multiple JSON paths at any time. The example below translates array based content (first selector below) and object based content (second selector below).

It is even possible to translate multilingual content containing multilingual content - if anyone ever wants to do it!

CODE
{
  "products": [
    {
      "language": "English",
      "text": "This is an example",
      "features": {
          "English": "English feature",
          "French": "French translation",
          "Spanish": "Spanish translation"
          }
    }
    {
      "language": "French",
      "text": "Ceci est un exemple",
    }
  ]
}

Example 6 - Reading meta data

Please check out the meta-data examples we explain with monolingual files.

The mechanism and configuration options are the same.

Final word

We only touched on some of the possibilities of the JSON filter. For more advanced files check out the JSON path links printed in the top.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.