public static GetLocalizedObjectType ( string key ) : LocalizedObjectType | ||
key | string | /// The language key from the .resx file /// |
return | LocalizedObjectType |
/// <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> /// 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); }
/// <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(); }
/// <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(); }
/// <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); } } }