示例#1
0
        protected override TreeViewItem BuildRoot()
        {
            var root = new TreeViewItem(-1, -1);
            var id   = 1;

            root.AddChild(new TableEntryTreeViewItem(null, null, id++, 0)
            {
                displayName = $"None ({m_AssetType.Name})"
            });

            if (m_AssetType == typeof(string))
            {
                var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();
                foreach (var collection in stringTableCollections)
                {
                    var tableNode = new TreeViewItem(id++, 0, collection.TableCollectionName)
                    {
                        icon = AssetDatabase.GetCachedIcon(AssetDatabase.GetAssetPath(collection)) as Texture2D
                    };
                    root.AddChild(tableNode);

                    var sharedData = collection.SharedData;
                    foreach (var entry in sharedData.Entries)
                    {
                        tableNode.AddChild(new TableEntryTreeViewItem(collection, entry, id++, 1));
                    }
                }
            }
            else
            {
                var assetTableCollections = LocalizationEditorSettings.GetAssetTableCollections();
                foreach (var collection in assetTableCollections)
                {
                    var tableNode = new TreeViewItem(id++, 0, collection.TableCollectionName)
                    {
                        icon = AssetDatabase.GetCachedIcon(AssetDatabase.GetAssetPath(collection)) as Texture2D
                    };
                    root.AddChild(tableNode);

                    // Only show keys that have a compatible type.
                    var sharedData = collection.SharedData;
                    foreach (var entry in sharedData.Entries)
                    {
                        var typeMetadata = entry.Metadata.GetMetadata <AssetTypeMetadata>();
                        if (typeMetadata == null || m_AssetType.IsAssignableFrom(typeMetadata.Type))
                        {
                            tableNode.AddChild(new TableEntryTreeViewItem(collection, entry, id++, 1));
                        }
                    }
                }
            }

            if (!root.hasChildren)
            {
                root.AddChild(new TreeViewItem(1, 0, "No Tables Found."));
            }

            return(root);
        }
示例#2
0
        internal protected virtual List <LocalizationTableCollection> GetCollections()
        {
            var tableCollections = new List <LocalizationTableCollection>();

            if (m_TableType == typeof(StringTable))
            {
                tableCollections.AddRange(LocalizationEditorSettings.GetStringTableCollections());
            }
            else
            {
                tableCollections.AddRange(LocalizationEditorSettings.GetAssetTableCollections());
            }
            return(tableCollections);
        }
        public static void PullAllExtensions()
        {
            // Get every String Table Collection
            var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();

            foreach (var collection in stringTableCollections)
            {
                // Its possible a String Table Collection may have more than one GoogleSheetsExtension.
                // For example if each Locale we pushed/pulled from a different sheet.
                foreach (var extension in collection.Extensions)
                {
                    if (extension is GoogleSheetsExtension googleExtension)
                    {
                        PullExtension(googleExtension);
                    }
                }
            }
        }
        public void Initialize(bool defaultSelectState = true)
        {
            m_ContentContainer.Clear();

            if (VisibleType.HasFlag(CollectionType.String))
            {
                var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();
                foreach (var collection in stringTableCollections)
                {
                    AddCollection(collection, defaultSelectState);
                }
            }

            if (VisibleType.HasFlag(CollectionType.Asset))
            {
                var assetTableCollections = LocalizationEditorSettings.GetAssetTableCollections();
                foreach (var collection in assetTableCollections)
                {
                    AddCollection(collection, defaultSelectState);
                }
            }
        }
示例#5
0
    public static void FixOrphans()
    {
        var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections().ToLookup(it => it.TableCollectionName);
        var allTables = Resources.FindObjectsOfTypeAll <StringTable>();

        foreach (StringTable table in allTables)
        {
            var collection = stringTableCollections[table.TableCollectionName].First();
            if (!collection.ContainsTable(table))
            {
                // Orphaned table, why does it do this?
                collection.AddTable(table);
                EditorUtility.SetDirty(collection);
                Debug.Log(table + " fixed");
            }
            else
            {
                Debug.Log(table + " looks ok");
            }
        }

        AssetDatabase.SaveAssets();
    }
        public static void ImportAllExtensions()
        {
            // Get every String Table Collection
            var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();

            foreach (var collection in stringTableCollections)
            {
                // Its possible a String Table Collection may have more than one extension.
                foreach (var extension in collection.Extensions)
                {
                    if (extension is CsvExtension csvExtension)
                    {
                        if (!string.IsNullOrEmpty(csvExtension.File) && File.Exists(csvExtension.File))
                        {
                            using (var stream = new StreamReader(csvExtension.File))
                            {
                                Csv.ImportInto(stream, collection, csvExtension.Columns);
                            }
                        }
                    }
                }
            }
        }
        public static void PullAllExtensions()
        {
            // Get every String Table Collection
            var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();

            foreach (var collection in stringTableCollections)
            {
                // Its possible a String Table Collection may have more than one extension.
                foreach (var extension in collection.Extensions)
                {
                    if (extension is CsvExtension csvExtension)
                    {
                        if (!string.IsNullOrEmpty(csvExtension.File))
                        {
                            using (var stream = new StreamWriter(csvExtension.File, false, Encoding.UTF8))
                            {
                                Csv.Export(stream, collection, csvExtension.Columns);
                            }
                        }
                    }
                }
            }
        }
示例#8
0
 internal protected virtual ReadOnlyCollection <StringTableCollection> GetStringTableCollections() => LocalizationEditorSettings.GetStringTableCollections();
示例#9
0
    public static void ToJSON(string apiKey = "", bool upload = false)
    {
        var fileData = new Dictionary <string, StringContent>();

        foreach (StringTableCollection collection in LocalizationEditorSettings.GetStringTableCollections())
        {
            string collectionName = collection.TableCollectionName;

            // Only export english values
            //List<CultureInfo> cultures = GetCulturesInfo(collection);
            //foreach (var culture in cultures)
            //{

            var    culture = CultureInfo.GetCultureInfo("en");
            string folder  = Path.Combine(Application.dataPath, $"Locales/{culture.TwoLetterISOLanguageName}");
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            string path = Path.Combine(folder, $"{collectionName}.json");
            var    json = new JSONObject();

            StringTable table = (StringTable)collection.GetTable(culture);

            foreach (SharedTableData.SharedTableEntry entry in table.SharedData.Entries)
            {
                string key = entry.Key;

                if (table.GetEntry(key) == null || string.IsNullOrEmpty(table.GetEntry(key).Value))
                {
                    continue;
                }

                json[key] = table.GetEntry(key).Value;
            }

            if (upload)
            {
                fileData.Add($"{collectionName}.json", new StringContent(json.ToString(2)));
            }
            else
            {
                using (StreamWriter writer = new StreamWriter(path, false))
                    writer.Write(json.ToString(2));

                Debug.Log($"Wrote: {path}");
            }

            //}
        }

        if (upload)
        {
            string infoUrl   = $"https://api.crowdin.com/api/project/{projectIdentifier}/info?key={apiKey}&json=true";
            string actionUrl = $"https://api.crowdin.com/api/project/{projectIdentifier}/update-file?key={apiKey}";
            string addUrl    = $"https://api.crowdin.com/api/project/{projectIdentifier}/add-file?key={apiKey}";

            using (var formData = new MultipartFormDataContent())
                using (var formData2 = new MultipartFormDataContent())
                    using (var client = new HttpClient())
                    {
                        var infoTask = client.GetStringAsync(infoUrl);
                        infoTask.Wait();
                        var info = JSON.Parse(infoTask.Result);

                        var files = fileData.ToLookup(it => info["files"].AsArray.Children.Any(a => a["name"].Equals(it.Key)));

                        if (files[false].Any(_ => true))
                        {
                            foreach (var file in files[false])
                            {
                                Debug.Log($"Adding {file.Key}");
                                formData2.Add(file.Value, $"files[{file.Key}]", $"files[{file.Key}]");
                            }

                            var addTask = client.PostAsync(addUrl, formData2);
                            addTask.Wait();
                            var add = addTask.Result;

                            if (!add.IsSuccessStatusCode)
                            {
                                var why = add.Content.ReadAsStringAsync();
                                why.Wait();

                                Debug.Log($"Failed to add files to crowdin");
                                Debug.Log(why.Result);
                                return;
                            }
                        }

                        foreach (var file in files[true])
                        {
                            Debug.Log($"Updating {file.Key}");
                            formData.Add(file.Value, $"files[{file.Key}]", $"files[{file.Key}]");
                        }

                        var responseTask = client.PostAsync(actionUrl, formData);
                        responseTask.Wait();
                        var response = responseTask.Result;

                        if (!response.IsSuccessStatusCode)
                        {
                            var why = response.Content.ReadAsStringAsync();
                            why.Wait();

                            Debug.Log($"Failed to update crowdin");
                            Debug.Log(why.Result);
                        }
                        else
                        {
                            Debug.Log($"Uploaded files to crowdin");
                        }
                    }
        }
    }
示例#10
0
    public static void FromJSON(string apiKey = "", bool download = false)
    {
        string downloadUrl = $"https://api.crowdin.com/api/project/{projectIdentifier}/export-file?key={apiKey}";

        foreach (StringTableCollection collection in LocalizationEditorSettings.GetStringTableCollections())
        {
            string collectionName = collection.TableCollectionName;

            List <LocaleIdentifier> cultures = GetCulturesInfo(collection);
            foreach (var culture in cultures)
            {
                if (culture.Code.Equals("en"))
                {
                    continue;
                }

                string folder = Path.Combine(Application.dataPath, $"Locales/{culture.Code}");
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                string   path = Path.Combine(folder, $"{collectionName}.json");
                JSONNode json;
                if (download)
                {
                    using (var client = new HttpClient())
                    {
                        var downloadTask = client.GetAsync($"{downloadUrl}&file={collectionName}.json&language={culture.Code}");
                        try
                        {
                            downloadTask.Wait();
                            var stringTask = downloadTask.Result.Content.ReadAsStringAsync();
                            stringTask.Wait();
                            json = JSON.Parse(stringTask.Result);
                        }
                        catch (Exception)
                        {
                            // 404 = Import empty, anything else = skip
                            if (downloadTask.Result.StatusCode != System.Net.HttpStatusCode.NotFound)
                            {
                                continue;
                            }
                            json = new JSONObject();
                        }
                    }
                }
                else
                {
                    json = GetNodeFromFile(path);
                }

                StringTable table = (StringTable)collection.GetTable(culture);
                table.MetadataEntries.Clear();
                StringTable tableEnglish = (StringTable)collection.GetTable(CultureInfo.GetCultureInfo("en"));

                foreach (SharedTableData.SharedTableEntry entry in table.SharedData.Entries)
                {
                    string key = entry.Key;

                    if (json.HasKey(key))
                    {
                        table.AddEntry(key, json[key]);
                    }

                    if (table.GetEntry(key) == null || string.IsNullOrEmpty(table.GetEntry(key).Value))
                    {
                        // Add english as default
                        table.AddEntry(key, tableEnglish.GetEntry(key).Value);
                    }

                    table.GetEntry(key).IsSmart = false;
                    table.GetEntry(key).MetadataEntries.Clear();
                    table.GetEntry(key).IsSmart = tableEnglish.GetEntry(key).IsSmart;
                }

                EditorUtility.SetDirty(table);
                if (download)
                {
                    Debug.Log($"Downloaded: {culture.Code} - {collectionName}.json");
                }
                else
                {
                    Debug.Log($"Loaded: {path}");
                }
            }
        }

        AssetDatabase.SaveAssets();
    }