Localize your Unity Game

In Unity 3D there isn't an automatic localisation system. But with the power of the OO Programming and C sharp it's possible to add a simple translation system.
This example will run on Desktop and Android  Systems (this could run on iOS systems also but not tested yet).




You need these components:
  1. lang.xml (with the translations)
  2. a Translate.cs object (which is a singleton)
  3. Then you can invoke a localized string with a simple line of code
So, let's start.
  1. Open (or create a new) Project 
  2. Create a "Resources" directory (inside your Asset directory
  3. Put a new file named "lang.xml" inside the new "Resources" directory
  4. Create a Translate.cs script 
Use this content for "lang.xml"

<?xml version="1.0" encoding="utf-8"?>
<languages>

<English>
    <string name="app_name">This is the name in english</string>
    <string name="score_text">Score: </string>
</English>
 
<French>
    <string name="app_name">Ceci est le nom en français</string>
    <string name="score_text">Score: </string>
</French>
 
<German>
    <string name="app_name">Dies ist der Name auf Deutsch</string>
    <string name="score_text">Ergebnis: </string>
</German>

<Italian>
    <string name="app_name">questo è il nome app italiano</string>
    <string name="score_text">Punteggio</string>
</Italian>

</languages>

Use this content for "Translate.cs" script

using System.Collections;
using System.Xml;
using UnityEngine;
public class Translate
{
    private static string defaultLanguage = "English";
    private static Translate instance = null;
    private static readonly object padlock = new object();
    private Hashtable Strings;
    private Translate()
    {
        string language = Application.systemLanguage.ToString();
        TextAsset textAsset = (TextAsset)Resources.Load("lang", typeof(TextAsset));
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(textAsset.text);
        // check if language exists
        if (xml.DocumentElement[language] == null){
            // system language isn't present, let's select the default one
            language = defaultLanguage;
        }
        setLanguage(xml, language );
    }
    public static Translate Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Translate();
                }
                return instance;
            }
        }
    }
    public void setLanguage(XmlDocument xml, string language)
    {
        Strings = new Hashtable();
        var element = xml.DocumentElement[language];
        if (element != null)
        {
            var elemEnum = element.GetEnumerator();
            while (elemEnum.MoveNext())
            {
                var xmlItem = (XmlElement)elemEnum.Current;
                Strings.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
            }
        }
        else
        {
            Debug.LogError("The specified language does not exist: " + language);
        }
    }
    public string getString(string name)
    {
        if (!Strings.ContainsKey(name))
        {
            Debug.LogError("The specified string does not exist: " + name);
            return "";
        }
        return (string)Strings[name];
    }
}

You can simply invoke a translation string with a line of code:

String translated_app_name = Translate.Instance.getString("app_name");

Comments

  1. ok i will trust in this, i have one similar but android cant read xml file

    ReplyDelete

Post a Comment