Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Smart Localization with NGUI

Discussion in 'Scripting' started by Brenden-Frank, Jul 5, 2013.

  1. Brenden-Frank

    Brenden-Frank

    Joined:
    Aug 5, 2012
    Posts:
    110
    Hey guys,

    Just wanted to post a snippet in case it turns out to be helpful for anyone using an alternate localization solution with NGUI.

    I'm currently using Smart Localization which can be found here: http://forum.unity3d.com/threads/173837-RELEASED-Smart-Localization-for-Unity3D

    It's really good, it's free and has some neat features such as Microsoft Translate integration.

    In order to use it with NGUI, simply type your string key into the label field and drag this script on.

    Cheers!

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Globalization;
    5.  
    6. [RequireComponent(typeof(UIWidget))]
    7. [AddComponentMenu("NGUI/UI/LocalizeWidget")]
    8. public class LocalizeWidget : MonoBehaviour
    9. {
    10.     // No public variables, we'll get the key from the widget field
    11.     private string key;
    12.     private string mLanguage;
    13.     private LanguageManager loc;
    14.  
    15.     // Localize the widget on start.
    16.     private void Start()
    17.     {
    18.         // Reference the language manager
    19.         loc = LanguageManager.Instance;
    20.        
    21.         // Hook up a delegate to run the localize script whenever a language was changed
    22.         loc.OnChangeLanguage += new ChangeLanguageEventHandler(Localize);
    23.        
    24.         // Initial localize run
    25.         Localize();
    26.     }
    27.    
    28.     // Incase the script didn't get the message from being inactive
    29.     private void OnEnable()
    30.     {
    31.         if(mLanguage != loc.language)
    32.             Localize();
    33.     }
    34.    
    35.     // Force-localize the widget.
    36.     private void Localize(LanguageManager thisLanguage=null)
    37.     {
    38.         UIWidget w = GetComponent<UIWidget>();
    39.         UILabel lbl = w as UILabel;
    40.         UISprite sp = w as UISprite;
    41.  
    42.         // If no localization key has been specified, use the label's text as the key
    43.         if (lbl != null)
    44.             key = lbl.text;
    45.        
    46.         string val = loc.GetTextValue(key);
    47.        
    48.         if(string.IsNullOrEmpty(val))
    49.             val = "Missing String";
    50.  
    51.         if (lbl != null)
    52.         {
    53.             // If this is a label used by input, we should localize its default value instead
    54.             UIInput input = NGUITools.FindInParents<UIInput>(lbl.gameObject);
    55.            
    56.             if (input != null  input.label == lbl)
    57.                 input.defaultText = val;
    58.             else
    59.                 lbl.text = val;
    60.         }
    61.         else if (sp != null)
    62.         {
    63.             sp.spriteName = val;
    64.             sp.MakePixelPerfect();
    65.         }
    66.        
    67.         // Set this widget's current language
    68.         mLanguage = loc.language;
    69.     }
    70. }
    71.  
     
  2. eric.so

    eric.so

    Joined:
    Jun 19, 2013
    Posts:
    15
    Thanks for the script. But I am getting an exception when using it.

    NullReferenceException: Object reference not set to an instance of an object
    NGUILocalizeWidget.OnEnable () (at Assets/Scripts/API/UI/NGUILocalizeWidget.cs:33)

    I tried to initialize the loc in Awake function but doesn't help.

    mLanguage != loc.language where mLanguage is null.