/// <summary> /// Deserializes JSON data contained within the passed filename /// </summary> /// <param name="filename"></param> public void LoadLocalizedText(string fileName) { localizedText = new Dictionary <string, string> (); string filePath = Path.Combine(Path.Combine(Application.streamingAssetsPath, "lang"), fileName); if (File.Exists(filePath)) { string dataAsJson = File.ReadAllText(filePath); LocalizedLanguageData loadedData = JsonUtility.FromJson <LocalizedLanguageData> (dataAsJson); for (int i = 0; i < loadedData.items.Length; i++) { localizedText.Add(loadedData.items[i].key, loadedData.items[i].value); } selectedLanguage = loadedData.Language; foreach (KeyValuePair <string, string> pair in unlocalizedText) { if (!localizedText.ContainsKey(pair.Key)) { localizedText.Add(pair.Key, pair.Value); } } Debug.Log("Data loaded, dictionary contains: " + localizedText.Count + " entries"); } else { Debug.LogError("Cannot find file! Defaulting to unlocalized text"); LoadUnlocalizedText(); } isReady = true; }
private void LoadUnlocalizedText() { unlocalizedText = new Dictionary <string, string> (); string filePath = Path.Combine(Application.streamingAssetsPath, "lang"); filePath = Path.Combine(filePath, unlocalizedTextPath); if (File.Exists(filePath)) { string dataAsJson = File.ReadAllText(filePath); LocalizedLanguageData loadedData = JsonUtility.FromJson <LocalizedLanguageData> (dataAsJson); for (int i = 0; i < loadedData.items.Length; i++) { unlocalizedText.Add(loadedData.items[i].key, loadedData.items[i].value); } Debug.Log("Data loaded, dictionary contains: " + unlocalizedText.Count + " entries"); } else { Debug.LogError("Cannot find file! Defaulting to English text"); LoadLocalizedText("localizedText_en.json"); } localizedText = unlocalizedText; }
/// Simply creates an empty language data object private void CreateNewData() { localizationData = new LocalizedLanguageData(); string unlocalizedJson = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "unlocalizedText_en.json")); LocalizedLanguageData unlocalizedData = JsonUtility.FromJson <LocalizedLanguageData> (unlocalizedJson); localizationData.items = unlocalizedData.items; }
private void MergeNewUnlocalizedStrings() { string unlocalizedJson = File.ReadAllText(Path.Combine(Path.Combine(Application.streamingAssetsPath, "lang"), "unlocalizedText_en.json")); LocalizedLanguageData unlocalizedData = JsonUtility.FromJson <LocalizedLanguageData> (unlocalizedJson); foreach (LocalizationItem item in unlocalizedData.items) { if (!localizationData.ContainsKey(item.key)) { localizationData.AddNewItem(item); } } }
// Deserializes localization data from disk private void LoadLanguageFile() { string filePath = EditorUtility.OpenFilePanel("Select localization data file", Path.Combine(Application.streamingAssetsPath, "lang"), "json"); if (!string.IsNullOrEmpty(filePath)) { string dataAsJson = File.ReadAllText(filePath); localizationData = JsonUtility.FromJson <LocalizedLanguageData> (dataAsJson); MergeNewUnlocalizedStrings(); } else { //TODO throw error cleanly due to illegal file path Debug.LogError("Could not find localization data at " + filePath + "."); } }
public static void UpdateAllLocalizationData() { string unlocalizedJson = File.ReadAllText(Path.Combine(Path.Combine(Application.streamingAssetsPath, "lang"), "unlocalizedText_en.json")); LocalizedLanguageData unlocalizedData = JsonUtility.FromJson <LocalizedLanguageData> (unlocalizedJson); SupportedLanguages currentLanguage = (SupportedLanguages)System.Enum.GetValues(typeof(SupportedLanguages)).GetValue(1); LocalizedLanguageData target; while ((int)currentLanguage < System.Enum.GetValues(typeof(SupportedLanguages)).Length + 1) { string langFile = string.Empty; switch (currentLanguage) { case SupportedLanguages.UNLOCALIZED: currentLanguage++; continue; case SupportedLanguages.English: langFile = "localizedText_en.json"; break; case SupportedLanguages.French: langFile = "localizedText_fr.json"; break; case SupportedLanguages.German: langFile = "localizedText_de.json"; break; case SupportedLanguages.Italian: langFile = "localizedText_it.json"; break; default: break; } target = JsonUtility.FromJson <LocalizedLanguageData> (Path.Combine(Path.Combine(Application.streamingAssetsPath, "lang"), langFile)); MergeNewUnlocalizedStrings(target); target = null; currentLanguage++; } }
/// <summary> /// Allows obtaining any language file via the instance /// </summary> /// <param name="filename">Language file to load</param> /// <returns>Data stored in language file, or null</returns> public static Dictionary <string, string> GetLocalizedTextDictionary(string filename) { Dictionary <string, string> text = null; string filePath = Path.Combine(Path.Combine(Application.streamingAssetsPath, "lang"), filename); if (File.Exists(filePath)) { text = new Dictionary <string, string> (); string dataAsJson = File.ReadAllText(filePath); LocalizedLanguageData loadedData = JsonUtility.FromJson <LocalizedLanguageData> (dataAsJson); for (int i = 0; i < loadedData.items.Length; i++) { text.Add(loadedData.items[i].key, loadedData.items[i].value); } Debug.Log("Data loaded, dictionary contains: " + text.Count + " entries"); } else { Debug.LogError("Cannot find file!"); } return(text); }