Пример #1
0
        private static void ReadFromJsonSource()
        {
            string filePath = EditorUtility.OpenFilePanel(
                "Import language source file",
                Path.Combine(LocalizationDirectory, SourceFolder),
                "json");

            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }
            string dataAsJson = File.ReadAllText(filePath);

            _localizationData = ConvertToLocalizationData(dataAsJson);
        }
Пример #2
0
        private static LocalizationData ConvertToLocalizationData(string dataAsJson)
        {
            var localizationJsonData = JsonUtility.FromJson <LocalizationJsonData>(dataAsJson);
            var data = new LocalizationData(localizationJsonData.language, new List <LocalizationItem>());

            foreach (var item in localizationJsonData.items)
            {
                data.items.Add(new LocalizationItem
                {
                    key   = CreateLocalizedTextKey(item.key),
                    value = item.value
                });
            }

            return(data);
        }
Пример #3
0
        private void OnEnable()
        {
            if (_localizationData == null)
            {
                _localizationData = new LocalizationData(SystemLanguage.English, new List <LocalizationItem>());
            }

            _systemLanguageProp = serializedObject.FindProperty(nameof(Language.title));
            _keyNameProp        = serializedObject.FindProperty(nameof(Language.keyName));
            _list = new ReorderableList(serializedObject, serializedObject.FindProperty(nameof(Language.items)),
                                        true, true, true, true)
            {
                drawHeaderCallback  = rect => { EditorGUI.LabelField(rect, "Localized Texts"); },
                drawElementCallback = (rect, index, isActive, isFocused) =>
                {
                    var element = _list.serializedProperty.GetArrayElementAtIndex(index);
                    EditorGUI.PropertyField(
                        new Rect(rect.x, rect.y, KeyPropertyWidth, EditorGUIUtility.singleLineHeight),
                        element.FindPropertyRelative(nameof(LocalizationItem.key)), GUIContent.none);
                    EditorGUI.PropertyField(
                        new Rect(rect.x + KeyPropertyWidth + ColumnSpace, rect.y,
                                 rect.width - KeyPropertyWidth - ColumnSpace, EditorGUIUtility.singleLineHeight),
                        element.FindPropertyRelative(nameof(LocalizationItem.value)), GUIContent.none);
                },
                onRemoveCallback = list =>
                {
                    if (EditorUtility.DisplayDialog("Warning!",
                                                    "Are you sure you want to delete the localized text?", "Yes", "No"))
                    {
                        ReorderableList.defaultBehaviours.DoRemoveButton(list);
                    }
                },
                onAddCallback = list =>
                {
                    int index = list.serializedProperty.arraySize;
                    list.serializedProperty.arraySize++;
                    list.index = index;
                    var element = list.serializedProperty.GetArrayElementAtIndex(index);
                    element.FindPropertyRelative(nameof(LocalizationItem.key)).objectReferenceValue =
                        CreateLocalizedTextKey(_keyNameProp.stringValue.Trim());
                    element.FindPropertyRelative(nameof(LocalizationItem.value)).stringValue =
                        _keyNameProp.stringValue.Trim();
                }
            };
        }
Пример #4
0
        private void ReadFromJsonSource()
        {
            string filePath = EditorUtility.OpenFilePanel(
                "Import language source file",
                Path.Combine(LocalizationDirectory, SourceFolder),
                "json");

            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }

            string dataAsJson = File.ReadAllText(filePath);

            _localizationData = ConvertToLocalizationData(dataAsJson);
            if (target is Language asset)
            {
                asset.title = _localizationData.language;
                asset.items = _localizationData.items;
                EditorUtility.SetDirty(asset);
                AssetDatabase.SaveAssets();
            }
        }