Author Topic: Localization system  (Read 50390 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Localization system
« on: February 14, 2014, 04:45:59 AM »
Overview

Localizing your game using NGUI is as simple as creating a CSV file with all your text and using the UILocalize component on your labels and sprites that you want to be localized.

NOTE: The CSV support was added in 3.5.0



The first step is creating the CSV file. You can use Google Docs, Excel, or any other application that's capable of creating excel style files.

The CSV file should contain two or more columns. The first column should contain keys that you will use in the Key field of the UILocalize component. Other columns should contain the actual localized values for those keys.

The first row should contain your language names -- English, French, Simplified Chinese, etc. When you want to switch a language in-game you can either use Localization.language = "English;" (or any other language defined in the first row), or just attach the LanguageSelection component to a drop-down list, turning it into a language selection list.

All other rows should contain the key in the first column and text values in all the subsequent columns. Just make sure that there are localized values present for all the given keys, or things may not work properly.

Once you're done, export the file in CSV file format as Localization.csv, and place it in the Resources folder. Unity 4 treats CSV files as Text files, but if you are still on Unity 3, you will need to rename that file to have a TXT extension or it won't be recognized.

To localize any label or sprite, attach UILocalize script to it and choose a key defined in the CSV file you created, and that's it! The label or sprite's value will be pulled from the localization file automatically, and when you switch languages, so will your label or sprite.

Pro-Tip #1

If you don't remember what your key was, just start typing in the UILocalize's "key" field. It will bring up a list of relevant entries for your convenience. Click on one of them to choose that value. Likewise you can click on the Preview options to have the localized object immediately assume that value.

Pro-Tip #2

If you are doing custom localization, you can still use the localization system. Use Localization.Get(key) anywhere in code to retrieve a localized value for the current language. To change the language -- Localization.language = "New Language";

Pro-Tip #3

Want even better localization tools? Have a look here -- http://www.tasharen.com/forum/index.php?topic=8324.0

Class Documentation

http://tasharen.com/ngui/docs/class_u_i_localize.html
http://tasharen.com/ngui/docs/class_localization.html

If you have a question regarding this component or would like me to clarify something, just post a reply here.
« Last Edit: March 05, 2014, 06:04:52 AM by ArenMook »

nkls

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Localization system
« Reply #1 on: February 21, 2014, 03:34:53 AM »
Great update. The preview and the support of csv´s in 3.5 are awsome. 
Got one question. I´ve managed to read my localization from a csv (saved from google spreadsheet) , but I cant get the line break to work on my localized labels  :/
I´ve tried \n , but doesnt seems to work.. Any advice on this?
I

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: Localization system
« Reply #2 on: February 21, 2014, 11:39:37 AM »
Open ByteReader.cs, and add this line after line 180:
  1.                         string line = ReadLine();
  2.                         if (line == null) return null;
  3.                         line = line.Replace("\\n", "\n"); // <-- this one

SketchWork

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 44
    • View Profile
Re: Localization system
« Reply #3 on: February 21, 2014, 11:54:28 AM »
CSV localization is much better - thanks for adding this.  One question though.

How expensive is calling UILocalize on every label, button, list, etc on mobile?

dxlong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Localization system
« Reply #4 on: February 22, 2014, 03:25:28 AM »
Use Localization.Get(key) in code can't get Chinese character!  :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: Localization system
« Reply #5 on: February 22, 2014, 11:24:09 AM »
It can if your code file is saved with proper encoding.

ZachHoefler

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 13
    • View Profile
Re: Localization system
« Reply #6 on: February 26, 2014, 02:05:17 PM »
I was having trouble with this until I realized NGUI was loading NGUI/Resources/Localization.csv instead of my Resources/Localization.csv file =] Deleting the NGUI example file fixed the issue for me.

Figured I'd post here in case anyone else was having the same problem.

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: Localization system
« Reply #7 on: February 26, 2014, 08:54:29 PM »
I hope you don't get rid of the old key = value formatting because it works for some projects better than having all of the languages in the same file. If you're like us and have a potentially gigantic string table and don't want to keep all of the strings in memory for every language. Could you try to include the mechanics for loading the old style of languages as well in the documentation? You can delete this post after you do.

If you want to use the old localization format. You can having a .txt file in the resources directory that is the same name as you set to Localization.language. So if you have a English_US.txt file located in the Resources directory you can load this string table by setting the language: Localization.language = "English.US";

ProTip: If you don't want to put the language at the root level of the Resources directory you can set the language to be the filepath to the resource such as: Localization.language = "Strings/English_US";

Bradamante3D

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 79
    • View Profile
Re: Localization system
« Reply #8 on: February 27, 2014, 06:38:01 PM »
It also seems the Localization in the UI Starter Kit: Starlink is broken. It complains with Localization.cs, line 408 (Localization key not found: "Quit") for any key you try.

Does that mean that the UI Starlink Kit is not compatible with new versions of NGUI or does it simply not work out of the box, but it would with some manual adjustments?
#301224014, #301432336, #302399130

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: Localization system
« Reply #9 on: February 27, 2014, 06:54:23 PM »
It means I totally forgot to update it. My bad, I'll do that tonight.

rptts

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Localization system
« Reply #10 on: March 03, 2014, 02:16:49 PM »
Hello,

I'm having some trouble making my localization system to work properly. Maybe I'm doing something wrong, but here's what I'm doing:

(I'm using unity 3.5)
In the Awake of my app i do:

  1. TextAsset languages = Resources.Load("myLanguages", typeof(TextAsset)) as TextAsset;
  2.                        
  3. Localization.LoadCSV(languages);
  4.  
  5. Localization.language = Localization.knownLanguages[0];
  6.  

but every time I instantiate a new UIPopupList (when i load a new scene, for example), it seems that the Localization forgets the dictionary because it no longer recognizes the keys. I get the warning from Localization.cs, line 408 (Debug.LogWarning("Localization key not found: '" + key + "'");)
Am I doing something wrong here?

Another thing that I would like to clarify is how can I change a localized string ("my string number {0}") when I change the Language dynamically (for example using UIPopupList). Before this last update I was updating it on the OnLocalize function, using the
  1. label.text = string.Format(label.text, 1);
method, but now it seems it "localizes" the label after the OnLocalize function, because although when I output the label to the console, the string is correct, but in the game interface it shows the original string.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: Localization system
« Reply #11 on: March 04, 2014, 12:31:18 AM »
Don't do any coding. Simply call your CSV file "Localization.txt". It needs to have the "TXT" extension for Unity 3.5. NGUI will load this file from resources automatically. Also make sure that you don't already have Localization.txt from NGUI's examples. It will conflict with your own.

rptts

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Localization system
« Reply #12 on: March 04, 2014, 07:33:06 AM »
I've made the pretty noob mistake of saving my settings prefab while I was playing the app and saved the UIPopupList with the onChange Event to notify the LanguageSelection delegade and OnChange methods. That was causing the problem, after removing them it's now working properly.

Anyway, sometimes simplifying things makes us realize easier where our problems are, tnx  :D

syslord2k2

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Localization system
« Reply #13 on: March 10, 2014, 11:57:20 AM »
I tried to use this system but it didn't work at all. After doing some logging i found out that it would only ever read the first line and nothing else. I attached the used Localization.csv and hope that somebody can help me.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: Localization system
« Reply #14 on: March 10, 2014, 09:31:19 PM »
That's not a CSV file. CSV = Comma Separated Values. Your values are separated by a semicolon.