SaveLanguageFile() public static method

Saves a language file(.resx) at the specified path containing the values in the languageValueDictionary
public static SaveLanguageFile ( string>.Dictionary languageValueDictionary, string filePath ) : void
languageValueDictionary string>.Dictionary /// Language value dictionary. ///
filePath string /// File path. ///
return void
Beispiel #1
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);
    }
Beispiel #2
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);
    }
    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));
                }
            }
        }
    }