Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by MajorSquirrel · Feb 09, 2016 at 07:22 PM · guitextupdatelocalizationlanguages

[DIY] Need some help supporting localization on a game and updating UI when a language is selected

Hi there,

This is sort of the continuation of a question I posted yesterday. I am aware that many assets exist in the Unity Assets Store, but I would like to do it myself for knowledge sake and "just do it, don't let your dreams be dreams".

I would like to support localization in my game, and I was mainly inspired by the Android localization format, meaning storing my strings into .xml files and getting them using a Language Loader :

 using CustomDictionary = System.Collections.Generic.Dictionary<string, string>;
 using System;
 using System.Collections;
 using System.IO;
 using System.Xml;
 
 public class                    LanguageLoader {
 
     private CustomDictionary    Strings;
     private string              DefaultString;
 
     public                      LanguageLoader() {
         this.Strings = new CustomDictionary();
         this.DefaultString = null;
     }
 
     public void                 Clear() {
         this.Strings.Clear();
     }
 
     public void                 Load(string path, string countryCode) {
         XmlDocument             xmlDocument;
         XmlElement              rootElement, element;
         IEnumerator             elementEnumerator;
 
         xmlDocument = new XmlDocument();
         try {
             xmlDocument.Load(Path.Combine(path, this.GetXmlLanguageFile(countryCode)));
             rootElement = xmlDocument.DocumentElement;
             if (rootElement == null || !("language".Equals(rootElement.Name)))
                 throw (new XmlException("Invalid root element !"));
             if (!rootElement.GetAttribute("code").Equals(countryCode))
                 throw (new XmlException("Country codes are different !"));
             elementEnumerator = rootElement.GetEnumerator();
             while (elementEnumerator.MoveNext()) {
                 element = (XmlElement) elementEnumerator.Current;
                 if (element.GetAttribute("name") == null || element.InnerText == null)
                     throw (new XmlException("Invalid string !"));
                 this.Strings.Add(element.GetAttribute("name"), element.InnerText);
             }
         } catch (XmlException exc) {
             UnityEngine.Debug.LogException(exc);
         }
 
     }
 
     public void                 SetDefaultString(string defaultString) {
         this.DefaultString = defaultString;
     }
 
     public string               GetXmlLanguageFile(string countryCode) {
         return (String.Concat(countryCode.Trim(), ".xml"));
     }
 
     public string               GetString(string key) {
         string                  returnedString = this.DefaultString;
 
         if (this.Strings.ContainsKey(key))
             returnedString = this.Strings[key];
         return (returnedString);
     }
 }

The .xml language file is well stored and I am able to get a string without any problem. My problem is that I don't know how to update my UI correctly when a language is selected by the user. For example, I have a main menu which contains four buttons (see the link above for a visual approach) and these four buttons have to be translated using localization. For this, I made a Menu Manager which contains a function that translates the buttons at start, but I find it really gross :

     public void             InitializeMainMenu() {
         this.MainMenu.transform.Find("Character Demo Button").GetChild(0).gameObject.GetComponent<Text>().text = this.Localization.GetString("text_playy_character_demo");
         this.MainMenu.transform.Find("Turn-based Demo Button").GetChild(0).gameObject.GetComponent<Text>().text = this.Localization.GetString("text_play_turnbased_demo");
         this.MainMenu.transform.Find("Languages Button").GetChild(0).gameObject.GetComponent<Text>().text = this.Localization.GetString("text_languages_menu");
         this.MainMenu.transform.Find("Quit Button").GetChild(0).gameObject.GetComponent<Text>().text = this.Localization.GetString("text_quit_game");
         this.MainMenu.SetActive(true);
     }

The further problem is when the user will select a language : is there any Unity function that updates the GUI once ? I heard about OnGUI() but I feel like this is useless to repeat this code every frame.

I apologize if I can't be more explicit and I'll try to give you more details as the discussion goes.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MajorSquirrel · Feb 10, 2016 at 01:12 PM

Coming back with some good news !

It might help someone in the future so I'll post my way of doing it. I think I'll go for custom events : when a language will be selected by the user, an event will be triggered and each gameobject listening to this event will update their text UI. :)

The tutorial I linked above is made for Unity 4.x, apparently there is a fresh custom event system (here and there) but I prefer the old one.

Hope it'll help someone. C:

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

53 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Display rigidbody speed to a world space canvas text 2 Answers

Built game on Android has GUI text pink, and slider menu has disappeared. 0 Answers

Localization Playerprefs remember language on startup 1 Answer

How to add text to a box ? 2 Answers

Removing an item from Inventory and updating item count on HUD 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges