Beispiel #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("");
        }
    }
 /// <summary>
 /// Callback when the translated text is translated
 /// </summary>
 /// <param name='key'>
 /// The key of the translated value
 /// </param>
 /// <param name='translatedValue'>
 /// Translated value.
 /// </param>
 public void TranslatedTextCompleted(string key, string translatedValue)
 {
     for (int i = 0; i < thisLanguageValues.Count; i++)
     {
         SerializableLocalizationObjectPair objectPair = thisLanguageValues[i];
         if (objectPair.keyValue == key)
         {
             objectPair.changedValue.TextValue = translatedValue;
             break;
         }
     }
 }
 /// <summary>
 /// Callback when the translated array of texts is completed
 /// </summary>
 /// <param name='keys'>
 /// Keys.
 /// </param>
 /// <param name='translatedValues'>
 /// Translated values.
 /// </param>
 public void TranslatedTextArrayCompleted(List <string> keys, List <string> translatedValues)
 {
     for (int j = 0; j < keys.Count; j++)
     {
         for (int i = 0; i < thisLanguageValues.Count; i++)
         {
             SerializableLocalizationObjectPair objectPair = thisLanguageValues[i];
             if (objectPair.keyValue == keys[j])
             {
                 objectPair.changedValue.TextValue = translatedValues[j];
                 break;
             }
         }
     }
 }
    /// <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 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();
        }
    }