/// <summary> /// Returns the localization for the given key, or the key itself, if no translation exists. /// </summary> public static string GetLocalization(string key, FallbackMode fallbackMode, string language, params object[] additionalValues) { string value; if (localizationTable.ContainsKey(language) && localizationTable[language].TryGetValue(key, out value)) { return(string.Format(value, additionalValues)); } if (!missingKeysLogged.Contains(key)) { missingKeysLogged.Add(key); Debug.ULogChannel("LocalizationTable", string.Format("Translation for {0} in {1} language failed: Key not in dictionary.", key, language)); } switch (fallbackMode) { case FallbackMode.ReturnKey: return(additionalValues != null && additionalValues.Length >= 1 ? key + " " + additionalValues[0] : key); case FallbackMode.ReturnDefaultLanguage: return(GetLocalization(key, FallbackMode.ReturnKey, DefaultLanguage, additionalValues)); default: return(string.Empty); } }
/// <summary> /// Returns the localization for the given key, or the key itself, if no translation exists. /// </summary> public static string GetLocalization(string key, FallbackMode fallbackMode, string language, params object[] additionalValues) { string value; if (localizationTable.ContainsKey(language) && localizationTable[language].TryGetValue(key, out value)) { return(string.Format(value, additionalValues)); } // If the key is improperly formatted then try to fix it and retry the lookup. if (key.Contains(" ") || key.Any(c => char.IsUpper(c))) { key = key.Replace(' ', '_').ToLower(); GetLocalization(key, fallbackMode, language, additionalValues); } if (!missingKeysLogged.Contains(key)) { missingKeysLogged.Add(key); UnityDebugger.Debugger.Log("LocalizationTable", string.Format("Translation for {0} in {1} language failed: Key not in dictionary.", key, language)); } switch (fallbackMode) { case FallbackMode.ReturnKey: return(additionalValues != null && additionalValues.Length >= 1 ? key + " " + additionalValues[0] : key); case FallbackMode.ReturnDefaultLanguage: return(GetLocalization(key, FallbackMode.ReturnKey, DefaultLanguage, additionalValues)); default: return(string.Empty); } }
/** * <summary> * Returns the localization for the given key, or the key itself, if no translation exists. * </summary> */ public static string GetLocalization(string key, FallbackMode fallbackMode, string language, params string[] additionalValues) { try //Use a try-catch statement, since this operation might throw a KeyNotFoundException. { //Ideally, return the correct value. return(string.Format(localizationTable[language + "_" + key], additionalValues)); } catch { #if UNITY_EDITOR //TODO: Think if #if is a good idea or not. //Log a warning into the console if this operation fails. Debug.LogWarning("Translation for " + key + " failed: Key not in dictionary."); #endif //Switch the fallback mode. switch (fallbackMode) { case FallbackMode.ReturnKey: if (additionalValues.Length >= 1) { return(key + " " + additionalValues[0]); } return(key); //Just return the key. case FallbackMode.ReturnEnglish: return(GetLocalization(key, FallbackMode.ReturnKey, "en_US", additionalValues)); //Return the english equivalent. default: return(""); //Return an empty string. } } }
/// <summary> /// Returns the localization for the given key, or the key itself, if no translation exists. /// </summary> public static string GetLocalization(string key, FallbackMode fallbackMode, string language, params object[] additionalValues) { string value; if (additionalValues != null) { for (int i = 0; i < additionalValues.Length; i++) { if (additionalValues[i] is string) { additionalValues[i] = GetLocalization(additionalValues[i].ToString(), fallbackMode, language); } } } Dictionary <string, string> languageTable; if (localizationTable.TryGetValue(language, out languageTable) && languageTable.TryGetValue(key, out value)) { try { return(string.Format(value, additionalValues)); } catch (FormatException) { Debug.LogWarning("Bad localization for " + key + ". Arguments found: " + additionalValues.Length); } } // If add returns true the key was not present in the HashSet (i.e we just added it and wasn't there before) if (key != string.Empty && IsNumber(key) && missingKeysLogged.Add(key)) { UnityDebugger.Debugger.LogWarning("LocalizationTable", string.Format("Translation for {0} in {1} language failed: Key not in dictionary.", key, language)); } switch (fallbackMode) { case FallbackMode.ReturnKey: return(additionalValues != null && additionalValues.Length >= 1 ? key + " " + additionalValues[0] : key); case FallbackMode.ReturnDefaultLanguage: return(GetLocalization(key, FallbackMode.ReturnKey, DefaultLanguage, additionalValues)); default: return(string.Empty); } }
/// <summary> /// Returns the localization for the given key, or the key itself, if no translation exists. /// </summary> public static string GetLocalization(string key, FallbackMode fallbackMode, string language, params object[] additionalValues) { string value; if (additionalValues != null) { for (int i = 0; i < additionalValues.Length; i++) { if (additionalValues[i] is string) { additionalValues[i] = GetLocalization(additionalValues[i].ToString(), fallbackMode, language); } } } if (localizationTable.ContainsKey(language) && localizationTable[language].TryGetValue(key, out value)) { try { return(string.Format(value, additionalValues)); } catch (FormatException) { Debug.LogWarning("Bad localization for " + key + ". Arguments found: " + additionalValues.Length); } } if (!missingKeysLogged.Contains(key) && key != string.Empty && IsNumber(key)) { missingKeysLogged.Add(key); UnityDebugger.Debugger.LogWarning("LocalizationTable", string.Format("Translation for {0} in {1} language failed: Key not in dictionary.", key, language)); } switch (fallbackMode) { case FallbackMode.ReturnKey: return(additionalValues != null && additionalValues.Length >= 1 ? key + " " + additionalValues[0] : key); case FallbackMode.ReturnDefaultLanguage: return(GetLocalization(key, FallbackMode.ReturnKey, DefaultLanguage, additionalValues)); default: return(string.Empty); } }