How to Translate Your Game Using the Unity Translation Package

Welcome to the wild world of localization, where Veggie Gladiators are about to prove that vegetables aren’t just for salads, but for speaking multiple languages too! Picture this: your game is a hit in Tokyo, and suddenly the Veggie Gladiators have to fight in a sushi bar. Without localization, they’ll be lost in translation faster than a potato in a fruit salad. So, let’s arm our Veggie Gladiators with languages and take over the global gaming arena, one veggie at a time! By Ben MacKinnon.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

Building the Menu

Before you add a new button to the menu, you should also add a translation for the New Game button. Can you repeat the previous steps for the CanvasPanelNew Game ButtonText (TMP) GameObject? The translation you need is Neues Spiel. If you get stuck, you can open the spoiler below for some help.

[spoiler title = “Solution”]

  1. Click the component menu for the TextMeshPro component and select Localize.
  2. Select the UI Strings table on the Table Collection dropdown on the new Localize String Event component.
  3. Click Add Table Entry.
  4. Update the values for the new entry:
    1. Entry Name: New Game
    2. English(en): New Game
    3. German(de): Neues Spiel
  1. Entry Name: New Game
  2. English(en): New Game
  3. German(de): Neues Spiel

New Game
[/spoiler]

Once you have the New Game button translated, duplicate the button by selecting it in the hierarchy and either right-clickDuplicate or press Control-D. Name the new button Menu. Change the Y position of the button to 60 so that it sits just below the New Game button.

Menu Button

Select the Text (TMP) child of the Menu button. Update the string in the TextMeshPro component to read Menu.

Once more, update the Localization String Event by adding another Table Entry. Name this one Menu with the German translation being Menü.

Menu Translation

Next, you’ll use this button to present a new panel with options for the user to change language.

Adding a Translation Option Menu

The translation options will be a new panel that’s presented in front of the title menu. When you’re done, it should look like this:

LanguageOptionsMenu

Select the Canvas gameObject in the hierarchy, and add a new GameObjectUIPanel.

New Panel

Name it OptionsPanel and set the color of the Image to 36, 36, 36, 221. This gives it a dark but slightly transparent background.

PanelOptions

Select the Menu Button from earlier, and set its OnClick event to set the OptionsPanel GameObject.SetActive to true.

Button Event for OptionsPanel

How the panel is laid out isn’t important, so feel free to make it look how you like. But for a speedy menu, select the Title gameObject and the two buttons from the original menu. Duplicate them, and drag the new objects to be children of the new OptionsPanel.

Rename the two buttons English and German. Update their text values to English and Deutsch and remove any LocalizeStringEvent components – you don’t want to translate these buttons!

Finally, to add translations to the new title, open up the Localization table, if you don’t still have it open, by selecting WindowAsset ManagementLocalization Tables. Click Add New Entry to create an entry with the Key value Choose Language. Set the English value to be the same and the German value to Sprache wählen.

New Table Entry

Select the OptionsPanelTitle object once more, and in the Localization String Event component, change the String Reference to this new key. You should also update the default value for the TextMeshPro component to Choose Language.

Selecting a new key

Now that you have some buttons for the different languages, it’s time to make them work!

Setting Up Localization Manager

In the RWLocalization folder, create a new script called LocalizationManager. Once Unity recompiles, add an empty GameObject to the scene, also named LocalizationManager, and add the new script to the gameObject.

Now it’s time for a little scripting! Open the LocalizationManager script in your preferred code editor.

Localization Settings

Remember when I mentioned at the start that Unity saves the translation settings as a serialized asset? Well, that opens up being able to read and modify those settings from code! To change the language of the game, you only need one method, and the use of a LINQ query.

At the top of the LocalizationManager file, add the following using statements:

using System.Linq;  
using UnityEngine;  
using UnityEngine.Localization.Settings;

Then, in the body of the class, add the following method:

public void SetLocale(string code)  
{  
    // 1
    var localeQuery = (from locale in LocalizationSettings.AvailableLocales.Locales  
        where locale.Identifier.Code == code 
        select locale).FirstOrDefault();  

    // 2
    if (localeQuery == null)  
    {        
        Debug.LogError($"No locale for {code} found");  
        return;  
    }    

    // 3
    LocalizationSettings.SelectedLocale = localeQuery;  
}

Here’s what the code does:

  1. This line is a LINQ query. It’s searching all the locales you set up earlier and selecting the first one it finds that matches the code passed to the method.
  2. This is a safety check to ensure you found a valid locale.
  3. Finally, you update the SelectedLocale in the LocalizationSettings to the one found by the query.

Save the script, and go back to the Unity editor.

Connecting Everything

Select the English button and replace the calls in On Click. First, you should drag LocalizationManager to the Object and select LocalizationManagerSetLocale (string) as the call. Fill in the variable field with en – the code for the English Locale that you set at the beginning of the tutorial.

Locale Codes

Second, add a reference to turn off the OptionsPanel gameObject. It should end up looking like this:

English Language Button

Finally, do the same with the German button, giving it the code de for the SetLocale method.

Turn the OptionsPanel gameObject off in the inspector, save the scene and click Play to try out the new menu options. You should be able to click Menu and then change the language between English and German.

Testing the Language Menu

You’ll notice that the dropdown you used to test the different language earlier also updates now when you use your UI to change the language.

Phew, that was a lot of work just to get a few buttons and title strings to update when changing language. But remember, there’s a whole host of dialogue strings to translate when the game actually starts!

Fortunately, there are some more tricks to use to mass import translation strings, and just a few edits to the existing code will have our Vegetables speaking German in no time!

Finding Translations

When working on a full production game supporting many languages, it isn’t feasible to manually step through every potential key and hand translate them all, unless you’re an extremely patient linguist! The usual approach for getting translations into your game would be to define all the keys you need and the source string. Then, you’d send your list of keys and sources off to a translator to provide all the translated strings back to reimport into your project.

Fortunately, the Unity Localization package supports a number of ways to export and import localized strings to your project. When dealing with an external or professional service, you’re likely to have your translation files hosted on the cloud with a service such as localizely or a self-hosted platform such as Weblate. These platforms can import and export files in formats such as CSV (Comma Separated Value) or XLIFF – an XML-based format that was created to be a standard for passing localizable data between applications.

In Unity, once you have created a Localization Table Collection like you did earlier, you can import or export that table of keys to either CSV or XLIFF from the component menu.

Table Collection Export

Of course, professional translation services don’t come free! If you’re just looking to get started and want a quick-win support solution, Unity Localization supports a third import method in the form of Google Sheets. The setup may be a little more complex, but the results can be instant.

Contributors

Ben MacKinnon

Author and Topics Master

Gijs Bannenberg

Tech Editor

Adriana Kutenko

Illustrator

Toby Flint

Final Pass Editor

Over 300 content creators. Join our team.