Search Unity

Multi-Language Data

Discussion in 'Community Learning & Teaching' started by IsGreen, May 2, 2015.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    I created a generic class that uses a parameter type, to create multilingual data, that is, which change according to the language selected in the application interface.

    I rated 88 languages, all I found.

    Typically, this is done with matrices or some routine that changes one by one the variable text, sound, or otherwise.

    But with the generic class MultiLanguage <T> is not necessary to directly access these variables, it automatically changes its value depending on the selected language.

    To create a variable Multilanguage assign an identification number ID (unique). The name and ID must be annotated separately in a paper or document (Table ...) For example:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class script1 : MonoBehaviour {
    5.  
    6.     public Rect rect1;
    7.     public Rect rect2;
    8.  
    9.     MultiLanguage<string> affirmative = 0; //Type string, ID 0
    10.     MultiLanguage<string> salutation = 1; //Type string, ID 1
    11.  
    12.  
    13.     void OnGUI(){
    14.  
    15.         GUI.Label(this.rect1, affirmative);
    16.         GUI.Label(this.rect2, salutation);
    17.  
    18.     }
    19.  
    20. }
    In the script1 we have declared two private multilingual string variables with a unique ID . In this class they can be used by name: affirmative and salutation.

    These multilanguage variables can be declared locally, and be used globally because they are stored in a Dictionary by unique ID (an integer).

    For example, the class script2 uses a multilingual AudioClip and Texture has been declared in another class, thanks to the unique ID we have assigned:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class script2 : MonoBehaviour {
    5.  
    6.     public Rect rect1;
    7.     public Rect rect2;
    8.     public AudioSource audioSource;
    9.  
    10.     void OnGUI(){
    11.      
    12.         if(GUI.Button(this.rect1, "AudioClip")) this.audioSource.PlayOneShot(MultiLanguage<AudioClip>.GetID(0));
    13.         GUI.DrawTexture(this.rect2,MultiLanguage<Texture>.GetID(0));
    14.      
    15.     }
    16.  
    17. }


    In the example I created a AssetBundle per language. The LoadLanguage.cs script is responsible for reading data from these AssetBundle.

    For the text, I use a .txt file where I keep all text strings in order of ID separated by newline. That is, the string 0 in .txt file is affirmative (ID 0), and the string 1 corresponds to salutation (ID 1).

    To store other data such as sound and textures, I keep the assets separately, and the name of the asset is composed of the type (Sound, Texture, ...) plus the ID number, ie, Sound0, Sound1, .... or Texture0, Texture1 ...

    Download link.