コード例 #1
0
        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);
            }
        }
コード例 #2
0
        public static void ExportToCSV(LocalizedStringReference reference)
        {
            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);

                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);
                }
            }

            int maxPluralCount;

            maxPluralCount = 1;

            foreach (string identifier in prefs.identifiers)
            {
                maxPluralCount = Mathf.Max(maxPluralCount, LocalizedString.GetPluralCount(new Language(identifier)));
            }

            using (var writer = new CsvFileWriter(prefs.path)) {
                //
                // header
                //
                var header = new List <string>();
                header.Add("Key");
                header.Add("Comment");
                header.Add("Reference");
                header.Add("Plural");
                header.Add("Index");
                header.AddRange(prefs.identifiers);
                writer.WriteRow(header);

                //
                // data
                //
                foreach (var key in keys)
                {
                    LocalizedString.Json src;
                    src = lookup["en"][key];

                    int valueCount;
                    valueCount = src.plural ? maxPluralCount : 1;

                    for (int valueIndex = 0; valueIndex < valueCount; valueIndex++)
                    {
                        var row = new List <string>();
                        row.Add(key);
                        row.Add(src.comment);
                        row.Add(src.reference);
                        row.Add(src.plural.ToString());
                        row.Add(valueIndex.ToString());

                        foreach (var identifier in prefs.identifiers)
                        {
                            int languagePluralCount;
                            languagePluralCount = LocalizedString.GetPluralCount(new Language(identifier));

                            try {
                                if (valueIndex < languagePluralCount)
                                {
                                    row.Add(lookup[identifier][key].values[valueIndex]);
                                }
                                else
                                {
                                    row.Add("N/A");
                                }
                            } catch {
                                row.Add(string.Empty);
                            }
                        }

                        writer.WriteRow(row);
                    }
                }
            }

            AssetDatabase.Refresh();
        }