// find Key used for a value public static string FindKey(string value, bool giveWarning = true) { foreach (KeyValuePair <string, LocalizerValue> keyValuePair in _keyValuePairs) { LocalizerValue keyValue = keyValuePair.Value; if (keyValue.value == value) { return(keyValue.key); } } if (giveWarning) { Logger.Warning("Coud not find value:\"" + value + "\" in localization dictionary.", LocalizerID.WARNING_COULD_NOT_FIND_VALUE); } return(null); }
public static void ExportFile(string fileName) { // load original file string fullFileName = Localizer.FullFileName(fileName); LocalizerKeys localizationKeys = JsonReader.ReadJson <LocalizerKeys>(fullFileName); if (localizationKeys == default) { return; // file failed to load } // collect keys int nKeys = localizationKeys.keys.Length; string[] keys = new string[nKeys]; for (int i = 0; i < nKeys; ++i) { LocalizerValue localizerValue = localizationKeys.keys[i]; keys[i] = localizerValue.key; } // export keys CreateKeysFile(fileName, keys); }
// retrieve Value of key public static string Value(string key, string[] variables = null, bool giveWarning = true) { // // check for compound key if (key.IndexOf(SEPERATOR) > -1) { return(CompoundKey(key, variables, giveWarning)); } // // ensure key starts with a '.' if (!IsKey(key)) { if (giveWarning) { Logger.Warning("Received invalid key \"" + key + "\"", LocalizerID.WARNING_INVALID_KEY_PASSED); } return((Testing) ? TEST_RETURN : key); } string[] strlist = ExpandKey(key); if (strlist.Length > 1) { // found embedded variables in key if (variables != null) { if (giveWarning) { // but variables were passed into Value() Logger.Warning("Key \"" + key + "\" has embedded variables: " + UtilsArray.Print(strlist) + ", but was passed in variable parameters: " + UtilsArray.Print(variables), LocalizerID.WARNING_HAS_EMBEDDED_AND_PASSED_VARIABLES); } } else { // transfer embedded vars to variables list variables = new string[strlist.Length - 1]; for (int i = 0; i < variables.Length; i++) { variables[i] = strlist[i + 1]; } } } key = strlist[0]; // if assigned, use VariablesTinkerer delegate to modify variables list if (VariablesTinkerer != null && UtilsArray.HasValue(variables)) { variables = VariablesTinkerer(variables); } // // retrieve value LocalizerValue stringKeyValue = LocalizerValue(key); string value; if (stringKeyValue != null) { value = stringKeyValue.value; // // replace variables if (variables != null) { // save variables last used with this key stringKeyValue.Variables = variables; // substitute variables int counter = 0; foreach (string variableKey in variables) { string variableValue = Value(variableKey, null, false); string replaceWithVariable = ""; replaceWithVariable += BRACES[0]; replaceWithVariable += counter++; replaceWithVariable += BRACES[1]; value = value.Replace(replaceWithVariable, variableValue); } } } else { // if failed to get value, then use the key value = key; } return((Testing) ? TEST_RETURN : value); }
// Export localization string file with all variables removed from the key private static void CreateKeysFile(string fileName, string[] keys, bool variablesInKeys = false) { // header string exportFileName = "JsonKeys/" + fileName + " " + Localizer.LanguageCode + ".json"; StreamWriter streamWriter = File.CreateText(exportFileName); streamWriter.WriteLine("{"); streamWriter.WriteLine(" \"keys\": ["); bool isFirst = true; // body foreach (string key in keys) { LocalizerValue value = Localizer.LocalizerValue(key); if (value == null) { continue; } if (!isFirst) { streamWriter.WriteLine(" },"); } isFirst = false; string justKey = Localizer.JustKey(key); streamWriter.WriteLine(" {"); streamWriter.WriteLine(" \"key\": \"" + justKey + "\","); if (!string.IsNullOrEmpty(value.context)) { streamWriter.WriteLine(" \"context\": \"" + value.context + "\","); } if (!string.IsNullOrEmpty(value.citation)) { streamWriter.WriteLine(" \"citation\": \"" + value.citation + "\","); } if (!string.IsNullOrEmpty(value.note)) { streamWriter.WriteLine(" \"note\": \"" + value.note + "\","); } string[] variables = variablesInKeys ? null : value.Variables; string export = Localizer.Value(key, variables); if (string.IsNullOrEmpty(export)) { Logger.Warning("key:" + key + " is empty value."); } export = UtilsString.CorrectForQuotes(export); export = UtilsString.CorrectForTabs(export); // check for single quote if (UtilsString.CheckForQuote(export, false)) { Logger.Warning("Found \' in key:" + key); } streamWriter.WriteLine(" \"original\": \"" + export + "\","); streamWriter.WriteLine(" \"value\": \"" + export + "\""); } // footer streamWriter.WriteLine(" }"); streamWriter.WriteLine(" ]"); streamWriter.WriteLine("}"); streamWriter.Close(); Logger.Print(">>> Exported file:" + exportFileName); }