public void Translate()
        {
            var translated = Localization.TranslateString(TableGuid, KeyGuid);

            if (string.IsNullOrEmpty(translated))
            {
                translated = Localization.TranslateString(TableGuid, KeyGuid, Settings.DefaultLocaleIndex());
            }
            if (string.IsNullOrEmpty(translated))
            {
                translated = $"'{Table.GuidToEntry(KeyGuid).Key}' : MISSING TRANSLATION";
            }
            Setter?.Invoke(translated);
        }
Пример #2
0
        private static int GetLocaleIndexPrefs()
        {
#if UNITY_EDITOR
            if (UnityEditor.EditorPrefs.HasKey(Constants.SELECTED_LOCALE_PREFS_KEY))
            {
                return(UnityEditor.EditorPrefs.GetInt(Constants.SELECTED_LOCALE_PREFS_KEY));
            }
            var localeIndex = Settings.DefaultLocaleIndex();
            UnityEditor.EditorPrefs.SetInt(Constants.SELECTED_LOCALE_PREFS_KEY, localeIndex);
            return(localeIndex);
#else
            if (PlayerPrefs.HasKey(Constants.SELECTED_LOCALE_PREFS_KEY))
            {
                return(PlayerPrefs.GetInt(Constants.SELECTED_LOCALE_PREFS_KEY));
            }
            var localeIndex = settings.DefaultLocaleIndex();
            PlayerPrefs.SetInt(Constants.SELECTED_LOCALE_PREFS_KEY, localeIndex);
            return(localeIndex);
#endif
        }
Пример #3
0
        private void OnGUI()
        {
            GUILayout.Label("Select Key");
            EditorGUI.BeginChangeCheck();
            searchQuery = GUILayout.TextField(searchQuery);
            if (string.IsNullOrEmpty(searchQuery))
            {
                var guiColor = GUI.color;
                GUI.color = Color.grey;
                var lastRect = GUILayoutUtility.GetLastRect();
                lastRect.x     += 4;
                lastRect.width -= 4;
                EditorGUI.LabelField(lastRect, "Search keys");
                GUI.color = guiColor;
            }
            if (EditorGUI.EndChangeCheck())
            {
                UpdateFilter();
            }

            var defaultLocaleIndex = settings.DefaultLocaleIndex();

            scrollPosition = GUILayout.BeginScrollView(scrollPosition);
            for (var index = 0; index < filteredEntries.Count; index++)
            {
                var entry = filteredEntries[index];
                var back  = GUI.backgroundColor;
                GUILayout.BeginHorizontal("box");
                if (GUILayout.Button("Select", GUILayout.MinWidth(100), GUILayout.ExpandWidth(false)))
                {
                    owner.OnKeyDirty(index);
                    Close();
                }
                GUI.backgroundColor = index % 2 == 0 ? Color.white : new Color(0.07f, 0.07f, 0.07f);
                GUI.backgroundColor = back;
                GUILayout.Label($"{entry.Key} - {(string.IsNullOrEmpty(entry.Values[defaultLocaleIndex]) ? "NO VALUE SET" : entry.Values[defaultLocaleIndex])}");
                GUILayout.EndHorizontal();
            }

            GUILayout.EndScrollView();
        }
        private void CreateSettingsGUI(LocalizationSettings settings)
        {
            var tableChoices  = settings.TableGuids.Prepend(Guid.Empty.ToString()).ToList();
            var defaultIndex  = settings.GuidToTableIndex(localizedString.TableGuid) + 1;
            var tableDropdown = new PopupField <string>("Table", tableChoices, defaultIndex, guid => settings.GuidToTableName(guid, "None"),
                                                        guid => settings.GuidToTableName(guid, "None"));

            tableDropdown.name = "TableDropdown";
            tableDropdown.RegisterValueChangedCallback(evt => {
                var table = settings.GuidToTable(evt.newValue);
                localizedString.TableGuid = evt.newValue;
                localizedString.Table     = table;
                localizedString.Key       = null;
                localizedString.KeyGuid   = null;
                EditorUtility.SetDirty(localizedString);
                UpdateKeyVisibility();
            });
            rootSettingsContainer.Add(tableDropdown);
            rootSettingsContainer.Add(localizationSettingsContainer = new VisualElement {
                name = "LocalizationSettingsContainer"
            });
            localizationSettingsContainer.AddGet <VisualElement>("KeyContainer").Do(keyContainer => {
                keyContainer.AddGet(new TextField("Key")
                {
                    name = "LocalizationKey", isReadOnly = true
                }).Do(self => {
                    self.SetEnabled(false);
                    self.BindProperty(keyProperty);
                });
                keyContainer.AddGet(new Button(OnSelectKey)
                {
                    text = "Select Key", name = "SelectKeyButton"
                });
            });
            localizationSettingsContainer.Add(new PropertyField(setterProperty).Do(field => {
                field.RegisterValueChangeCallback(evt => {
                    var count = localizedString.Setter.GetPersistentEventCount();
                    for (var i = 0; i < count; i++)
                    {
                        localizedString.Setter.SetPersistentListenerState(i, UnityEventCallState.EditorAndRuntime);
                    }
                });
            }));

            // Preview section
            if (localizedString.PreviewLocaleIndex == -1)
            {
                localizedString.PreviewLocaleIndex = settings.DefaultLocaleIndex();
            }

            var previewContainer = localizationSettingsContainer.AddGet <VisualElement>("PreviewContainer");

            previewContainer.AddGet(previewToggleButton = new Button(TogglePreview)
            {
                name = "TogglePreviewButton", text = "Preview"
            })
            .SetClass(localizedString.IsPreviewActive, "active");

            previewButtons  = new List <Button>();
            previewSettings = previewContainer.AddGet <VisualElement>("PreviewSettings").SetClass(!localizedString.IsPreviewActive, "hidden");
            previewSettings.AddGet(new ScrollView(ScrollViewMode.Horizontal)
            {
                name = "PreviewLocalesContainer"
            }).Do(container => {
                for (var i = 0; i < settings.Locales.Count; i++)
                {
                    var locale      = settings.Locales[i];
                    var localeIndex = i;
                    previewButtons.Add(container.AddGet(
                                           new Button(() => SetPreviewLocale(localeIndex))
                    {
                        text = $"{locale.LocaleCode}"
                    }
                                           .SetClass(localeIndex == localizedString.PreviewLocaleIndex, "active")
                                           )
                                       );
                }
            });
            if (localizedString.IsPreviewActive)
            {
                UpdatePreview();
            }
        }