Unity NGUI Localization

Intro

Currently working on localization, just to share my implementation.

There are some advanced ways doing so as well, e.g. CSV support was added in 3.5.0. But this time I am still working with txt version.

Basic

We will need Unity3D installed and equipped with NGUI 2.7.0+
We will meet these two classes:
Localization is the manager class where we set up localization information, langauges, localized string resource.

UILocalize

Setup

We need to prepare our localized strings, format goes like this: KEY = string

locale-en.txt
play = play
start_game = Start Game
pause = Pause
restart = Restart
options = Options
locale-cn.txt
play = 玩游戏
start_game = 开始游戏 
pause = 暫停 
restart = 重新啟動 
options = 选項

If you want to include leading space in your localized string, you can hack like this:

scores =^    分数

Attach Localization class

Attach the Localization manager to your game, it works as a singleton.

Create a GameObject, attach Localization script to that object.

Setup the locale resource file. In this case, I have 3 locales to work on.

Snippet from Localization.cs
if (mInstance == null)
                {
                    GameObject go = new GameObject("_Localization");
                    DontDestroyOnLoad(go);
                    mInstance = go.AddComponent<Localization>();
                }

It creates a "_Localization" object in your game and stay alive through out the whole game after initilization.

So, we don't need to define it everywhere in the game. (but you still can define it in your separated scene for testing purpose. Note that if a Localization instance is alive, a new one WILLNOT be initialized)

Attach UILocalize

I have a UILabel which displays "Start Game"

I want to translate "Start Game" into a different language.
Let's attach a script and set up the KEY

Assign Language

There are several ways to detect/ pick languages.
NGUI also provides a helper, NGUI LaguageSection. It helps you to create a drop down list for languages.

For iOS game, I detect language using this little script:
Current device language in iOS @ StackOverflow

After you figured out which locale to use, set current in Localization instance.

Localization.instance.currentLanguage = "locale-cn";

Run

Build and run your game.

This also works on UISprite and UIButton.

comments powered by Disqus