public static IEnumerable <LocalizedString.Json> GetStringsFromTextAsset(TextAsset textAsset) { LocalizedString.Json[] value = null; try { value = JsonUtility.FromJson <LocalizedStringDictionary.Json>(textAsset.text).localizedStrings; } catch { } finally { if (value == null) { value = new LocalizedString.Json[0]; } } return(value); }
public static IEnumerable <LocalizedString.Json> GetStringsFromMonoScript(string guid) { string path; path = AssetDatabase.GUIDToAssetPath(guid); string text; text = AssetDatabase.LoadAssetAtPath <TextAsset>(path).text; Dictionary <string, LocalizedString.Json> dictionary; dictionary = new Dictionary <string, LocalizedString.Json>(); { string pattern; pattern = @"GetString(?:Format)?\( [\n\r\s\t]*(\/\*\s*(.+?)\s*\*\/)? [\n\r\s\t]*(\""(.+?)\"") .+? \)"; MatchCollection matches; matches = Regex.Matches(text, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline); if (matches.Count != 0) { foreach (Match match in matches) { LocalizedString.Json json; json = new LocalizedString.Json(); json.reference = string.Format("{0}:{1}", path, GetLineNumber(text, match)); json.comment = match.Groups[2].Value.ToString(); json.key = match.Groups[4].Value.ToString(); json.plural = false; json.values = string.IsNullOrEmpty(json.comment) ? new string[0] : new string[] { json.comment }; dictionary[json.key] = json; } } } { string pattern; pattern = @"GetPluralString(?:Format)?\( [\n\r\s\t]*(\/\*\s*(.+?)\s*\*\/)? [\n\r\s\t]*(\""(.+?)\"") .+? \)"; MatchCollection matches; matches = Regex.Matches(text, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline); if (matches.Count != 0) { foreach (Match match in matches) { LocalizedString.Json json; json = new LocalizedString.Json(); json.reference = string.Format("{0}:{1}", path, GetLineNumber(text, match)); json.comment = match.Groups[2].Value.ToString(); json.key = match.Groups[4].Value.ToString(); json.plural = true; json.values = string.IsNullOrEmpty(json.comment) ? new string[0] : new string[] { json.comment }; dictionary[json.key] = json; } } } { string pattern; pattern = @"GetString(?:Format)?\( [\n\r\s\t]*(""(.+?)""), [\n\r\s\t]*(""(.+?)"") .+? \)"; MatchCollection matches; matches = Regex.Matches(text, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline); if (matches.Count != 0) { foreach (Match match in matches) { LocalizedString.Json json; json = new LocalizedString.Json(); json.reference = string.Format("{0}:{1}", path, GetLineNumber(text, match)); json.comment = match.Groups[4].Value.ToString(); json.key = match.Groups[2].Value.ToString(); json.plural = false; json.values = string.IsNullOrEmpty(json.comment) ? new string[0] : new string[] { json.comment }; dictionary[json.key] = json; } } } { string pattern; pattern = @"GetPluralString(?:Format)?\( [\n\r\s\t]*(""(.+?)""), [\n\r\s\t]*(""(.+?)"") .+? \)"; MatchCollection matches; matches = Regex.Matches(text, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline); if (matches.Count != 0) { foreach (Match match in matches) { LocalizedString.Json json; json = new LocalizedString.Json(); json.reference = string.Format("{0}:{1}", path, GetLineNumber(text, match)); json.comment = match.Groups[4].Value.ToString(); json.key = match.Groups[2].Value.ToString(); json.plural = true; json.values = string.IsNullOrEmpty(json.comment) ? new string[0] : new string[] { json.comment }; dictionary[json.key] = json; } } } return(dictionary.Values); }
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); } }