Multilanguage for projects using JSON and Dictionary

7

Stats

1,036 visits, 1,375 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 27 Jan, 2022.

Hello guys, this is my first tutorial and I'm going to show how you can setup a multi language project using JSON and Dictionary.

First of all, lets setup our basic project.

1) Create a new empty project and add the follow objects:

AJAX, JSON and Dictionary.

- AJAX is responsible for loading our translation file;

- JSON is responsible for receiving from AJAX the data of our translation file;

- Dictionary is responsible to store all our translations to not depend on JSON object as you may need it for other purposes;

2) Lets create our JSON file:

Right click on FIles -> NEW -> Json

Now we have our basic setup done.

Let's understand how the json file will store the translations.

Double click on your translations.json file and it will open your file with some example data.

You must be seeing:

{
	"foo": {
		"bar": 42
	}
}

We are going to store our translations based on Layouts, its easier to understand and also easier when you need to find and entry on your file.

For our file the structure is going to be:

{
	"Layout_Name": {
		"Object_Reference": {
			"lang_abreviation": "text",
			"lang_abreviation": "text"
		}
		{
		"Object_Reference": {
			"lang_abreviation": "text",
			"lang_abreviation": "text"
		}
	}
	"Layout_Name": {
		"Object_Reference": {
			"lang_abreviation": "text",
			"lang_abreviation": "text"
		}
	}
}

3) Our first layour is going to be an intro Layout, so lets rename it to intro.

4) We need a text object on our layout so lets create it and drop it on our Intro Layout.

5) Lets create and instance variable on our Text Object to use as reference name of what is going to show. In this case I am using: language_reference as instance name and "welcome" as initial value.

6) We need to setup a default language for our project, to store this value I am using a global variable, so lets create it:

7) Lets fill our file with some data, here is mine:

8) Lets load our file using Ajax and on loaded lets parse it to the Json Object:

We already have a structure that could work using directly Ajax and Json but to let the Json object free for others purposes lets store our data on a Dictionary.

9) Lets use some fors to convert our Json into dictionary entries:

Lets run it on debug mode and see if the dictionary is receiving the keys correctly, if yes you should see the follow:

Now that we already have our dictionary, we need to fill our text object.

10) Lets create and action that is going to pick all text objects and fill it by Layout Name + language_reference variable + default language variable.

Now you can test it, the first layout will show Welcome on your text object.

To change the language simple change the default_language variable on your project and call the same action on item 10. Thats it.

Here is the entire event sheet:

Thanks.

  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!