Skip to content

Localization System

Yue Wu edited this page May 6, 2021 · 6 revisions

Overview

PwC will be selling Bodylogical to other countries (currently aiming for Japan), so a localization system is required to store, display and switch to different languages on the fly. We wish to have the language text as separate files such that PwC can make changes without opening Unity.

All localization scripts are located in the localization directory. Also, there is the Localization Manager that does overall control. The actual localization files are located at Assets/Resources/Localizations.

File Format

The localization data files are XML files. I initially chose this format over JSON because XML supports comments, which is useful in determining what a specific field is about.

The newest version of Json.NET (which is used to handle server communication) seems to have added support for comments in JSON. If that is the case, it might be a good idea to use json for localizations, as it is more concise and human-readable.

The files will be parsed into Localization objects. Fortunately, C# has official support for parsing XML files into classes. There are many inner classes inside the script for the dirty parsing job; if you are interested in how it is done, just search for the different methods and attributes ([XmlRoot], etc.).

A Localization object consists of several sections, each section represented as a Dict.

Params and Groups

Sometimes we want to insert some text inside a localized text. For example, when we want to display age, we might have:

Age: 25

年齢:25

Notice that the parameter (age) is just a number; it does not need to be localized.

In some other cases, we might want to localize the parameters as well. And there might be more than one parameters, some of which may be in different order. for example, when we want to display the date.

5/2025

2025年5月

Localized Param and Localized Group are used to resolve this issue. A Localized Param is a parameter for a localized text; it contains a boolean to specify if this parameter is localized as well. A Localized Group consists of an id and a list of Localized Params.

The id format for each key is [section].[field]. Sections are only for readability, but serve no functional purpose.

Localized Text and Localization Manager

We are about to see the last piece of the puzzle -- Localized Text and Localization Manager.

The Localized Text is basically a Localized Group. However, it is a MonoBehaviour and should be attached to a Text game object. When you need to set the localized text, use SetText(). The script will hold the parameters passed in in case the user switches language midway through the app (if we don't store the parameters, the script doesn't know what id to look up in the Localization dictionary!). You can use Clear() to clear the text.

The Localization Manager is the controller of everything above. When the app starts up, it will load the localization files, find all LocalizedText instances, and set up the texts. There is also ChangeLanguage() method that would switch among different languages, and FormatString() that would give out the actual string given the id and parameters.

Clone this wiki locally