Advertisement
artemisart

Unity basic lang system

Apr 22nd, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System.Collections.Generic;
  5.  
  6. namespace ModelisaTools
  7. {
  8.     [InitializeOnLoad]
  9.     public static class Lang
  10.     {
  11.         private static int currentLanguage;
  12.         private static List<string> languages;
  13.         private static List<List<string>> words;
  14.        
  15.         static Lang ()
  16.         {
  17.             currentLanguage = 0;
  18.             languages = new List<string> ();
  19.             words = new List<List<string>> ();
  20.            
  21.             if (Directory.Exists (Application.dataPath + "/ModelisaTools/Lang"))
  22.             {
  23.                 string[] files = Directory.GetFiles (Application.dataPath + "/ModelisaTools/Lang");
  24.                 if (files.Length > 0)
  25.                 {
  26.                     string langs = "";
  27.                     for (int i = 0; i < files.Length; i++)
  28.                     {
  29.                         ReadFile (files[i]);
  30.                         languages.Add (Path.GetFileName (files[i]).Substring (0, 2));
  31.                         langs += languages[i];
  32.                         if (i < files.Length - 1) langs += ", ";
  33.                     }
  34.                     DebugConsole.Log ("Lang initialized : " + langs);
  35.                 }
  36.                 else
  37.                 {
  38.                     DebugConsole.Log ("Lang initialization failed : no files found.");
  39.                 }
  40.             }
  41.             else
  42.             {
  43.                 DebugConsole.Log ("ModelisaTools : Lang directory not found !");
  44.             }
  45.         }
  46.        
  47.         private static void ReadFile (string path)
  48.         {
  49.             List<string> langWords = new List<string> ();
  50.             string[] data = File.ReadAllText (path).Split ('$');
  51.            
  52.             for (int i = 0; i < data.Length; i++)
  53.             {
  54.                 data[i] = data[i].Trim (" \n\t".ToCharArray ());
  55.                 if (!string.IsNullOrEmpty (data[i]) && !data[i].StartsWith ("//"))
  56.                 {
  57.                     langWords.Add (data[i]);
  58.                 }
  59.             }
  60.            
  61.             words.Add (langWords);
  62.         }
  63.        
  64.         public static string Trad (string str)
  65.         {
  66.             string trad = "";
  67.            
  68.             if (languages[currentLanguage] == "EN")
  69.             {
  70.                 trad = str;
  71.             }
  72.             else
  73.             {
  74.                 if (words[languages.IndexOf ("EN")].Contains (str))
  75.                 {
  76.                     trad = words[currentLanguage][words[languages.IndexOf ("EN")].IndexOf (str)];
  77.                 }
  78.                 else {
  79.                     trad = "TRAD ERROR : " + str;
  80.                 }
  81.             }
  82.            
  83.             return trad;
  84.         }
  85.        
  86.         public static void DrawLangButton (Rect rect)
  87.         {
  88.             if (GUI.Button (rect, languages[currentLanguage]))
  89.             {
  90.                 currentLanguage = (currentLanguage + 1) % languages.Count;
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement