A simple translation system in Unity

In this article we will look at how to make a text translation system in the Unity editor relatively quickly.

First, we need to install the Localization add-on in Unity. To do this, in the running editor, go to the top menu Windows – Package Manager. In the manager, in the first menu at the top, instead of In Project, select the Unity Registry item, and in the search box, just type Local and the desired add-on will appear. We will install it.

Next, we need to configure our project and add translation folders to it. To do this, go to Edit – Project Settings. Here in the list on the left we will see the Localization item.

Since we will not have any settings yet, click Create and create a folder in the Assets of our project, for example – LocalSettings. After that, in the same window we will add the languages ​​that we will need in our project. To do this, click on Locale Generator and check the languages ​​that we need.

After that, click on Generate Locales. We will be prompted to create a folder for our translations. We create a folder Locales inside our LocalSettings.

Now that we have a sample of our languages, we need to immediately select the language that will be our default. To do this, in the Specific Locale Selector section, in Locale ID, click on the round icon and in the pop-up window select the language that we want to set as default.

We will also set the main language in the Project Locale Identifier item. You can close this window and proceed to creating a translation table.

Go to Window – Asset Manager – Localization Tables. A translation table window will open, where we don’t have anything yet. Click on the New Table Collection button. Since we have a simple option for adding translations, we will change the translation only for text, so below, in the Type item, we need to set the String Talbe Collection parameter. Below, add the name of our table and click Create. We will be prompted to create a folder for this, you can create it in our LocalSettings.

Now we will have a table created where we can add the key for translating strings and the translation text.

Also, you need to click on the button above the translation window and make sure that the Preload All Tables item is checked.

Next, we can add any text object to the scene, remove any text in it. Add the Localize String Event component. In the String Reference item, click and in the pop-up window select the translation key we need.

A little lower, in Update String (String), click on the plus sign, on the left, where we have None (Object), drag our TextMeshPro, and on the right, where there is No Function, select TextMeshProUGUI – text.

At this stage, we can already try to run our game, and see that when switching the language, in the upper right corner of the scene, the translation of our text element will change.

Now let’s switch the language when clicking on certain buttons in the scene, for example, in the settings popup, the language change button.

Let’s create a script, and call it, for example, LocaleSelector, with the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.Localization.Settings;

public class LocaleSelector : MonoBehaviour
{
    private void Start()
    {
        int ID = PlayerPrefs.GetInt("LocaleKey", 0);
        ChangeLocale(ID);
    }

    private bool active = false;

    public void ChangeLocale(int localeID)
    {
        if (active == true)
            return;
        StartCoroutine(SetLocale(localeID));
    }

    private IEnumerator SetLocale(int _localeID)
    {
        active = true;
        yield return LocalizationSettings.InitializationOperation;
        LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[_localeID];
        PlayerPrefs.SetInt("LocaleKey", _localeID);
        active = false;
    }
}

Now, in our scene hierarchy, we will create an empty object and call it LocalizationManager and drag our script into the manager inspector window.

After that, we move on to the button (button element), which will be responsible for enabling a specific translation. Select the button, and add an On Click () handler to the Inspector.

Drag our created LocalizationManager to the object field, and in the function select LocaleSelector – ChangeLocale(int)

Below our LocaleSelector there is an input for entering the ID of the language to which this button will switch. If, for example, you added two languages ​​- English and Ukrainian, and set Ukrainian as the default, then its ID will be 0, and English – 1.

Save everything and run and check the operation of the language switching.

By the way, our script saves the set language and loads the game with the previously selected language, so there is no need to switch all the time.

It should also be noted that in this way you can translate not only text objects, but also button labels, which are also text.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *