/// <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 TestCreateNewRootKeys() { // Show root language editor window and get root values EditRootLanguageFileWindow window = EditRootLanguageFileWindow.ShowWindow(); Dictionary <string, LocalizedObject> dict = LanguageHandlerEditor.LoadParsedLanguageFile(string.Empty, true); // Add any missing TEXT.RSC records DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (dfUnity.IsReady) { TextResourceFile rscFile = new TextResourceFile(dfUnity.Arena2Path, "TEXT.RSC"); for (int i = 0; i < rscFile.RecordCount; i++) { TextResourceFile.TextRecord record = rscFile.GetTextRecordByIndex(i); string key = RSCNamespacePrefix + record.id.ToString(); if (!dict.ContainsKey(key)) { LocalizedObject obj = new LocalizedObject(); obj.ObjectType = LocalizedObjectType.STRING; obj.TextValue = record.text; LanguageDictionaryHelper.AddNewKeyPersistent(dict, key, obj); } } } //// Add new root keys and values //LocalizedObject obj = new LocalizedObject(); //obj.ObjectType = LocalizedObjectType.STRING; //obj.TextValue = "This is a new text key."; //LanguageDictionaryHelper.AddNewKeyPersistent(dict, "NewKey.Test", obj); // Set new root values window.SetRootValues(dict); }
/// <summary> /// Adds a new key to a dictionary<string,string> and does not stop until a unique key is found /// </summary> public static string AddNewKeyPersistent(Dictionary <string, string> thisDictionary, string desiredKey, string newValue) { LocalizedObjectType thisKeyType = LocalizedObject.GetLocalizedObjectType(desiredKey); //Clean the key from unwanted type identifiers //Nothing will happen to a regular string, since a string doesn't have an identifier desiredKey = LocalizedObject.GetCleanKey(desiredKey, thisKeyType); if (!thisDictionary.ContainsKey(desiredKey) && thisKeyType == LocalizedObjectType.STRING) { thisDictionary.Add(desiredKey, newValue); return(desiredKey); } else { bool newKeyFound = false; int count = 0; string newKeyName = desiredKey; while (!newKeyFound) { if (!thisDictionary.ContainsKey(newKeyName)) { bool duplicateFound = false; foreach (KeyValuePair <string, string> stringPair in thisDictionary) { string cleanKey = LocalizedObject.GetCleanKey(stringPair.Key); if (cleanKey == newKeyName) { duplicateFound = true; break; } } if (!duplicateFound) { thisDictionary.Add(LocalizedObject.GetFullKey(newKeyName, thisKeyType), newValue); newKeyFound = true; return(desiredKey); } else { newKeyName = desiredKey + count; count++; } } else { newKeyName = desiredKey + count; count++; } } Debug.Log("Duplicate keys in dictionary was found! - renaming key:" + desiredKey + " to:" + newKeyName); return(newKeyName); } }
/// <summary> /// Gets the audio clip for the current language, returns null if nothing is found /// </summary> /// <returns> /// The audio clip. Null if nothing is found /// </returns> /// <param name='key'> /// Key. /// </param> public AudioClip GetAudioClip(string key) { LocalizedObject thisObject = GetLocalizedObject(key); if (thisObject != null) { return(Resources.Load("Localization/" + language + "/Audio Files/" + key) as AudioClip); } return(null); }
/// <summary> /// Gets the texture for the current language, returns null if nothing is found /// </summary> /// <returns> /// The loaded prefab. Null if nothing is found /// </returns> /// <param name='key'> /// Key. /// </param> public Texture GetTexture(string key) { LocalizedObject thisObject = GetLocalizedObject(key); if (thisObject != null) { return(Resources.Load("Localization/" + language + "/Textures/" + key) as Texture); } return(null); }
/// <summary> /// Gets the prefab game object for the current language, returns null if nothing is found /// </summary> /// <returns> /// The loaded prefab. Null if nothing is found /// </returns> /// <param name='key'> /// Key. /// </param> public GameObject GetPrefab(string key) { LocalizedObject thisObject = GetLocalizedObject(key); if (thisObject != null) { return(Resources.Load("Localization/" + language + "/Prefabs/" + key) as GameObject); } return(null); }
/// <summary> /// Returns a text value in the current language for the key. Returns null if nothing is found. /// </summary> /// <returns> /// The value in the specified language, returns null if nothing is found /// </returns> /// <param name='key'> /// The Language Key. /// </param> public string GetTextValue(string key) { LocalizedObject thisObject = GetLocalizedObject(key); if (thisObject != null) { return(thisObject.TextValue); } Debug.LogError("LanguageManager.cs: Invalid Key:" + key + "for language: " + language); return(null); }
string GetTestData(string testKey, string testValue, LocalizedObjectType objectType) { StringBuilder resxData = new StringBuilder(resxTemplateDataStart); resxData.Append("<data name=\""); resxData.Append(LocalizedObject.GetFullKey(testKey, objectType)); resxData.Append("\" xml:space=\"preserve\">\n"); resxData.Append("<value>"); resxData.Append(testValue); resxData.Append("</value>\n"); resxData.Append("</data>\n"); resxData.Append(resxTemplateDataEnd); return(resxData.ToString()); }
/// <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)); }
private void propertyGrid_PropertyValueChanged(object s, System.Windows.Forms.PropertyValueChangedEventArgs e) { if (OnPropertyValueChanged != null) { OnPropertyValueChanged(this, e); } object selObj = SelectedObject; if (selObj != null) { LocalizedObject lo = selObj as LocalizedObject; if (lo != null) { app.DataChanged(lo.Object); } } }
void OnKeyAdded(int addedIndex) { LocalizedObject dummyObject = new LocalizedObject(); dummyObject.ObjectType = LocalizedObjectType.STRING; dummyObject.TextValue = "New Value"; string addedKey = LanguageDictionaryHelper.AddNewKeyPersistent(parsedRootValues, "New Key", dummyObject); LocalizedObject copyObject = new LocalizedObject(); copyObject.ObjectType = LocalizedObjectType.STRING; copyObject.TextValue = "New Value"; changedRootKeys.Add(new SerializableStringPair(addedKey, addedKey)); changedRootValues[addedIndex] = new SerializableLocalizationObjectPair(addedKey, copyObject); }
/// <summary> /// Reads a specific data tag from the xml document. /// </summary> /// <param name='reader'> /// Reader. /// </param> private void ReadData(XmlReader reader) { //If these values are not being set, //something is wrong. string key = "ERROR"; string value = "ERROR"; if (reader.HasAttributes) { while (reader.MoveToNextAttribute()) { if (reader.Name == "name") { key = reader.Value; } } } //Move back to the element reader.MoveToElement(); //Read the child nodes if (reader.ReadToDescendant("value")) { do { //value = reader.ReadString(); value = reader.ReadElementContentAsString(); if (reader.Name.Equals("data")) { break; } }while (reader.ReadToNextSibling("value")); } //Add the raw values to the dictionary textDataBase.Add(key, value); //Add the localized parsed values to the localizedObjectDataBase LocalizedObject newLocalizedObject = new LocalizedObject(); newLocalizedObject.ObjectType = LocalizedObject.GetLocalizedObjectType(key); newLocalizedObject.TextValue = value; localizedObjectDataBase.Add(LocalizedObject.GetCleanKey(key, newLocalizedObject.ObjectType), newLocalizedObject); }
void SaveRootLanguageFile() { var changeNewRootKeys = new Dictionary <string, string>(); var 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 = LanguageDictionaryHelper.AddNewKeyPersistent(changeNewRootKeys, rootKey.originalValue, rootValue.changedValue.GetFullKey(rootKey.changedValue)); //Check for possible duplicates and rename them(same as above) LanguageDictionaryHelper.AddNewKeyPersistent(changeNewRootValues, newKeyValue, rootValue.changedValue.TextValue); } //Add the full values before saving var changeNewRootKeysToSave = new Dictionary <string, string>(); var changeNewRootValuesToSave = new Dictionary <string, string>(); foreach (var 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]); } SmartCultureInfoCollection allCultures = SmartCultureInfoEx.Deserialize(LocalizationWorkspace.CultureInfoCollectionFilePath()); LanguageHandlerEditor.SaveRootLanguageFile(changeNewRootKeysToSave, changeNewRootValuesToSave, LanguageHandlerEditor.CheckAndSaveAvailableLanguages(allCultures)); //Fire the root language changed event if (OnRootFileChanged != null) { OnRootFileChanged(); } //Reload the window(in case of duplicate keys) SetRootValues(LanguageHandlerEditor.LoadParsedLanguageFile(null, true)); }
/// <summary> /// Sets the dictionaries necessary to change them within the extension /// </summary> /// <param name='rootValues'> /// Root values. /// </param> public void SetRootValues(Dictionary <string, LocalizedObject> rootValues) { changedRootValues.Clear(); changedRootKeys.Clear(); parsedRootValues.Clear(); foreach (var rootValue in rootValues) { changedRootKeys.Add(new SerializableStringPair(rootValue.Key, rootValue.Key)); changedRootValues.Add(new SerializableLocalizationObjectPair(rootValue.Key, rootValue.Value)); LocalizedObject copyObject = new LocalizedObject(); copyObject.ObjectType = rootValue.Value.ObjectType; copyObject.TextValue = rootValue.Value.TextValue; parsedRootValues.Add(rootValue.Key, copyObject); } searchText = ""; }
/// <summary>Renames the localized file from resources.</summary> public static void RenameFileFromResources(string key, string newKey, SmartCultureInfo cultureInfo) { string languageFolderPath = null; LocalizedObjectType keyType = LocalizedObject.GetLocalizedObjectType(key); string cleanKey = LocalizedObject.GetCleanKey(key); string cleanNewKey = LocalizedObject.GetCleanKey(newKey); switch (keyType) { case LocalizedObjectType.GAME_OBJECT: languageFolderPath = LocalizationWorkspace.LanguagePrefabsFolderPathRelative(cultureInfo.languageCode) + "/" + cleanKey + LocalizationWorkspace.prefabFileEnding; break; case LocalizedObjectType.AUDIO: languageFolderPath = LocalizationWorkspace.LanguageAudioFolderPathRelative(cultureInfo.languageCode); break; case LocalizedObjectType.TEXTURE: languageFolderPath = LocalizationWorkspace.LanguageTexturesFolderPathRelative(cultureInfo.languageCode); break; case LocalizedObjectType.TEXT_ASSET: languageFolderPath = LocalizationWorkspace.LanguageTextAssetsFolderPathRelative(cultureInfo.languageCode); break; case LocalizedObjectType.FONT: languageFolderPath = LocalizationWorkspace.LanguageFontsFolderPathRelative(cultureInfo.languageCode); break; } if (keyType != LocalizedObjectType.GAME_OBJECT) { string fileExtension = FileUtility.GetFileExtension(cleanKey, languageFolderPath); languageFolderPath += "/" + cleanKey + fileExtension; } if (FileUtility.Exists(Application.dataPath + languageFolderPath)) { AssetDatabase.RenameAsset("Assets" + languageFolderPath, cleanNewKey); } AssetDatabase.Refresh(); }
/// <summary> Deletes the localized file from resources.</summary> public static void DeleteFileFromResources(string key, SmartCultureInfo cultureInfo) { string languageFolderPath = string.Empty; string cleanKey = LocalizedObject.GetCleanKey(key); LocalizedObjectType keyType = LocalizedObject.GetLocalizedObjectType(key); switch (keyType) { case LocalizedObjectType.GameObject: languageFolderPath = LocalizationWorkspace.LanguagePrefabsFolderPathRelative(cultureInfo.languageCode) + "/" + cleanKey + LocalizationWorkspace.prefabFileEnding; break; case LocalizedObjectType.Audio: languageFolderPath = LocalizationWorkspace.LanguageAudioFolderPathRelative(cultureInfo.languageCode); break; case LocalizedObjectType.Texture: languageFolderPath = LocalizationWorkspace.LanguageTexturesFolderPathRelative(cultureInfo.languageCode); break; case LocalizedObjectType.TextAsset: languageFolderPath = LocalizationWorkspace.LanguageTextAssetsFolderPathRelative(cultureInfo.languageCode); break; case LocalizedObjectType.Font: languageFolderPath = LocalizationWorkspace.LanguageFontsFolderPathRelative(cultureInfo.languageCode); break; } if (keyType != LocalizedObjectType.GameObject) { string fileExtension = FileUtility.GetFileExtension(cleanKey, languageFolderPath); languageFolderPath += "/" + cleanKey + fileExtension; } if (FileUtility.Exists(Application.dataPath + languageFolderPath)) { AssetDatabase.DeleteAsset("Assets" + languageFolderPath); } AssetDatabase.Refresh(); }
public void AddToStringDictionary_UniqueNames() { Dictionary <string, string> testDictionary = new Dictionary <string, string>(); string tempKey = "TestKey"; string tempValue = "temp"; LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.String), tempValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.Audio), tempValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.GameObject), tempValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.Texture), tempValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.String), tempValue); }
public void AddToStringDictionary_UniqueNames() { Dictionary <string, string> testDictionary = new Dictionary <string, string>(); string tempKey = "TestKey"; string tempValue = "temp"; LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.STRING), tempValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.AUDIO), tempValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.GAME_OBJECT), tempValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.TEXTURE), tempValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, LocalizedObject.GetFullKey(tempKey, LocalizedObjectType.STRING), tempValue); }
public void AddKey(string key, string value) { if (parsedRootValues.ContainsKey(key)) { return; } LocalizedObject dummyObject = new LocalizedObject(); dummyObject.ObjectType = LocalizedObjectType.STRING; dummyObject.TextValue = key; string addedKey = LanguageDictionaryHelper.AddNewKeyPersistent(parsedRootValues, key, dummyObject); LocalizedObject copyObject = new LocalizedObject(); copyObject.ObjectType = LocalizedObjectType.STRING; copyObject.TextValue = value; changedRootKeys.Add(new SerializableStringPair(addedKey, addedKey)); changedRootValues.Add(new SerializableLocalizationObjectPair(addedKey, copyObject)); }
/// <summary> /// Renames the localized file from resources. /// </summary> /// <param name='key'> /// Key. /// </param> /// <param name='thisCultureInfo'> /// This culture info. /// </param> public static void RenameFileFromResources(string key, string newKey, CultureInfo thisCultureInfo) { string languageFolderPath = "/SmartLocalizationLanguage/Resources/Localization/" + thisCultureInfo.Name; LocalizedObjectType thisKeyType = LocalizedObject.GetLocalizedObjectType(key); string cleanKey = LocalizedObject.GetCleanKey(key); string cleanNewKey = LocalizedObject.GetCleanKey(newKey); if (thisKeyType == LocalizedObjectType.GAME_OBJECT) { languageFolderPath += "/Prefabs/" + cleanKey + ".prefab"; if (CheckIfFileExists(Application.dataPath + languageFolderPath)) { AssetDatabase.RenameAsset("Assets" + languageFolderPath, cleanNewKey); } } else if (thisKeyType == LocalizedObjectType.AUDIO) { languageFolderPath += "/Audio Files/"; string fileExtension = GetFileExtension(cleanKey, languageFolderPath); languageFolderPath += cleanKey + fileExtension; if (CheckIfFileExists(Application.dataPath + languageFolderPath)) { AssetDatabase.RenameAsset("Assets" + languageFolderPath, cleanNewKey); } } else if (thisKeyType == LocalizedObjectType.TEXTURE) { languageFolderPath += "/Textures/"; string fileExtension = GetFileExtension(cleanKey, languageFolderPath); languageFolderPath += cleanKey + fileExtension; if (CheckIfFileExists(Application.dataPath + languageFolderPath)) { AssetDatabase.RenameAsset("Assets" + languageFolderPath, cleanNewKey); } } AssetDatabase.Refresh(); }
public void AddToObjectDictionary_UniqueNames() { var testDictionary = new Dictionary <string, LocalizedObject>(); string tempKey = "TestKey"; LocalizedObject tempStringValue = new LocalizedObject(); tempStringValue.ObjectType = LocalizedObjectType.STRING; tempStringValue.TextValue = "TEMP"; LocalizedObject tempAudioValue = new LocalizedObject(tempStringValue); tempAudioValue.ObjectType = LocalizedObjectType.AUDIO; LocalizedObject tempPrefabValue = new LocalizedObject(tempStringValue); tempPrefabValue.ObjectType = LocalizedObjectType.GAME_OBJECT; LocalizedObject tempTextureValue = new LocalizedObject(tempStringValue); tempTextureValue.ObjectType = LocalizedObjectType.TEXTURE; LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, tempKey, tempStringValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, tempKey, new LocalizedObject(tempStringValue)); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, tempKey, tempAudioValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, tempKey, tempPrefabValue); LanguageDictionaryHelper.AddNewKeyPersistent(testDictionary, tempKey, tempTextureValue); }
/// <summary> /// Sets the dictionaries necessary to change them within the extension /// </summary> /// <param name='rootValues'> /// Root values. /// </param> public void SetRootValues(Dictionary<string, LocalizedObject> rootValues) { changedRootValues.Clear(); changedRootKeys.Clear(); parsedRootValues.Clear(); foreach(KeyValuePair<string,LocalizedObject> rootValue in rootValues) { changedRootKeys.Add(new SerializableStringPair(rootValue.Key, rootValue.Key)); changedRootValues.Add(new SerializableLocalizationObjectPair(rootValue.Key, rootValue.Value)); LocalizedObject copyObject = new LocalizedObject(); copyObject.ObjectType = rootValue.Value.ObjectType; copyObject.TextValue = rootValue.Value.TextValue; parsedRootValues.Add(rootValue.Key, copyObject); } searchText = ""; }
/// <summary> Copies the file into the resources folder. Naming the new asset to KEY </summary> public static string CopyFileIntoResources(SerializableLocalizationObjectPair objectPair, SmartCultureInfo thisCultureInfo) { if(!DirectoryUtility.CheckAndCreate(LocalizationWorkspace.LanguageRuntimeFolderPath(thisCultureInfo.languageCode))) { return ""; } string newFileName = objectPair.keyValue; string filePath = string.Empty; string currentAssetPath = string.Empty; LocalizedObject objectToCopy = objectPair.changedValue; if(objectToCopy.ObjectType == LocalizedObjectType.AUDIO && objectToCopy.ThisAudioClip != null) { filePath = LocalizationWorkspace.LanguageAudioFolderPath(thisCultureInfo.languageCode); currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisAudioClip); } else if(objectToCopy.ObjectType == LocalizedObjectType.TEXTURE && objectToCopy.ThisTexture != null) { filePath = LocalizationWorkspace.LanguageTexturesFolderPath(thisCultureInfo.languageCode); currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisTexture); } else if(objectToCopy.ObjectType == LocalizedObjectType.GAME_OBJECT && objectToCopy.ThisGameObject != null) { filePath = LocalizationWorkspace.LanguagePrefabsFolderPath(thisCultureInfo.languageCode); currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisGameObject); } else if(objectToCopy.ObjectType == LocalizedObjectType.TEXT_ASSET && objectToCopy.ThisTextAsset != null) { filePath = LocalizationWorkspace.LanguageTextAssetsFolderPath(thisCultureInfo.languageCode); currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.ThisTextAsset); } else if(objectToCopy.ObjectType == LocalizedObjectType.FONT && objectToCopy.Font != null) { filePath = LocalizationWorkspace.LanguageFontsFolderPath(thisCultureInfo.languageCode); currentAssetPath = AssetDatabase.GetAssetPath(objectToCopy.Font); } else { return string.Empty; } if(!DirectoryUtility.CheckAndCreate(filePath)) { return ""; } //Get the fileExtension of the asset string fileExtension = FileUtility.GetFileExtension(Application.dataPath + currentAssetPath); if(objectToCopy.ObjectType != LocalizedObjectType.GAME_OBJECT){ //Copy or replace the file to the new path FileUtil.ReplaceFile(currentAssetPath, filePath + "/" + newFileName + fileExtension); string metaFile = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length) + currentAssetPath.Substring(0, currentAssetPath.Length - fileExtension.Length) + fileExtension + ".meta"; if(File.Exists(metaFile)) { FileUtil.ReplaceFile(metaFile, filePath + "/" + newFileName + fileExtension + ".meta"); } } else{ string relativePath = filePath + "/" + newFileName + fileExtension; relativePath = "Assets" + relativePath.Substring(Application.dataPath.Length); PrefabUtility.CreatePrefab(relativePath, objectToCopy.ThisGameObject); } return AssetDatabase.AssetPathToGUID(currentAssetPath); }
/// <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, SmartCultureInfoCollection availableCultures) { //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 LanguageDictionaryHelper.AddNewKeyPersistent(changedDictionary, changedKey.Key, changedRootValues[changedKey.Key]); } else { //Add the new key along with the new changed value LanguageDictionaryHelper.AddNewKeyPersistent(changedDictionary, changedKey.Value, changedRootValues[changedKey.Key]); } } //Look if any keys were deleted,(so that we can delete the created files) List<string> deletedKeys = new List<string>(); IEnumerable<string> originalKeys = LoadLanguageFile(null, true).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, LocalizationWorkspace.RootLanguageFilePath()); //Change all the key values for all the translated languages var changedCultureValues = new Dictionary<string, string>(); foreach(var cultureInfo in availableCultures.cultureInfos) { var currentCultureValues = LoadLanguageFile(cultureInfo.languageCode, false); foreach(var changedKey in changedRootKeys) { string currentValue; currentCultureValues.TryGetValue(changedKey.Key, out currentValue); if(currentValue == null) { currentValue = ""; } //If the key is changed, we need to change the asset names as well if(changedKey.Key != changedKey.Value && currentValue != "") { 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); currentValue = ""; } else { //just rename it otherwise RenameFileFromResources(changedKey.Key, changedKey.Value, cultureInfo); } } LanguageDictionaryHelper.AddNewKeyPersistent(changedCultureValues, changedKey.Value, currentValue); } //Save the language file SaveLanguageFile (changedCultureValues, LocalizationWorkspace.LanguageFilePath(cultureInfo.languageCode)); changedCultureValues.Clear(); //Remove all the deleted files associated with the deleted keys foreach(string deletedKey in deletedKeys) { Debug.Log("Deleted key!:" + deletedKey); DeleteFileFromResources(deletedKey, cultureInfo); } } }
/// <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); } } }
public SerializableLocalizationObjectPair(string keyValue, LocalizedObject changedValue) { this.keyValue = keyValue; this.changedValue = changedValue; }
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(); } }
/// <summary> /// Adds a new key to a dictionary<string,LocalizedObject> and does not stop until a unique key is found /// </summary> public static string AddNewKeyPersistent(Dictionary<string, LocalizedObject> languageDictionary, string desiredKey, LocalizedObject newValue) { if(!languageDictionary.ContainsKey(desiredKey)) { languageDictionary.Add(desiredKey, newValue); return desiredKey; } else { bool newKeyFound = false; int count = 0; while(!newKeyFound) { if(!languageDictionary.ContainsKey(desiredKey + count)) { languageDictionary.Add(desiredKey + count, newValue); newKeyFound = true; } else { count++; } } Debug.LogWarning("Duplicate keys in dictionary was found! - renaming key:" + desiredKey + " to:" + (desiredKey + count)); return (desiredKey + count); } }
/// <summary> /// Initializes a new instance of the <see cref="SerializableLocalizationObjectPair"/> class. /// </summary> /// <param name='keyValue'> /// Key value. /// </param> /// <param name='changedValue'> /// Changed value. /// </param> public SerializableLocalizationObjectPair(string keyValue, LocalizedObject changedValue) { this.keyValue = keyValue; this.changedValue = changedValue; }
/// <summary> /// Adds a new key to a dictionary<string,LocalizedObject> and does not stop until a unique key is found /// </summary> public static string AddNewKeyPersistent(Dictionary<string, LocalizedObject> thisDictionary, string desiredKey, LocalizedObject newValue) { if(!thisDictionary.ContainsKey(desiredKey)) { thisDictionary.Add(desiredKey, newValue); return desiredKey; } else { bool newKeyFound = false; int count = 0; while(!newKeyFound) { if(!thisDictionary.ContainsKey(desiredKey + count)) { thisDictionary.Add(desiredKey + count, newValue); newKeyFound = true; } else { count++; } } Debug.LogWarning("Duplicate keys in dictionary was found! - renaming key:" + desiredKey + " to:" + (desiredKey + count)); return (desiredKey + count); } }
/// <summary> /// Reads a specific data tag from the xml document. /// </summary> /// <param name='reader'> /// Reader. /// </param> private void ReadData(XmlReader reader) { //If these values are not being set, //something is wrong. string key = "ERROR"; string value = "ERROR"; if (reader.HasAttributes) { while (reader.MoveToNextAttribute()) { if (reader.Name == "name") { key = reader.Value; } } } //Move back to the element reader.MoveToElement(); //Read the child nodes if (reader.ReadToDescendant("value")) { do { value = reader.ReadString(); } while (reader.ReadToNextSibling("value")); } //Add the raw values to the dictionary textDataBase.Add(key, value); //Add the localized parsed values to the localizedObjectDataBase LocalizedObject newLocalizedObject = new LocalizedObject(); newLocalizedObject.ObjectType = LocalizedObject.GetLocalizedObjectType(key); newLocalizedObject.TextValue = value; localizedObjectDataBase.Add(LocalizedObject.GetCleanKey(key,newLocalizedObject.ObjectType), newLocalizedObject); }