示例#1
0
    /// <summary>
    /// Copies the file into the resources folder. Naming the new asset to
    /// KEY
    /// </summary>
    /// <returns>
    /// The file into resources.
    /// </returns>
    /// <param name='objectPair'>
    /// Object pair.
    /// </param>
    public static string CopyFileIntoResources(SerializableLocalizationObjectPair objectPair, CultureInfo thisCultureInfo)
    {
        string languageFolderPath = Application.dataPath + "/SmartLocalizationLanguage/Resources/Localization/";

        LocFileUtility.CheckAndCreateDirectory(languageFolderPath + thisCultureInfo.Name + "/");

        //TODO: Create generic function
        LocalizedObject objectToCopy = objectPair.changedValue;

        if (objectToCopy.ObjectType == LocalizedObjectType.AUDIO && objectToCopy.ThisAudioClip != null)
        {
            string thisFilePath = languageFolderPath + thisCultureInfo.Name + "/Audio Files/";
            string newFileName  = objectPair.keyValue;

            LocFileUtility.CheckAndCreateDirectory(thisFilePath);

            //Get the current path of the object
            string currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisAudioClip);

            //Get the fileExtension of the asset
            string fileExtension = LocFileUtility.GetFileExtension(Application.dataPath + currentAssetPath);

            //Copy or replace the file to the new path
            FileUtil.ReplaceFile(currentAssetPath, thisFilePath + newFileName + fileExtension);

            return(AssetDatabase.AssetPathToGUID(currentAssetPath));
        }
        else if (objectToCopy.ObjectType == LocalizedObjectType.TEXTURE && objectToCopy.ThisTexture != null)
        {
            string thisFilePath = languageFolderPath + thisCultureInfo.Name + "/Textures/";
            string newFileName  = objectPair.keyValue;

            LocFileUtility.CheckAndCreateDirectory(thisFilePath);
            string currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisTexture);

            string fileExtension = LocFileUtility.GetFileExtension(Application.dataPath + currentAssetPath);

            FileUtil.ReplaceFile(currentAssetPath, thisFilePath + newFileName + fileExtension);

            return(AssetDatabase.AssetPathToGUID(currentAssetPath));
        }
        else if (objectToCopy.ObjectType == LocalizedObjectType.GAME_OBJECT && objectToCopy.ThisGameObject != null)
        {
            string thisFilePath = languageFolderPath + thisCultureInfo.Name + "/Prefabs/";
            string newFileName  = objectPair.keyValue;

            LocFileUtility.CheckAndCreateDirectory(thisFilePath);
            string currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisGameObject);

            string fileExtension = LocFileUtility.GetFileExtension(Application.dataPath + currentAssetPath);

            FileUtil.ReplaceFile(currentAssetPath, thisFilePath + newFileName + fileExtension);

            return(AssetDatabase.AssetPathToGUID(currentAssetPath));
        }
        else
        {
            return("");
        }
    }
    void Initialize()
    {
        if (undoManager == null)
        {
            // Instantiate Undo Manager
            undoManager = new HOEditorUndoManager(this, "Smart Localization - Edit Root Language Window");
        }
        if (keyTypes == null)
        {
            //Get the different key types
            keyTypes = Enum.GetNames(typeof(LocalizedObjectType));
        }

        if (changedRootKeys == null)
        {
            changedRootKeys = new List <SerializableStringPair>();
        }
        if (changedRootValues == null)
        {
            changedRootValues = new List <SerializableLocalizationObjectPair>();
        }
        if (parsedRootValues == null)
        {
            parsedRootValues = new Dictionary <string, LocalizedObject>();
        }

        if (parsedRootValues.Count < 1)
        {
            SetRootValues(LocFileUtility.LoadParsedLanguageFile(null));
        }
    }
示例#3
0
    public void Initialize()
    {
        // Instantiate Undo Manager
        if (undoManager == null)
        {
            undoManager = new HOEditorUndoManager(this, "Smart Localization - Main Window");
        }
        if (microsoftTranslator == null)
        {
            microsoftTranslator = new MicrosoftTranslatorManager();

            //cws == Cry Wolf Studios
            //mt == Microsoft Translator
            if (EditorPrefs.HasKey("cws_mtClientID") && EditorPrefs.HasKey("cws_mtClientSecret") && EditorPrefs.HasKey("cws_mtKeepAuthenticated"))
            {
                mtCliendID     = EditorPrefs.GetString("cws_mtClientID");
                mtCliendSecret = EditorPrefs.GetString("cws_mtClientSecret");
                keepTranslatorAuthenticated = EditorPrefs.GetBool("cws_mtKeepAuthenticated");
            }

            //Authenticate on enable
            if (keepTranslatorAuthenticated)
            {
                microsoftTranslator.GetAccessToken(mtCliendID, mtCliendSecret);
            }
        }

        if (availableLanguages == null || availableLanguages.Count < 1)
        {
            LocFileUtility.CheckAvailableLanguages(availableLanguages, notAvailableLanguages, notAvailableLanguagesEnglishNames);
        }
    }
    public void Initialize(CultureInfo thisCultureInfo, bool checkTranslation)
    {
        if (smartLocWindow != null && !Application.isPlaying && thisCultureInfo != null)
        {
            if (undoManager == null)
            {
                // Instantiate Undo Manager
                undoManager = new HOEditorUndoManager(this, "Smart Localization - Translate Language Window");
            }

            if (thisCultureInfo != null)
            {
                bool newLanguage = thisCultureInfo != this.thisCultureInfo ? true : false;
                this.thisCultureInfo = thisCultureInfo;
                if (thisLanguageValues == null || thisLanguageValues.Count < 1 || newLanguage)
                {
                    InitializeLanguage(thisCultureInfo, LocFileUtility.LoadParsedLanguageFile(null), LocFileUtility.LoadParsedLanguageFile(thisCultureInfo.Name));
                }
            }

            if (checkTranslation)
            {
                //Check if the language can be translated
                canLanguageBeTranslated = false;
                CheckIfCanBeTranslated();

                if (translateFromDictionary != null)
                {
                    translateFromDictionary.Clear();
                    translateFromDictionary = null;
                }
            }
        }
    }
    /// <summary>
    /// Call this from OnInspectorGUI in your own editor class
    /// </summary>
    /// <returns>
    /// The selected key.
    /// </returns>
    /// <param name='currentKey'>
    /// Current key.
    /// </param>
    /// <param name='sort'>
    /// Set this to true if you only want to show keys of a specific type
    /// </param>
    /// <param name='sortType'>
    /// Sort type.
    /// </param>
    public static string SelectKeyGUI(string currentKey, bool sort = false, LocalizedObjectType sortType = LocalizedObjectType.INVALID)
    {
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("Smart Localization", EditorStyles.boldLabel);
        if (GUILayout.Button("Open", GUILayout.Width(50)))
        {
            SmartLocalizationWindow.ShowWindow();
        }
        EditorGUILayout.EndHorizontal();

        if (autoRefresh || parsedRootValues.Count == 0 || sortType != loadedObjectType)
        {
            RefreshList(sort, sortType);
        }
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("Sort Mode: ", EditorStyles.miniLabel, GUILayout.Width(55));
        if (sort)
        {
            GUILayout.Label(sortType.ToString() + " only.", EditorStyles.miniLabel);
        }
        else
        {
            GUILayout.Label("Showing ALL types", EditorStyles.miniLabel);
        }
        EditorGUILayout.EndHorizontal();

        if (LocFileUtility.CheckIfRootLanguageFileExists() && !Application.isPlaying)
        {
            int index = parsedRootValues.IndexOf(currentKey);
            index = Mathf.Max(0, index);
            int newIndex = index;
            newIndex = EditorGUILayout.Popup(index, parsedRootValues.ToArray());

            if (newIndex != index)
            {
                currentKey = parsedRootValues[newIndex];
            }

            if (!autoRefresh && GUILayout.Button("Refresh list", GUILayout.Width(80)))
            {
                RefreshList(sort, sortType);
            }
        }
        else if (Application.isPlaying)
        {
            GUILayout.Label("Feature not available in play mode.", EditorStyles.miniLabel);
        }
        else
        {
            GUILayout.Label("There is no Smart Localization system created", EditorStyles.miniLabel);
            //There is no language created
            if (GUILayout.Button("Create New Localization System"))
            {
                LocFileUtility.CreateRootResourceFile();
            }
        }

        return(currentKey);
    }
    /// <summary>
    /// Initializes the Language
    /// </summary>
    void InitializeLanguage(CultureInfo info, Dictionary <string, LocalizedObject> rootValues, Dictionary <string, LocalizedObject> thisLanguageValues)
    {
        this.rootValues = rootValues;
        this.thisLanguageValues.Clear();
        this.thisLanguageValues = LocFileUtility.CreateSerializableLocalizationList(thisLanguageValues);
        //Load assets
        LocFileUtility.LoadAllAssets(this.thisLanguageValues);

        this.thisLanguage = (thisCultureInfo.EnglishName + " - " + thisCultureInfo.Name);
        rootFileChanged   = false;
    }
示例#7
0
    /// <summary>
    /// Returns a list with all the available languages
    /// </summary>
    /// <returns></returns>
    public static List <CultureInfo> GetAvailableLanguages()
    {
        List <CultureInfo> availableLanguages = new List <CultureInfo>();

        CultureInfo[] cultureInfos = CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures);
        foreach (CultureInfo info in cultureInfos)
        {
            if (LocFileUtility.CheckIfLanguageFileExists(info.Name))
            {
                availableLanguages.Add(info);
            }
        }
        return(availableLanguages);
    }
示例#8
0
    /// <summary>
    /// Creates a new language file with the keys from the root file.
    /// </summary>
    /// <param name='languageName'>
    /// The name of the language
    /// </param>
    public static void CreateNewLanguage(string languageName)
    {
        Dictionary <string, string> rootValues = LocFileUtility.LoadLanguageFile(null);

        //Copy the keys over to the new language
        Dictionary <string, string> baseDictionary = new Dictionary <string, string>();

        foreach (KeyValuePair <string, string> keyPair in rootValues)
        {
            baseDictionary.Add(keyPair.Key, "");
        }

        //Save the new language file
        LocFileUtility.SaveLanguageFile(baseDictionary, LocFileUtility.rootLanguageFilePath + "." + languageName + LocFileUtility.resXFileEnding);
    }
    /// <summary>
    /// Adds a new key to the dictionary
    /// </summary>
    private void AddNewKey()
    {
        LocalizedObject dummyObject = new LocalizedObject();

        dummyObject.ObjectType = LocalizedObjectType.STRING;
        dummyObject.TextValue  = "New Value";

        string addedKey = LocFileUtility.AddNewKeyPersistent(parsedRootValues, "New Key", dummyObject);

        LocalizedObject copyObject = new LocalizedObject();

        copyObject.ObjectType = LocalizedObjectType.STRING;
        copyObject.TextValue  = "New Value";
        changedRootKeys.Add(new SerializableStringPair(addedKey, addedKey));
        changedRootValues.Add(new SerializableLocalizationObjectPair(addedKey, copyObject));
    }
示例#10
0
    /// <summary>
    /// Creates the root resource file.
    /// </summary>
    public static void CreateRootResourceFile()
    {
        //Check if the localizationfolders exists, if not - create them.
        string localizationPath = Application.dataPath + "/SmartLocalizationLanguage/";

        LocFileUtility.CheckAndCreateDirectory(localizationPath);
        localizationPath += "Resources/";
        LocFileUtility.CheckAndCreateDirectory(localizationPath);
        localizationPath += "Localization/";
        LocFileUtility.CheckAndCreateDirectory(localizationPath);

        //Add a dummy value so that the user will see how everything works
        Dictionary <string, string> baseDictionary = new Dictionary <string, string>();

        baseDictionary.Add("MyFirst.Key", "MyFirstValue");

        LocFileUtility.SaveLanguageFile(baseDictionary, LocFileUtility.rootLanguageFilePath + LocFileUtility.resXFileEnding);
    }
示例#11
0
    /// <summary>
    /// Checks the available languages.
    /// </summary>
    public static void CheckAvailableLanguages(List <CultureInfo> availableLanguages, List <CultureInfo> notAvailableLanguages, List <string> notAvailableLanguagesEnglishNames)
    {
        availableLanguages.Clear();
        notAvailableLanguages.Clear();
        notAvailableLanguagesEnglishNames.Clear();

        CultureInfo[] cultureInfos = CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures);
        foreach (CultureInfo info in cultureInfos)
        {
            if (LocFileUtility.CheckIfLanguageFileExists(info.Name))
            {
                availableLanguages.Add(info);
            }
            else
            {
                notAvailableLanguages.Add(info);
                notAvailableLanguagesEnglishNames.Add(info.EnglishName);
            }
        }
        notAvailableLanguagesEnglishNames.Sort();
    }
示例#12
0
    /// <summary>
    /// Creates a new language. Gets the languagename and passes it over to LocFileUtility.CreateNewLanguage
    /// </summary>
    private void CreateNewLanguage(string englishName)
    {
        string languageName = "ERROR";

        //Find the chosen culture name
        foreach (CultureInfo info in notAvailableLanguages)
        {
            if (info.EnglishName == englishName)
            {
                languageName = info.Name;
                break;
            }
        }

        if (languageName == "ERROR")
        {
            Debug.Log("ERROR:Couldn't create the language: " + englishName);
            return;
        }
        LocFileUtility.CreateNewLanguage(languageName);

        //Update the available languages
        LocFileUtility.CheckAvailableLanguages(availableLanguages, notAvailableLanguages, notAvailableLanguagesEnglishNames);
    }
    public static void RefreshList(bool sort, LocalizedObjectType sortType)
    {
        if (!Application.isPlaying)
        {
            parsedRootValues.Clear();

            Dictionary <string, LocalizedObject> values = LocFileUtility.LoadParsedLanguageFile(null);
            if (sort)
            {
                loadedObjectType = sortType;
                foreach (KeyValuePair <string, LocalizedObject> pair in values)
                {
                    if (pair.Value.ObjectType == sortType)
                    {
                        parsedRootValues.Add(pair.Key);
                    }
                }
            }
            else
            {
                //Use invalid for showing all
                loadedObjectType = LocalizedObjectType.INVALID;

                parsedRootValues.AddRange(values.Keys);
            }

            if (parsedRootValues.Count > 0)
            {
                parsedRootValues.Insert(0, "--- No key selected ---");
            }
            else
            {
                parsedRootValues.Add("--- No localized keys available ---");
            }
        }
    }
示例#14
0
    /// <summary>
    /// Saves the root language file and updates all the available languages.
    /// </summary>
    public static void SaveRootLanguageFile(Dictionary <string, string> changedRootKeys, Dictionary <string, string> changedRootValues)
    {
        //The dictionary with all the final changes
        Dictionary <string, string> changedDictionary = new Dictionary <string, string>();

        foreach (KeyValuePair <string, string> changedKey in changedRootKeys)
        {
            if (changedKey.Key == changedKey.Value)
            {
                //The key is not changed, just add the key and the changed value to the new dictionary
                AddNewKeyPersistent(changedDictionary, changedKey.Key, changedRootValues[changedKey.Key]);
            }
            else
            {
                //Add the new key along with the new changed value
                AddNewKeyPersistent(changedDictionary, changedKey.Value, changedRootValues[changedKey.Key]);
            }
        }

        //Look if any keys were deleted,(so that we can delete the created files)
        //(Somewhat costly operation)
        List <string>        deletedKeys  = new List <string>();
        IEnumerable <string> originalKeys = LoadLanguageFile(null).Keys;

        foreach (string originalKey in originalKeys)
        {
            bool foundMatch = false;
            foreach (KeyValuePair <string, string> changedKey in changedRootKeys)
            {
                if (originalKey == changedKey.Key)
                {
                    foundMatch = true;
                    break;
                }
            }
            if (!foundMatch)
            {
                deletedKeys.Add(originalKey);
            }
        }

        //Save the language file
        SaveLanguageFile(changedDictionary, LocFileUtility.rootLanguageFilePath + LocFileUtility.resXFileEnding);

        //Change all the key values for all the translated languages
        Dictionary <string, string> changedCultureValues = new Dictionary <string, string>();
        List <CultureInfo>          availableLanguages   = GetAvailableLanguages();

        foreach (CultureInfo cultureInfo in availableLanguages)
        {
            Dictionary <string, string> currentCultureValues = LocFileUtility.LoadLanguageFile(cultureInfo.Name);
            foreach (KeyValuePair <string, string> changedKey in changedRootKeys)
            {
                string thisValue;
                currentCultureValues.TryGetValue(changedKey.Key, out thisValue);
                if (thisValue == null)
                {
                    thisValue = "";
                }

                //If the key is changed, we need to change the asset names as well
                if (changedKey.Key != changedKey.Value && thisValue != "")
                {
                    LocalizedObjectType originalType = LocalizedObject.GetLocalizedObjectType(changedKey.Key);
                    LocalizedObjectType changedType  = LocalizedObject.GetLocalizedObjectType(changedKey.Value);

                    if (originalType != changedType)
                    {
                        //If the type is changed, then delete the asset and reset the value
                        DeleteFileFromResources(changedKey.Key, cultureInfo);
                        thisValue = "";
                    }
                    else
                    {
                        //just rename it otherwise
                        RenameFileFromResources(changedKey.Key, changedKey.Value, cultureInfo);
                    }
                }

                AddNewKeyPersistent(changedCultureValues, changedKey.Value, thisValue);
            }

            //Save the language file
            SaveLanguageFile(changedCultureValues, LocFileUtility.rootLanguageFilePath + "." + cultureInfo.Name + LocFileUtility.resXFileEnding);
            changedCultureValues.Clear();

            //Remove all the deleted files associated with the deleted keys
            foreach (string deletedKey in deletedKeys)
            {
                DeleteFileFromResources(deletedKey, cultureInfo);
            }
        }
    }
示例#15
0
    void OnGUI()
    {
        if (EditorWindowUtility.ShowWindow())
        {
            if (smartLocWindow == null || thisCultureInfo == null)
            {
                this.Close();                // Temp fix
            }
            else if (!rootFileChanged)
            {
                undoManager.CheckUndo();
                GUILayout.Label("Language - " + thisLanguage, EditorStyles.boldLabel, GUILayout.Width(200));

                //Copy all the Base Values
                GUILayout.Label("If you want to copy all the base values from the root file", EditorStyles.miniLabel);
                if (GUILayout.Button("Copy All Base Values", GUILayout.Width(150)))
                {
                    int count = 0;
                    foreach (KeyValuePair <string, LocalizedObject> rootValue in rootValues)
                    {
                        if (rootValue.Value.ObjectType == LocalizedObjectType.STRING)
                        {
                            thisLanguageValues[count].changedValue.TextValue = rootValue.Value.TextValue;
                        }
                        count++;
                    }
                }

                GUILayout.Label("Microsoft Translator", EditorStyles.boldLabel);
                if (!smartLocWindow.MicrosoftTranslator.IsInitialized)
                {
                    GUILayout.Label("Microsoft Translator is not authenticated", EditorStyles.miniLabel);
                }
                else
                {
                    if (canLanguageBeTranslated)
                    {
                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Label("Translate From:", GUILayout.Width(100));
                        translateFromLanguageValue = EditorGUILayout.Popup(translateFromLanguageValue, availableTranslateLangEnglishNames);
                        EditorGUILayout.EndHorizontal();

                        if (oldTranslateFromLanguageValue != translateFromLanguageValue)
                        {
                            oldTranslateFromLanguageValue = translateFromLanguageValue;
                            //The value have been changed, load the language file of the other language that you want to translate from
                            //I load it like this to show the translate buttons only on the ones that can be translated i.e some values
                            //in the "from" language could be an empty string - no use in translating that
                            if (translateFromDictionary != null)
                            {
                                translateFromDictionary.Clear();
                                translateFromDictionary = null;
                            }
                            if (translateFromLanguageValue != 0)
                            {
                                string englishName = availableTranslateLangEnglishNames[translateFromLanguageValue];
                                foreach (CultureInfo info in availableTranslateFromLanguages)
                                {
                                    if (info.EnglishName == englishName)
                                    {
                                        translateFromDictionary = LocFileUtility.LoadParsedLanguageFile(info.Name);
                                        translateFromLanguage   = info.Name;
                                        break;
                                    }
                                }
                            }
                        }

                        //Translate all the available keys
                        if (translateFromLanguageValue != 0 && GUILayout.Button("Translate all text", GUILayout.Width(150)))
                        {
                            List <string> keys             = new List <string>();
                            List <string> textsToTranslate = new List <string>();
                            int           characterCount   = 0;
                            foreach (KeyValuePair <string, LocalizedObject> stringPair in translateFromDictionary)
                            {
                                if (stringPair.Value.ObjectType == LocalizedObjectType.STRING &&
                                    stringPair.Value.TextValue != null && stringPair.Value.TextValue != "")
                                {
                                    int textLength = stringPair.Value.TextValue.Length;
                                    //Microsoft translator only support translations below 1000 character
                                    //I'll cap it to 700, which gives 300 extra if the translated value is longer
                                    if (textLength < 700)
                                    {
                                        characterCount += textLength;
                                        keys.Add(stringPair.Key);
                                        textsToTranslate.Add(stringPair.Value.TextValue);
                                    }
                                }

                                //Microsoft translator only support translations with 100 array values and a total
                                // character cap of 10000,
                                // I'll cap it to 7000, which gives 3000 extra if the translated value is longer
                                if (keys.Count >= 99 || characterCount >= 7000)
                                {
                                    //Create a new reference to the list with keys, because we need it non-cleared in the callback
                                    List <string> keysToSend = new List <string>();
                                    keysToSend.AddRange(keysToSend.ToArray());

                                    //Send the values
                                    smartLocWindow.MicrosoftTranslator.TranslateArray(textsToTranslate, translateFromLanguage,
                                                                                      thisCultureInfo.Name, keysToSend, new TranslateCompleteArrayCallback(TranslatedTextArrayCompleted));

                                    //Reset values
                                    characterCount = 0;
                                    keys.Clear();
                                    textsToTranslate.Clear();
                                }
                            }
                            if (keys.Count != 0)
                            {
                                smartLocWindow.MicrosoftTranslator.TranslateArray(textsToTranslate, translateFromLanguage,
                                                                                  thisCultureInfo.Name, keys, new TranslateCompleteArrayCallback(TranslatedTextArrayCompleted));

                                //Reset values
                                characterCount = 0;
                                keys.Clear();
                                textsToTranslate.Clear();
                            }
                        }
                    }
                    else
                    {
                        GUILayout.Label(thisCultureInfo.EnglishName + " is not available for translation", EditorStyles.miniLabel);
                    }
                }

                GUILayout.Label("Language Values", EditorStyles.boldLabel);
                //Search field
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Search for Key:", GUILayout.Width(100));
                searchText = EditorGUILayout.TextField(searchText);
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Key", EditorStyles.boldLabel, GUILayout.Width(120));
                GUILayout.Label("Base Value", EditorStyles.boldLabel, GUILayout.Width(120));
                GUILayout.Label("Copy Base", EditorStyles.miniLabel, GUILayout.Width(70));
                if (canLanguageBeTranslated)
                {
                    //TODO::Change to small picture
                    GUILayout.Label("T", EditorStyles.miniLabel, GUILayout.Width(20));
                }
                GUILayout.Label(thisLanguage + " Value", EditorStyles.boldLabel);
                EditorGUILayout.EndHorizontal();

                //Check if the user searched for a value
                bool didSearch = false;
                if (searchText != "")
                {
                    didSearch = true;
                    GUILayout.Label("Search Results - \"" + searchText + "\":", EditorStyles.boldLabel);
                }

                //Start the scroll view
                scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
                int iterationCount = 0;
                foreach (KeyValuePair <string, LocalizedObject> rootValue in rootValues)
                {
                    if (didSearch)
                    {
                        //If the name of the key doesn't contain the search value, then skip a value
                        if (!rootValue.Key.ToLower().Contains(searchText.ToLower()))
                        {
                            continue;
                        }
                    }

                    if (rootValue.Value.ObjectType == LocalizedObjectType.STRING)
                    {
                        OnTextFieldGUI(rootValue, iterationCount);
                    }
                    else if (rootValue.Value.ObjectType == LocalizedObjectType.AUDIO)
                    {
                        OnAudioGUI(rootValue, iterationCount);
                    }
                    else if (rootValue.Value.ObjectType == LocalizedObjectType.GAME_OBJECT)
                    {
                        OnGameObjectGUI(rootValue, iterationCount);
                    }
                    else if (rootValue.Value.ObjectType == LocalizedObjectType.TEXTURE)
                    {
                        OnTextureGUI(rootValue, iterationCount);
                    }

                    iterationCount++;
                }
                //End the scroll view
                EditorGUILayout.EndScrollView();

                if (guiChanged)
                {
                    GUILayout.Label("- You have unsaved changes", EditorStyles.miniLabel);
                }

                //If any changes to the gui is made
                if (GUI.changed)
                {
                    guiChanged = true;
                }

                GUILayout.Label("Save Changes", EditorStyles.boldLabel);
                GUILayout.Label("Remember to always press save when you have changed values", EditorStyles.miniLabel);
                if (GUILayout.Button("Save/Rebuild"))
                {
                    //Copy everything into a dictionary
                    Dictionary <string, string> newLanguageValues = new Dictionary <string, string>();
                    foreach (SerializableLocalizationObjectPair objectPair in this.thisLanguageValues)
                    {
                        if (objectPair.changedValue.ObjectType == LocalizedObjectType.STRING)
                        {
                            newLanguageValues.Add(objectPair.changedValue.GetFullKey(objectPair.keyValue), objectPair.changedValue.TextValue);
                        }
                        else
                        {
                            //Delete the file in case there was a file there previously
                            LocFileUtility.DeleteFileFromResources(objectPair.changedValue.GetFullKey(objectPair.keyValue), thisCultureInfo);

                            //Store the path to the file
                            string pathValue = LocFileUtility.CopyFileIntoResources(objectPair, thisCultureInfo);
                            newLanguageValues.Add(objectPair.changedValue.GetFullKey(objectPair.keyValue), pathValue);
                        }
                    }
                    LocFileUtility.SaveLanguageFile(newLanguageValues, LocFileUtility.rootLanguageFilePath + "." + thisCultureInfo.Name + LocFileUtility.resXFileEnding);
                    guiChanged = false;
                }

                undoManager.CheckDirty();
            }
            else
            {
                //The root file did change, which means that you have to reload. A key might have changed
                //We can't have language files with different keys
                GUILayout.Label("The root file might have changed", EditorStyles.boldLabel);
                GUILayout.Label("The root file did save, which means that you have to reload. A key might have changed.", EditorStyles.miniLabel);
                GUILayout.Label("You can't have language files with different keys", EditorStyles.miniLabel);
                if (GUILayout.Button("Reload Language File"))
                {
                    InitializeLanguage(thisCultureInfo, LocFileUtility.LoadParsedLanguageFile(null), LocFileUtility.LoadParsedLanguageFile(thisCultureInfo.Name));
                }
            }
        }
    }
示例#16
0
    void OnGUI()
    {
        if (EditorWindowUtility.ShowWindow())
        {
            GUILayout.Label("Settings", EditorStyles.boldLabel);
            if (!LocFileUtility.CheckIfRootLanguageFileExists())
            {
                if (GUILayout.Button("Create New Localization System"))
                {
                    LocFileUtility.CreateRootResourceFile();
                }
            }
            else
            {
                undoManager.CheckUndo();
                if (GUILayout.Button("Refresh"))
                {
                    LocFileUtility.CheckAvailableLanguages(availableLanguages, notAvailableLanguages, notAvailableLanguagesEnglishNames);
                }
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Microsoft Translator Settings", EditorStyles.boldLabel, GUILayout.Width(200));
                if (microsoftTranslator.IsInitialized)
                {
                    GUILayout.Label(" - Authenticated!", EditorStyles.miniLabel);
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Client ID:", GUILayout.Width(70));
                mtCliendID = EditorGUILayout.TextField(mtCliendID);
                GUILayout.Label("Client Secret:", GUILayout.Width(100));
                mtCliendSecret = EditorGUILayout.TextField(mtCliendSecret);
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Save", GUILayout.Width(50)))
                {
                    SaveMicrosoftTranslatorSettings();
                    if (!microsoftTranslator.IsInitialized)
                    {
                        microsoftTranslator.GetAccessToken(mtCliendID, mtCliendSecret);
                    }
                }
                if (!microsoftTranslator.IsInitialized)
                {
                    if (GUILayout.Button("Authenticate!", GUILayout.Width(150)))
                    {
                        microsoftTranslator.GetAccessToken(mtCliendID, mtCliendSecret);
                    }
                }
                keepTranslatorAuthenticated = EditorGUILayout.Toggle("Keep Authenticated", keepTranslatorAuthenticated);
                EditorGUILayout.EndHorizontal();

                GUILayout.Label("Edit Root Language File", EditorStyles.boldLabel);
                if (GUILayout.Button("Edit"))
                {
                    ShowRootEditWindow(LocFileUtility.LoadParsedLanguageFile(null));
                }

                GUILayout.Label("Create new language", EditorStyles.boldLabel);
                chosenCreateNewCultureValue = EditorGUILayout.Popup(chosenCreateNewCultureValue, notAvailableLanguagesEnglishNames.ToArray());
                if (GUILayout.Button("Create Language"))
                {
                    CreateNewLanguage(notAvailableLanguagesEnglishNames[chosenCreateNewCultureValue]);
                }

                GUILayout.Label("Translate Languages", EditorStyles.boldLabel);
                //Start the scroll view
                scrollPosition = GUILayout.BeginScrollView(scrollPosition);
                bool languageDeleted = false;
                foreach (CultureInfo info in availableLanguages)
                {
                    EditorGUILayout.BeginHorizontal();
                    if (GUILayout.Button(info.EnglishName + " - " + info.Name))
                    {
                        //Open language edit window
                        ShowTranslateWindow(info);
                    }
                    if (GUILayout.Button("Delete", GUILayout.Width(60)))
                    {
                        LocFileUtility.DeleteLanguage(info);
                        languageDeleted = true;
                        break;
                    }
                    EditorGUILayout.EndHorizontal();
                }

                if (languageDeleted)                //Refresh
                {
                    LocFileUtility.CheckAvailableLanguages(availableLanguages, notAvailableLanguages, notAvailableLanguagesEnglishNames);
                }

                //End the scroll view
                GUILayout.EndScrollView();
                undoManager.CheckDirty();
            }
        }
    }
    void OnGUI()
    {
        if (EditorWindowUtility.ShowWindow())
        {
            undoManager.CheckUndo();
            GUILayout.Label("Root Values", EditorStyles.boldLabel);

            //Search field
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Search for Key:", GUILayout.Width(100));
            searchText = EditorGUILayout.TextField(searchText);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Key Type", GUILayout.Width(100));
            GUILayout.Label("Key");
            GUILayout.Label("Base Value/Comment");
            GUILayout.Label("Delete", EditorStyles.miniLabel, GUILayout.Width(50));
            EditorGUILayout.EndHorizontal();

            //Create the scroll view
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

            //Delete key information
            bool deleteKey     = false;
            int  indexToDelete = 0;

            //Check if the user searched for a value
            bool didSearch = false;
            if (searchText != "")
            {
                didSearch = true;
                GUILayout.Label("Search Results - \"" + searchText + "\":", EditorStyles.boldLabel);
            }

            for (int i = 0; i < changedRootKeys.Count; i++)
            {
                SerializableStringPair rootValue = changedRootKeys[i];
                if (didSearch)
                {
                    //If the name of the key doesn't contain the search value, then skip a value
                    if (!rootValue.originalValue.ToLower().Contains(searchText.ToLower()))
                    {
                        continue;
                    }
                }
                EditorGUILayout.BeginHorizontal();

                //Popup of all the different key values
                changedRootValues[i].changedValue.ObjectType = (LocalizedObjectType)EditorGUILayout.Popup((int)changedRootValues[i].changedValue.ObjectType,
                                                                                                          keyTypes, GUILayout.Width(100));

                rootValue.changedValue = EditorGUILayout.TextField(rootValue.changedValue);
                changedRootValues[i].changedValue.TextValue = EditorGUILayout.TextField(changedRootValues[i].changedValue.TextValue);
                if (GUILayout.Button("Delete", GUILayout.Width(50)))
                {
                    deleteKey     = true;
                    indexToDelete = i;
                }

                EditorGUILayout.EndHorizontal();
            }

            //End the scrollview
            EditorGUILayout.EndScrollView();

            if (GUILayout.Button("Add New Key"))
            {
                AddNewKey();
            }

            //Delete the key outside the foreach loop
            if (deleteKey)
            {
                DeleteKey(indexToDelete);
            }

            if (guiChanged)
            {
                GUILayout.Label("- You have unsaved changes", EditorStyles.miniLabel);
            }

            //If any changes to the gui is made
            if (GUI.changed)
            {
                guiChanged = true;
            }

            GUILayout.Label("Save Changes", EditorStyles.boldLabel);
            GUILayout.Label("Remember to always press save when you have changed values", EditorStyles.miniLabel);
            if (GUILayout.Button("Save Root Language File"))
            {
                Dictionary <string, string> changeNewRootKeys   = new Dictionary <string, string>();
                Dictionary <string, string> changeNewRootValues = new Dictionary <string, string>();

                for (int i = 0; i < changedRootKeys.Count; i++)
                {
                    SerializableStringPair             rootKey   = changedRootKeys[i];
                    SerializableLocalizationObjectPair rootValue = changedRootValues[i];
                    //Check for possible duplicates and rename them
                    string newKeyValue = LocFileUtility.AddNewKeyPersistent(changeNewRootKeys, rootKey.originalValue, rootValue.changedValue.GetFullKey(rootKey.changedValue));

                    //Check for possible duplicates and rename them(same as above)
                    LocFileUtility.AddNewKeyPersistent(changeNewRootValues, newKeyValue, rootValue.changedValue.TextValue);
                }

                //Add the full values before saving
                Dictionary <string, string> changeNewRootKeysToSave   = new Dictionary <string, string>();
                Dictionary <string, string> changeNewRootValuesToSave = new Dictionary <string, string>();

                foreach (KeyValuePair <string, string> rootKey in changeNewRootKeys)
                {
                    LocalizedObject thisLocalizedObject = parsedRootValues[rootKey.Key];
                    changeNewRootKeysToSave.Add(thisLocalizedObject.GetFullKey(rootKey.Key), rootKey.Value);
                    changeNewRootValuesToSave.Add(thisLocalizedObject.GetFullKey(rootKey.Key), changeNewRootValues[rootKey.Key]);
                }

                LocFileUtility.SaveRootLanguageFile(changeNewRootKeysToSave, changeNewRootValuesToSave);

                //Fire the root language changed event
                if (OnRootFileChanged != null)
                {
                    OnRootFileChanged();
                }

                //Reload the window(in case of duplicate keys)
                SetRootValues(LocFileUtility.LoadParsedLanguageFile(null));
                guiChanged = false;
            }

            undoManager.CheckDirty();
        }
    }