using UnityEditor; using UnityEditor.Localization; using UnityEngine; namespace Crowdin.Editor.UnityLocalization { using Plugin.Helpers.PluginLocalization; using Plugin.Models; using Plugin.Operations; using Plugin.Server_API.Helpers; public class PushStringsToCrowdin : BasePopupEditor { [MenuItem("Tools/Crowdin/Push strings to Crowdin", false, 91)] public static void OpenExportPopup() { InitPopup(LocalizationHelper.GetLocalStringByKey(LocalKeys.push_strings_popup_title)); } private TableSelector tableSelector; private BranchSelector branchSelector; private UpdateOptionSelector updateOptionSelector; private bool updateStrings = true; private bool cleanupMode = false; protected override void OnEnable() { base.OnEnable(); tableSelector = TableSelector.CreateForStringTables(); branchSelector = BranchSelector.CreateAndLoadSettings(lifetime.Token); updateOptionSelector = UpdateOptionSelector.CreateAndLoadSettings(); if (SettingsHelper.GetProjectType() is ProjectType.StringBased) { branchSelector.RefreshBranchesList(); // don't await, separate thread task } } private void OnGUI() { if (!isBusy) { bool isProjectStringBased = SettingsHelper.GetProjectType() is ProjectType.StringBased; tableSelector.DrawLayout(); updateOptionSelector.DrawLayout(); if (isProjectStringBased) { branchSelector.DrawLayout(); } if (isProjectStringBased) { GUILayout.BeginHorizontal("box"); updateStrings = UIHelper.AddToggle_UpdateStrings(updateStrings); cleanupMode = UIHelper.AddToggle_CleanupMode(cleanupMode); GUILayout.EndHorizontal(); GUILayout.Space(10); } if (GUILayout.Button(LocalizationHelper.GetLocalStringByKey(LocalKeys.push_strings_popup_title))) { if (!IsProjectSelected()) { return; } var stringTableCollection = LocalizationEditorSettings.GetStringTableCollections(); if (stringTableCollection.Count is 0) { EditorUtility.DisplayDialog( LocalizationHelper.GetLocalStringByKey(LocalKeys.alert_title), LocalizationHelper.GetLocalStringByKey(LocalKeys.alert_push_strings), LocalizationHelper.GetLocalStringByKey(LocalKeys.alert_btn) ); return; } if (tableSelector.NoTablesAreSelected()) { DialogUtility.NoTablesAreSelected(); return; } tableSelector.SaveSettings(); branchSelector.SaveSettings(); updateOptionSelector.SaveSettings(); var options = new PushStringsOptions( tableSelector.ResolveTables(), isProjectStringBased ? branchSelector.GetSelectedBranchId() : null, updateStrings, cleanupMode, updateOptionSelector.SelectedApiKey); RunOperation( (progress, cancellationToken) => PushStrings.RunAsync(options, progress, cancellationToken), result => { status.AppendLine(); status.AppendLine(LocalizationHelper.GetLocalStringByKey( LocalKeys.send_strings_finished, result.SentStrings.ToString())); }); } } OnGUIStatusLabel(); } protected override void OnDisable() { base.OnDisable(); tableSelector?.Dispose(); branchSelector?.Dispose(); } } }