public string GetString(string key, string language) { if (IsLanguageSupported(language)) { if (_keyToIndexLookup.ContainsKey(key)) { LocalisationData data = _dataLookup[language]; int index = _keyToIndexLookup[key]; return(data.strings[index]); } else { return(kDefaultString); } } else { #if !FINAL_BUILD Debug.LogErrorFormat("[LocalisationController] {0} is not a supported language", language); #endif return(kDefaultString); } }
private IEnumerator RoutineLoad() { // load keys file string keysPath = Path.Combine(_filePath, "keys.json"); string keysData = null; #if UNITY_EDITOR try { keysData = File.ReadAllText(keysPath); } catch { } #else UnityWebRequest request = UnityWebRequest.Get(keysPath); yield return(request.SendWebRequest()); if (request.isDone && !request.isHttpError && !request.isNetworkError) { keysData = request.downloadHandler.text; } request.Dispose(); #endif if (!string.IsNullOrEmpty(keysData)) { LocalisationKeys keys = JsonUtility.FromJson <LocalisationKeys>(keysData); // process keys int i = 0, l = keys.keys.Count; for (; i < l; ++i) { _keyToIndexLookup.Add(keys.keys[i], i); } _keys = keys.keys.ToArray(); // load data l = keys.languageFiles.Count; _supportedLanguages = new string[l]; for (i = 0; i < l; ++i) { string languagePath = Path.Combine(_filePath, keys.languageFiles[i]); string languageData = null; #if UNITY_EDITOR languageData = File.ReadAllText(languagePath); #else request = UnityWebRequest.Get(languagePath); yield return(request.SendWebRequest()); if (request.isDone && !request.isHttpError && !request.isNetworkError) { languageData = request.downloadHandler.text; } request.Dispose(); #endif if (!string.IsNullOrEmpty(languageData)) { LocalisationData data = JsonUtility.FromJson <LocalisationData>(languageData); _dataLookup.Add(data.code, data); _supportedLanguages[i] = data.code; } else { throw new UnityException(string.Format("[LocalisationController] Failed to load: {0}", languagePath)); } } IsLoaded = true; if (string.IsNullOrEmpty(_currentLanguage)) { _currentLanguage = GetDefaultLanguage(_defaultLanguage); } if (EventLoaded != null) { EventLoaded(this, _currentLanguage); } } else { throw new UnityException(string.Format("[LocalisationController] Failed to load keys: {0}", keysPath)); } #if UNITY_EDITOR return(null); #endif }