Beispiel #1
0
        public void AddLocale_WithNonPersistentLocale_GeneratesError()
        {
            var locale = ScriptableObject.CreateInstance <Locale>();

            Assert.Throws <AssetNotPersistentException>(() => LocalizationEditorSettings.AddLocale(locale));
            Object.DestroyImmediate(locale);
        }
        public IEnumerator ListViewUpdatesWhenLocaleIsAdded()
        {
            bool          localeAdded = false;
            WrapperWindow window      = GetWindow((wnd) =>
            {
                // Add a new Locale
                if (!localeAdded)
                {
                    var newLocale = Locale.CreateLocale(SystemLanguage.Hebrew);
                    LocalizationEditorSettings.AddLocale(newLocale);
                    Assert.That(m_FakeLocaleProvider.TestLocales, Does.Contain(newLocale));
                    localeAdded = true;
                    return(false);
                }
                else
                {
                    CheckListContainsProjectLocales(wnd);
                    return(true);
                }
            });

            var root = m_PropertyDrawer.CreatePropertyGUI(m_ScriptableObject.FindProperty("provider"));

            window.rootVisualElement.Add(root);

            yield return(null);

            Assert.That(window.TestCompleted, Is.True);
        }
Beispiel #3
0
        public IEnumerator ListViewUpdatesWhenLocaleIsAdded()
        {
            WrapperWindow window = GetWindow((wnd) =>
            {
                // Perform an update
                var height = m_PropertyDrawer.GetPropertyHeight(m_Property, GUIContent.none);
                m_PropertyDrawer.OnGUI(new Rect(wnd.position.x, wnd.position.y, wnd.position.width, height), m_Property, GUIContent.none);

                // Add a new Locale
                var newLocale = Locale.CreateLocale(SystemLanguage.Hebrew);
                LocalizationEditorSettings.AddLocale(newLocale);

                // Update again, it should not contain the new element
                height = m_PropertyDrawer.GetPropertyHeight(m_Property, GUIContent.none);
                m_PropertyDrawer.OnGUI(new Rect(wnd.position.x, wnd.position.y, wnd.position.width, height), m_Property, GUIContent.none);

                Assert.That(m_FakeLocaleProvider.TestLocales, Does.Contain(newLocale));
                CheckListViewContainsProjectLocales();

                return(true);
            });

            yield return(null);

            Assert.That(window.TestCompleted, Is.True);
        }
Beispiel #4
0
        public override VisualElement CreatePropertyGUI(SerializedProperty property)
        {
            var root = Resources.GetTemplate(nameof(LocalesProvider));

            var list = new ReorderableList(new List <Locale>());

            list.HeaderTitle         = "Available Locales";
            list.ReorderCallback     = ChangeLocaleOrder;
            list.CreateItemCallback  = CreateItem;
            list.RefreshListCallback = UpdateList;
            list.RemoveCallback      = RemoveSelectedLocale;
            list.DisplayAddButton    = false; // There is no UI Toolkit ObjectPicker in 2019.4
            root.Q("locales-list").Add(list);

            var openGenerator = root.Q <Button>("open-generator-button");

            openGenerator.clicked += LocaleGeneratorWindow.ShowWindow;

            var addCustomLocale = root.Q <Button>("add-custom-locale");

            addCustomLocale.clicked += CustomLocaleUIWindow.ShowWindow;

            var addAll = root.Q <Button>("add-all-button");

            addAll.clicked += () =>
            {
                var assets = AssetDatabase.FindAssets("t:Locale");
                for (int i = 0; i < assets.Length; ++i)
                {
                    LocalizationEditorSettings.AddLocale(AssetDatabase.LoadAssetAtPath <Locale>(AssetDatabase.GUIDToAssetPath(assets[i])), true);
                }
                list.RefreshList();
            };

            Action <Locale>       projectLocaleAddedRemoved = l => list.RefreshList();
            EventHandler <Locale> projectLocaleSortChanged  = (a, b) => list.RefreshList();

            root.RegisterCallback <AttachToPanelEvent>(evt =>
            {
                LocalizationEditorSettings.EditorEvents.LocaleSortOrderChanged += projectLocaleSortChanged;
                LocalizationEditorSettings.EditorEvents.LocaleAdded            += projectLocaleAddedRemoved;
                LocalizationEditorSettings.EditorEvents.LocaleRemoved          += projectLocaleAddedRemoved;
            });

            root.RegisterCallback <DetachFromPanelEvent>(evt =>
            {
                LocalizationEditorSettings.EditorEvents.LocaleSortOrderChanged -= projectLocaleSortChanged;
                LocalizationEditorSettings.EditorEvents.LocaleAdded            -= projectLocaleAddedRemoved;
                LocalizationEditorSettings.EditorEvents.LocaleRemoved          -= projectLocaleAddedRemoved;
            });

            return(root);
        }
        void DrawToolbar(Rect rect)
        {
            var commandName = Event.current.commandName;
            var controlId   = GUIUtility.GetControlID(FocusType.Passive);

            var fieldPos  = EditorGUI.PrefixLabel(rect, GUIContent.none);
            var selection = (ToolBarChoices)GUI.Toolbar(fieldPos, -1, Styles.toolbarButtons);

            switch (selection)
            {
            case ToolBarChoices.LocaleGeneratorWindow:
                LocaleGeneratorWindow.ShowWindow();
                break;

            case ToolBarChoices.RemoveSelected:
            {
                var selectedLocales = ListView.GetSelection();
                for (int i = selectedLocales.Count - 1; i >= 0; --i)
                {
                    var item = ListView.GetRows()[selectedLocales[i]] as SerializedLocaleItem;
                    LocalizationEditorSettings.RemoveLocale(item.SerializedObject.targetObject as Locale, true);
                }
                ListView.SetSelection(new int[0]);
                ListView.Reload();
            }
            break;

            case ToolBarChoices.AddAsset:
                EditorGUIUtility.ShowObjectPicker <Locale>(null, false, string.Empty, controlId);
                break;

            case ToolBarChoices.AddAllAssets:
            {
                var assets = AssetDatabase.FindAssets("t:Locale");
                for (int i = 0; i < assets.Length; ++i)
                {
                    LocalizationEditorSettings.AddLocale(AssetDatabase.LoadAssetAtPath <Locale>(AssetDatabase.GUIDToAssetPath(assets[i])), true);
                }
                ListView.Reload();
            }
            break;
            }

            if (commandName == "ObjectSelectorClosed" && EditorGUIUtility.GetObjectPickerControlID() == controlId && EditorGUIUtility.GetObjectPickerObject() != null)
            {
                LocalizationEditorSettings.AddLocale(EditorGUIUtility.GetObjectPickerObject() as Locale, true);
                ListView.Reload();
            }
        }
        public void Setup()
        {
            m_Instance         = ScriptableObject.CreateInstance <Fixture>();
            m_ScriptableObject = new SerializedObject(m_Instance);
            m_Property         = m_ScriptableObject.FindProperty("provider");
            Assert.That(m_ScriptableObject, Is.Not.Null);
            Assert.That(m_Property, Is.Not.Null);

            m_FakeLocaleProvider = new FakeLocaleProvider();
            LocalizationEditorSettings.Instance = m_FakeLocaleProvider;
            LocalizationEditorSettings.AddLocale(Locale.CreateLocale(SystemLanguage.English));
            LocalizationEditorSettings.AddLocale(Locale.CreateLocale(SystemLanguage.French));
            LocalizationEditorSettings.AddLocale(Locale.CreateLocale(SystemLanguage.Korean));

            m_PropertyDrawer = new LocalesProviderPropertyDrawer();
        }
Beispiel #7
0
        public void ProjectLocalesIsUpdatedWhenRemoveLocaleIsUndone()
        {
            const string localeAssetPath = "Assets/HebrewRemove.asset";
            var          locale          = Locale.CreateLocale(SystemLanguage.Hebrew);

            AssetDatabase.CreateAsset(locale, localeAssetPath);
            LocalizationEditorSettings.AddLocale(locale, false);
            Assert.That(LocalizationEditorSettings.GetLocales(), Does.Contain(locale), "Expected new locale asset to be added to Project Locales.");

            LocalizationEditorSettings.RemoveLocale(locale, true);
            Assert.That(LocalizationEditorSettings.GetLocales(), Does.Not.Contains(locale), "Expected locale to not be in project locales after calling RemoveLocale.");

            Undo.PerformUndo();
            Assert.That(LocalizationEditorSettings.GetLocales(), Does.Contain(locale), "Expected locale asset to be in project locale after calling Undo.");

            AssetDatabase.DeleteAsset(localeAssetPath);
        }
        internal static void ExportSelectedLocales(string path, List <LocaleIdentifier> selectedIdentifiers)
        {
            try
            {
                // Generate the locale assets
                EditorUtility.DisplayProgressBar(Texts.progressTitle, "Creating Locale Objects", 0);
                var localeDict = new Dictionary <LocaleIdentifier, Locale>(); // Used for quick look up of parents
                var locales    = new List <Locale>();

                foreach (var selectedIdentifier in selectedIdentifiers)
                {
                    var locale = CreateInstance <Locale>();
                    locale.Identifier = selectedIdentifier;
                    locale.name       = selectedIdentifier.CultureInfo.EnglishName;
                    locales.Add(locale);
                    localeDict[selectedIdentifier] = locale;
                }

                // When checking for fallbacks we also need to take into account the existing locales
                var allLocales = new List <Locale>(locales);
                allLocales.AddRange(LocalizationEditorSettings.GetLocales());

                // Set up fallbacks
                foreach (var locale in allLocales)
                {
                    // A custom locale may not have a CultureInfo.
                    var cultureInfo = locale.Identifier.CultureInfo;
                    if (cultureInfo == null)
                    {
                        continue;
                    }

                    var    localeParentCultureInfo = locale.Identifier.CultureInfo.Parent;
                    Locale foundParent             = null;
                    while (localeParentCultureInfo != CultureInfo.InvariantCulture && foundParent == null)
                    {
                        localeDict.TryGetValue(localeParentCultureInfo.Name, out foundParent);
                        localeParentCultureInfo = localeParentCultureInfo.Parent;
                    }

                    if (foundParent != null)
                    {
                        locale.Metadata.AddMetadata(new FallbackLocale(foundParent));
                        EditorUtility.SetDirty(locale);
                    }
                }

                // Export the assets
                AssetDatabase.StartAssetEditing(); // Batch the assets into a single asset operation
                var relativePath = PathHelper.MakePathRelative(path);
                for (int i = 0; i < locales.Count; ++i)
                {
                    var locale = locales[i];
                    EditorUtility.DisplayProgressBar(Texts.progressTitle, "Creating Asset " + locale.name, i / (float)locales.Count);
                    var assetPath = Path.Combine(relativePath, $"{locale.name} ({locale.Identifier.Code}).asset");
                    assetPath = AssetDatabase.GenerateUniqueAssetPath(assetPath);
                    AssetDatabase.CreateAsset(locale, assetPath);
                }
                AssetDatabase.StopAssetEditing();

                // Import the Locales now instead of waiting for them to be imported via the asset post processor.
                // If we wait for them to be imported during the asset post processor then they will not be available
                // until all current assets have been imported.
                // This can cause duplicate Locales to be created when importing multiple tables with missing Locales.
                locales.ForEach(l => LocalizationEditorSettings.AddLocale(l));
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }