/// <summary> /// Récupère la chaîne ayant pour clé key /// </summary> /// <param name="dictionary">Dictionnaire dans lequel chercher</param> /// <param name="key">Clé du texte a récupérer</param> /// <returns>Renvoi le texte qui à pour clé key. Renvoi la clé si elle ne correspond à aucun texte</returns> private string GetText(DictionariesEnum dictionary, string key) { if (texts.ContainsKey(dictionary)) { if (texts[dictionary].ContainsKey(key)) { // Rajout de la gestion des clés de lang return(GestionCle(texts[dictionary][key], dictionary, key)); } else { return(dictionary.ToString() + "_" + key); } } else { return("No dictionary : " + dictionary); } }
/// <summary> /// Parse le texte pour remplacer les clés de lang par leur valeur /// </summary> /// <param name="texte"></param> /// <returns></returns> private string GestionCle(string texte, DictionariesEnum dictionary, string key) { char c; string res = ""; string tmp, tmpRes = ""; for (int i = 0; i < texte.Length; i++) { c = texte[i]; if (c == '{') { tmp = ""; while (i + 1 < texte.Length && texte[i + 1] != ':' && texte[i + 1] != '}') { i++; tmp += texte[i]; } if (i + 1 >= texte.Length) { Debug.LogError("Erreur de clé dans le lang : " + dictionary.ToString() + ", " + key); break; } i++; if (i + 1 < texte.Length) { if (texte[i + 1] == 'b') { i = i + 4; } else if (texte[i + 1] == 'g') { i = i + 5; } else { Debug.LogError("Erreur de clé (girl ou boy) dans le lang : " + dictionary.ToString() + ", " + key); break; } } else { Debug.LogError("Erreur de clé dans le lang : " + dictionary.ToString() + ", " + key); break; } if (tmpRes == "") { Debug.LogError("Erreur de clé incohérente dans le lang : " + dictionary.ToString() + ", " + key); break; } res += tmpRes; continue; } res += c; } return(res); }
/// <summary> /// Renvoi le dictionnaire associé au dictionnaire <paramref name="dictionary"/> pour la langue <paramref name="lang"/>. /// </summary> /// <param name="dictionary">dictionnaire qu'on veut charger</param> /// <param name="lang">langue qu'on veut charger</param> /// <returns></returns> private Dictionary <string, string> LoadTextDictionary(DictionariesEnum dictionary, LanguagesEnum lang) { TextAsset txtAsset = Resources.Load <TextAsset>("Lang/Texts/" + dictionary.ToString()); if (txtAsset == null) { Debug.LogError("Lang : no file at: Lang/Texts/" + dictionary.ToString()); return(null); } string txtString = txtAsset.text; string[] lines = txtString.Split(System.Environment.NewLine.ToCharArray()); if (lines.Length <= 0) { Debug.LogError("Lang : not enough lines in Lang/Texts/" + dictionary.ToString()); return(null); } string[] languages = lines[0].Split('|'); int languageIndex = -1; for (int i = 0; i < languages.Length; i++) { if (languages[i].Equals(lang.ToString())) { languageIndex = i; break; } } if (languageIndex < 0) { Debug.LogError("Lang : " + lang.ToString() + " not found in Lang/Texts/" + dictionary.ToString()); return(null); } Dictionary <string, string> ret = new Dictionary <string, string>(); string[] line; char[] tmpCharArray; for (int i = 1; i < lines.Length; i++) { line = lines[i].Split('|'); if (line.Length <= 0 || lines[i] == "") { } else if (line.Length == 1) { Debug.LogError("LangPackage: dictionary " + dictionary + " line " + i + " found only the key, skipping this line"); } else if (!line[0].Equals("")) { tmpCharArray = line[languageIndex].ToCharArray(); if (tmpCharArray[0] == '"' && tmpCharArray[tmpCharArray.Length - 1] == '"') { line[languageIndex] = line[languageIndex].Substring(1, line[languageIndex].Length - 2); } line[languageIndex] = line[languageIndex].Replace("\"\"", "\""); line[languageIndex] = line[languageIndex].Replace("\\n", "\n"); line[languageIndex] = line[languageIndex].Replace("{$pipe}", "|"); ret.Add(line[0], line[languageIndex]); } } return(ret); }