Add Multiple Language Support to Your Unity Projects

Discussion in 'Showcase' started by Tryder, Oct 21, 2013.

  1. Tryder

    Tryder

    Member

    Joined:
    Mar 26, 2012
    Messages:
    89
    After writing a couple of Android applications I thought it would be nice if one could implement support for multiple languages in their Unity projects as easily as in an Android project. I had this idea a few months ago and finally got around to writing it.

    With Unity Multiple Language Support you can easily include multi-lingual support in your Unity projects through XML. Simply create one or more XML files with the strings that have multiple translations and then reference those strings in your scripts with a common identifier. XML format is as follows:

    Code (csharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <languages>
    3.     <English>
    4.          <string name="app_name">Some Game</string>
    5.          <string name="score_text">Score: </string>
    6.     </English>
    7.  
    8.     <French>
    9.         <string name="app_name">Certains Jeux</string>
    10.         <string name="score_text">Score: </string>
    11.     </French>
    12.  
    13.     <German>
    14.         <string name="app_name">Einige Spiele</string>
    15.         <string name="score_text">Ergebnis: </string>
    16.     </German>
    17. </languages>
    Once the Lang Class has been initialized to a particular language you can reference the strings stored in your XML file as follows:

    Code (csharp):
    1. var scoreText : String = langClass.getString("score_text") + newScore.ToString();
    Furthermore you can reset the language on the fly with the following line:

    Code (csharp):
    1. langClass.setLanguage(Path.Combine(Application.dataPath, "lang.xml"), "French");
    The text stored in your XML resource is read as a string so any rich text formatting will remain intact, but you'll need to read up on how to format the XML file for special characters. For the most part characters such as quotes and carats need an escape character.

    The main difference between this implementation and Android's is that Android requires each language to have it's own separate XML file while UMLS leaves it up to the developer to decide whether to throw it all into one XML file or split it up into multiple files. For smaller games a single XML file should do just fine, but since all strings from the selected language are stored in RAM larger games might find it useful to use multiple XML resources such as LVL_01.xml, LVL_02.xml, etcetera. RPGs could even split their resources per conversation such as Adam_01.xml and Jenny_07.xml.

    Feel free to grab Unity Multiple Language Support off of my Google Drive for free:
    https://docs.google.com/file/d/0B6BscJ4Cq-K7WjhTM1B1N1FWSDA/edit?usp=sharing

    P.S. Google Drive opens the zip file so you can download individual items. To download the whole zip file just select File --> Download.

    P.P.S. UMLS provides support for XML resources stored on the local system and resources stored over the web, however; I have not tested UMLS with web based resources so feel free to contact me if you're having trouble.
     
    Last edited: Oct 22, 2013
  2. rattlesnake

    rattlesnake

    Member

    Joined:
    Jul 18, 2013
    Messages:
    23
    Thank youuuuu so muuuch ! it's awesome :D
     
  3. rattlesnake

    rattlesnake

    Member

    Joined:
    Jul 18, 2013
    Messages:
    23
    Ammmm I have an error :/

    Could you please help me ?

    Lang.js(55,27): BCE0018: The name 'XmlDocument' does not denote a valid type ('not found').

    Edit : By the way I'm in API compatibility Level .net 2.0 (with no subset) into the player settings.
    Maybe this cause the problem ?
     
    Last edited: Nov 25, 2013
  4. Tryder

    Tryder

    Member

    Joined:
    Mar 26, 2012
    Messages:
    89
    Hmm. I'm taking a look at msdn.microsoft.com and it indicates that the XmlDocument class is supported as far back as .net 1.0. Check out http://msdn.microsoft.com/en-us/library/system.xml.xmldocument%28v=vs.110%29.aspx for more information about the XmlDocument class.

    I'm noticing in the error that you're receiving it states that it doesn't recognize the XmlDocument type and then references line 55, I've pulled up my script and UniSciTE indicates that the first reference to the XmlDocument type is on line 42 and no reference is made on line 55. Did you modify the script at all? If so make sure you still have the following line at the top:

    Code (csharp):
    1. import System.Xml;
     
  5. shivansps

    shivansps

    Member

    Joined:
    Feb 26, 2014
    Messages:
    15
    This is to load it from resources folder directly, there seems to be no other way of using local storage outside of the editor.

    Code (JavaScript):
    1.     var xmlLang : TextAsset  = Resources.Load("lang") as TextAsset;
    2.     langClass = new Lang(xmlLang.text, currentLang, true);
    And to change it
    Code (JavaScript):
    1.         var xmlLang: TextAsset  = Resources.Load("lang") as TextAsset;
    2.         langClass.setLanguageWeb(xmlLang.text, currentLang);
     
  6. rodv27

    rodv27

    Member

    Joined:
    Nov 17, 2013
    Messages:
    10
    Is there a Csharp version of this?
     
  7. NareshKhandla

    NareshKhandla

    Member

    Joined:
    Aug 6, 2013
    Messages:
    19
    I want to write Gujarati language , please tell me how to do this. Thank you!.