using System.Collections.Generic; using System.Globalization; using UnityEngine; public class Boostrapi18n { private static Boostrapi18n _Instance; private Dictionary textDic; int currentIndex = 2; //default en public Boostrapi18n() { textDic = ReadFromCsv(); } public static Boostrapi18n Instance { get { if (_Instance == null) { _Instance = new Boostrapi18n(); _Instance.Init(); } return _Instance; } } private void Init() { textDic = ReadFromCsv(); } private Dictionary ReadFromCsv() { //read tsv var tsv = BetterStreamingAssets.ReadAllText("boostrapi18n.tsv"); textDic = new Dictionary(); var lines = tsv.Split('\n'); if (lines.Length < 2) return textDic; var header = lines[0]; var headers = header.Split('\t'); if (headers.Length < 2) return textDic; string Language = "en"; if (!PlayerPrefs.HasKey("Lang")) { CultureInfo info = System.Globalization.CultureInfo.CurrentUICulture; Language = info.TwoLetterISOLanguageName; if (Language == "zh") { if (info.DisplayName.Contains("Simplified")) { Language = "zh_Hans"; } else { Language = "zh_Hant"; } } } else { Language = PlayerPrefs.GetString("Lang"); } for (int i = 1; i < headers.Length; i++) { //把value[1]里面的"\n","\r","\r\n"替换为"" string v = headers[i].Replace("\n", "").Replace("\r", "").Replace("\r\n", ""); if (v == "text_" + Language) { currentIndex = i - 1; break; } } for (int i1 = 1; i1 < lines.Length; i1++) { string line = lines[i1]; var values = line.Split('\t'); if (values.Length < 2) continue; var key = values[0]; string[] value = new string[values.Length - 1]; for (int i = 1; i < values.Length; i++) { //把value[1]里面的"\n","\r","\r\n"替换为"" string v = values[i].Replace("\n", "").Replace("\r", "").Replace("\r\n", "").Replace("\\r\\n", "\r\n"); value[i - 1] = v; //value[i - 1] = values[i]; } textDic.TryAdd(key, value); } return textDic; } public string GetText(string key) { if (!textDic.ContainsKey(key)) { return string.Empty; } return textDic[key][currentIndex]; } }