Example #1
0
        /// <summary>
        /// Fills a LocalisationData with the specified languages and keys and values in the format key-language
        /// </summary>
        /// <param name="localisationData"></param>
        /// <param name="languages"></param>
        /// <param name="keys"></param>
        internal static void FillLocalisationData(LocalisationData localisationData, string[] languages, string[] keys)
        {
            foreach (var language in languages)
            {
                localisationData.AddLanguage(language);
            }

            foreach (var key in keys)
            {
                var entry = localisationData.AddEntry(key);
                for (var i = 0; i < languages.Length; i++)
                {
                    entry.Languages[i] = key + "-" + languages[i];
                }
            }
        }
Example #2
0
        /// <summary>
        /// Load LocalisationData files
        /// </summary>
        public static void Reload(LocalisationConfiguration localisationConfiguration = null)
        {
            Clear();

            if (localisationConfiguration == null)
            {
                localisationConfiguration = GameManager.LoadResource <LocalisationConfiguration>("LocalisationConfiguration");
            }
            //localisationConfiguration = GameObject.FindObjectOfType<LocalisationConfiguration>();
            var setupMode = localisationConfiguration == null ? LocalisationConfiguration.SetupModeType.Auto : localisationConfiguration.SetupMode;

            string[] loadedSupportedLanguages = new string[0];

            // set localisation data
            if (setupMode == LocalisationConfiguration.SetupModeType.Auto)
            {
                // Try to load the default Localisation file directly - don't use GameMangager method as we load in 'reverse' as user items
                // should overwrite system ones.
                var asset = Resources.Load <LocalisationData>("Default/Localisation");
                if (asset != null)
                {
                    LocalisationData         = ScriptableObject.Instantiate(asset); // create a copy so we don't overwrite values.
                    loadedSupportedLanguages = asset.GetLanguageNames();
                }

                // try and load identifier localisation if specified and present, or if not user localisation
                var identifierLocalisationLoaded = false;
                if (GameManager.IsActive && GameManager.GetIdentifierBase() != null)
                {
                    asset = Resources.Load <LocalisationData>(GameManager.GetIdentifierBase() + "/Localisation");
                    if (asset != null)
                    {
                        identifierLocalisationLoaded = true;
                        if (LocalisationData == null)
                        {
                            LocalisationData = asset;
                        }
                        else
                        {
                            LocalisationData.Merge(asset);
                        }
                        loadedSupportedLanguages = asset.GetLanguageNames(); // override any previous
                    }
                }
                if (!identifierLocalisationLoaded)
                {
                    asset = Resources.Load <LocalisationData>("Localisation");
                    if (asset != null)
                    {
                        if (LocalisationData == null)
                        {
                            LocalisationData = asset;
                        }
                        else
                        {
                            LocalisationData.Merge(asset);
                        }
                        loadedSupportedLanguages = asset.GetLanguageNames(); // override any previous
                    }
                }
                if (LocalisationData == null)
                {
                    MyDebug.LogWarning("GlobalLocalisation: No localisation data was found so creating an empty one. Please check that a localisation files exist at /Resources/Localisation or /Resources/Default/Localisation!");
                }
            }
            else if (setupMode == LocalisationConfiguration.SetupModeType.Specified)
            {
                foreach (var localisationData in localisationConfiguration.SpecifiedLocalisationData)
                {
                    // first item gets loaded / copied, subsequent get merged into this.
                    if (LocalisationData == null)
                    {
                        LocalisationData = ScriptableObject.Instantiate(localisationData);  // create a copy so we don't overwrite values.
                    }
                    else
                    {
                        LocalisationData.Merge(localisationData);
                    }
                    loadedSupportedLanguages = localisationData.GetLanguageNames(); // if exists override
                }
                if (LocalisationData == null)
                {
                    MyDebug.LogWarning("GlobalLocalisation: No localisation data was found so creating an empty one. Please check that localisation files exist and are in the correct location!");
                }
            }

            // if nothing loaded then create an empty localisation to avoid errors.
            if (LocalisationData == null)
            {
                LocalisationData = ScriptableObject.CreateInstance <LocalisationData>();
                LocalisationData.AddLanguage("English", "en");
                loadedSupportedLanguages = LocalisationData.GetLanguageNames();
            }

            // set Supported Languages - either from config if present or based upon loaded files.
            if (localisationConfiguration != null && localisationConfiguration.SupportedLanguages.Length > 0)
            {
                List <string> validSupportedLanguages = new List <string>();
                foreach (var language in localisationConfiguration.SupportedLanguages)
                {
                    if (LocalisationData.ContainsLanguage(language))
                    {
                        validSupportedLanguages.Add(language);
                    }
                    else
                    {
                        Debug.Log("GlobalLocalisation: Localisation files do not contain definitions for the specified supported language '" + language + "'");
                    }
                }
                SupportedLanguages = validSupportedLanguages.ToArray();
            }
            else
            {
                SupportedLanguages = loadedSupportedLanguages;
            }


            // if no usable language is already set then set to the default language.
            if (!CanUseLanguage(Language))
            {
                SetLanguageToDefault();
            }
        }
Example #3
0
        protected void DrawLanguages()
        {
            _languagesHelpRect = EditorHelper.ShowHideableHelpBox("GameFramework.LocalisationEditorWindow.Languages", "Here you can specify the languages for which you will provide localised values.\n\nYou should enter the language name and also an optional ISO-639-1 code for use with google translate if you want to perform automatic translations. For convenience Unity supported languages are available from the dropdown button at the bottom right.", _languagesHelpRect);
            EditorGUILayout.BeginVertical("Box");

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Name", GUILayout.ExpandWidth(true));
            EditorGUILayout.LabelField("Code", GUILayout.Width(60 + GuiStyles.RemoveButtonWidth + 6));
            //EditorGUILayout.LabelField("", GUILayout.Width(GuiStyles.RemoveButtonWidth));
            EditorGUILayout.EndHorizontal();

            serializedObject.Update();

            string languageForDeleting = null;

            for (var i = 0; i < _languagesProperty.arraySize; i++)
            {
                EditorGUILayout.BeginHorizontal();

                var languageProperty = _languagesProperty.GetArrayElementAtIndex(i);
                var nameProperty     = languageProperty.FindPropertyRelative("Name");
                EditorGUILayout.PropertyField(nameProperty, GUIContent.none, GUILayout.ExpandWidth(true));

                var codeProperty = languageProperty.FindPropertyRelative("Code");
                EditorGUILayout.PropertyField(codeProperty, GUIContent.none, GUILayout.Width(60));

                if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.Width(GuiStyles.RemoveButtonWidth)))
                {
                    languageForDeleting = nameProperty.stringValue;
                    break;
                }

                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.EndVertical();

            serializedObject.ApplyModifiedProperties();

            // add functionality
            EditorGUILayout.BeginHorizontal();
            _newLanguage = EditorGUILayout.TextField("", _newLanguage, GUILayout.ExpandWidth(true));
            if (string.IsNullOrEmpty(_newLanguage) || _targetLocalisationData.ContainsLanguage(_newLanguage))
            {
                GUI.enabled = false;
            }
            if (GUILayout.Button(new GUIContent("Add", "Add the specified language to the list"), EditorStyles.miniButton, GUILayout.Width(100)))
            {
                Undo.RecordObject(_targetLocalisationData, "Add Language");
                _targetLocalisationData.AddLanguage(_newLanguage);
                _targetChanged = true;
                _newLanguage   = "";
            }
            GUI.enabled = true;

            //if (GUILayout.Button(EditorGUIUtility.IconContent("Toolbar Plus More", "Add to list"), GUILayout.Width(25)))
            if (GUILayout.Button(new GUIContent("+", "Add a new language to the list"), EditorStyles.miniButton, GUILayout.Width(20)))
            {
                var menu = new GenericMenu();
                foreach (var languageDefinition in Languages.LanguageDefinitions)
                {
                    if (!_targetLocalisationData.ContainsLanguage(languageDefinition.Name))
                    {
                        menu.AddItem(new GUIContent(languageDefinition.Name + " (" + languageDefinition.Code + ")"), false, AddLanguage, languageDefinition.Name);
                    }
                }
                menu.ShowAsContext();
            }
            EditorGUILayout.EndHorizontal();

            // delay deleting to avoid editor issues.
            if (languageForDeleting != null &&
                EditorUtility.DisplayDialog("Delete Language?", string.Format("Are you sure you want to delete the language '{0}'?\n\nDeleting this language will also delete all translations for this language from the list of entries.", languageForDeleting), "Yes", "No"))
            {
                Undo.RecordObject(_targetLocalisationData, "Delete Language");
                _targetLocalisationData.RemoveLanguage(languageForDeleting);
                _targetChanged = true;
            }
        }