Example #1
0
 protected void DrawTools()
 {
     EditorGUILayout.BeginVertical("Box");
     EditorGUILayout.LabelField(new GUIContent("Import / Export", ""), EditorStyles.boldLabel);
     _importExportHelpRect = EditorHelper.ShowHideableHelpBox("GameFramework.LocalisationEditorWindow.ImportExport", "If you would like to edit the data outside Unity then you can import from and export to .csv (text) files. Entries from any imported file will be merged with existing entries, replacing any keys that already exist with a similar name.\n\nIf you have previously used .csv files for localisation then you should use the import button to import old files into the new localisation system", _importExportHelpRect);
     if (GUILayout.Button("Import csv", EditorStyles.miniButton))
     {
         var newFileName = EditorUtility.OpenFilePanel("Select a .csv localisation file", _importExportFilename, "csv");
         if (!string.IsNullOrEmpty(newFileName))
         {
             _importExportFilename = newFileName;
             var importedLocalisationData = LocalisationData.LoadCsv(_importExportFilename);
             if (importedLocalisationData != null)
             {
                 Undo.RecordObject(_targetLocalisationData, "Import Localisation Csv");
                 _targetLocalisationData.Merge(importedLocalisationData);
                 _targetChanged = true;
                 EditorUtility.DisplayDialog("Localisation Import", string.Format("Import Complete!\n\nImported {0} languages and {1} entries.", importedLocalisationData.Languages.Count, importedLocalisationData.Entries.Count), "Ok");
             }
             else
             {
                 EditorUtility.DisplayDialog("Localisation Import", "Import failed!\n\nSee the console window for further details.", "Ok");
             }
         }
     }
     if (GUILayout.Button("Export csv", EditorStyles.miniButton))
     {
         var newFileName = EditorUtility.SaveFilePanel("Select a .csv localisation file", _importExportFilename, "localisation", "csv");
         if (!string.IsNullOrEmpty(newFileName))
         {
             _importExportFilename = newFileName;
             if (_targetLocalisationData.WriteCsv(_importExportFilename))
             {
                 EditorUtility.DisplayDialog("Localisation Export", "Export complete!", "Ok");
             }
             else
             {
                 EditorUtility.DisplayDialog("Localisation Export", "Export failed!\n\nSee the console window for further details.", "Ok");
             }
         }
     }
     EditorGUILayout.EndVertical();
 }
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();
            }
        }