public static void ExtractStrings(MenuCommand command) { LocalizedStringReference reference; reference = command.context as LocalizedStringReference; Language language; language = GetDefaultLanguage(); TextAsset textAsset; textAsset = GetTextAsset(reference, language) ?? CreateTextAsset(reference, language); IEnumerable <LocalizedString.Json> jsonStrings; jsonStrings = GetStringsFromTextAsset(textAsset); IEnumerable <LocalizedString.Json> sourceStrings; sourceStrings = GetStringsFromMonoScriptsInDirectory(GetSourceDirectory()); IEnumerable <LocalizedString.Json> mergedStrings; mergedStrings = MergeStrings(jsonStrings, sourceStrings); LocalizedStringDictionary.Json json; json = new LocalizedStringDictionary.Json(mergedStrings.ToArray()); File.WriteAllText(AssetDatabase.GetAssetPath(textAsset), JsonUtility.ToJson(json, true).ReplaceSpacesWithTabs()); AssetDatabase.Refresh(); }
public static void ImportFromCSV(LocalizedStringReference reference, bool overwrite = false) { EditorPrefsValue prefs; prefs = GetEditorPrefsValue(reference); if (!prefs.HasValidIdentifiers) { throw new System.InvalidOperationException(string.Format("Invalid identifiers: {0}", string.Join(",", prefs.identifiers))); } if (!prefs.HasValidPath) { throw new System.InvalidOperationException(string.Format("Invalid path: {0}", prefs.path)); } Dictionary <string, Dictionary <string, LocalizedString.Json> > lookup; lookup = new Dictionary <string, Dictionary <string, LocalizedString.Json> >(); HashSet <string> identifiers; identifiers = new HashSet <string>(); HashSet <string> keys; keys = new HashSet <string>(); foreach (string identifier in reference.Identifiers) { lookup[identifier] = new Dictionary <string, LocalizedString.Json>(); identifiers.Add(identifier); if (!overwrite) { TextAsset asset; asset = LocalizedStringReferenceEditor.GetTextAsset(reference, new Language(identifier)); foreach (var stringJson in LocalizedStringReferenceEditor.GetStringsFromTextAsset(asset)) { lookup[identifier][stringJson.key] = stringJson; keys.Add(stringJson.key); } } } using (var reader = new CsvFileReader(prefs.path)) { List <string> header = new List <string>(); reader.ReadRow(header); List <string> row = new List <string>(); while (reader.ReadRow(row)) { string key; key = row[0]; bool isPlural; isPlural = System.Convert.ToBoolean(row[3]); int valueIndex; valueIndex = isPlural ? System.Convert.ToInt32(row[4]) : 0; for (int index = 5; index < row.Count; index++) { string identifier; identifier = header[index]; int valueCount; valueCount = isPlural ? LocalizedString.GetPluralCount(new Language(identifier)) : 1; if (valueIndex >= 0 && valueIndex < valueCount) { Dictionary <string, LocalizedString.Json> dictionary; if (!lookup.TryGetValue(identifier, out dictionary)) { lookup[identifier] = dictionary = new Dictionary <string, LocalizedString.Json>(); } LocalizedString.Json localizedString; if (!dictionary.TryGetValue(key, out localizedString)) { localizedString = new LocalizedString.Json(); localizedString.key = key; localizedString.comment = row[1]; localizedString.plural = isPlural; localizedString.reference = row[2]; localizedString.values = new string[valueCount]; } string[] values; values = new string[valueCount]; localizedString.values.CopyTo(values, 0); values[valueIndex] = row[index]; localizedString.values = values; dictionary[key] = localizedString; } } } foreach (string identifier in prefs.identifiers) { Dictionary <string, LocalizedString.Json> dictionary; if (lookup.TryGetValue(identifier, out dictionary) && dictionary.Count != 0) { LocalizedString.Json[] localizedStrings; localizedStrings = dictionary.Values.ToArray(); LocalizedStringDictionary.Json json; json = new LocalizedStringDictionary.Json(localizedStrings); Language language; language = new Language(identifier); string path; path = LocalizedStringReferenceEditor.GetTextAssetPath(reference, language) ?? LocalizedStringReferenceEditor.CreateTextAssetPath(reference, language); if (!string.IsNullOrEmpty(path)) { File.WriteAllText(path, JsonUtility.ToJson(json, true).ReplaceSpacesWithTabs()); } } } AssetDatabase.Refresh(); LocalizedStringReferenceEditor.UpdateGuids(reference); } }