/// <summary> /// Performs a manual ToString() on a LocalizedObjectType enum since ToString() is not /// allowed on WebPlayer platforms. /// </summary> /// <param name="objectType">The enumeration to get the string value from.</param> /// <returns>The string value of the localized object type enumeration</returns> public static string GetLocalizedObjectTypeStringValue(LocalizedObjectType objectType) { switch (objectType) { case LocalizedObjectType.AUDIO: return("AUDIO"); case LocalizedObjectType.GAME_OBJECT: return("GAME_OBJECT"); case LocalizedObjectType.STRING: return("STRING"); case LocalizedObjectType.TEXTURE: return("TEXTURE"); case LocalizedObjectType.TEXT_ASSET: return("TEXT_ASSET"); case LocalizedObjectType.FONT: return("FONT"); default: return("STRING"); } }
/// <summary> /// Performs a manual ToString() on a LocalizedObjectType enum since ToString() is not /// allowed on WebPlayer platforms. /// </summary> /// <param name="objectType">The enumeration to get the string value from.</param> /// <returns>The string value of the localized object type enumeration</returns> public static string GetLocalizedObjectTypeStringValue(LocalizedObjectType objectType) { switch (objectType) { case LocalizedObjectType.Audio: return("AUDIO"); case LocalizedObjectType.GameObject: return("GAME_OBJECT"); case LocalizedObjectType.String: return("STRING"); case LocalizedObjectType.Texture: return("TEXTURE"); case LocalizedObjectType.TextAsset: return("TEXT_ASSET"); case LocalizedObjectType.Font: return("FONT"); default: return("STRING"); } }
/// <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(70))) { 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> /// 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> /// Creates a new Localized Object from another /// </summary> /// <param name="other">The object to copy</param> public LocalizedObject(LocalizedObject other) { if (other != null) { objectType = other.ObjectType; textValue = other.TextValue; overrideLocalizedObject = other.OverrideLocalizedObject; } }
/// <summary> /// Gets the full key value with identifiers and everything /// </summary> /// <returns> /// The full key. /// </returns> /// <param name='parsedKey'> /// Parsed key. (Clean key originally from GetCleanKey) /// </param> public static string GetFullKey(string parsedKey, LocalizedObjectType objectType) { if (objectType == LocalizedObjectType.STRING) { //No identifier is returned for a string return(parsedKey); } return(keyTypeIdentifier + objectType.ToString() + "]" + parsedKey); }
/// <summary> /// Gets the full key value with identifiers and everything /// </summary> /// <returns> /// The full key. /// </returns> /// <param name='parsedKey'> /// Parsed key. (Clean key originally from GetCleanKey) /// </param> public static string GetFullKey(string parsedKey, LocalizedObjectType objectType) { if (objectType == LocalizedObjectType.String) { //No identifier is returned for a string return(parsedKey); } return(keyTypeIdentifier + GetLocalizedObjectTypeStringValue(objectType) + endBracket + parsedKey); }
/// <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> /// Call this from OnInspectorGUI in your own editor class. It will create buttons and a selectable popup with all the keys available. /// </summary> /// <param name="currentKey">The currently chosen key</param> /// <param name="sort">If the select key GUI should filter keys with a certain type</param> /// <param name="sortType">If sort is true, this is the type of keys that will be shown.</param> /// <returns>The currently chosen key</returns> public static string SelectKeyGUI(string currentKey, bool sort = false, LocalizedObjectType sortType = LocalizedObjectType.INVALID) { if (!ShouldShowKeySelector()) { return(currentKey); } 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(); 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(70))) { RefreshList(sort, sortType); } return(currentKey); }
/// <summary> /// Call this from OnInspectorGUI in your own editor class. It will create buttons and a selectable popup with all the keys available. /// </summary> /// <param name="currentKey">The currently chosen key</param> /// <param name="sort">If the select key GUI should filter keys with a certain type</param> /// <param name="sortType">If sort is true, this is the type of keys that will be shown.</param> /// <returns>The currently chosen key</returns> public static string SelectKeyGUI(string currentKey, bool sort = false, LocalizedObjectType sortType = LocalizedObjectType.INVALID) { if (!ShouldShowKeySelector()) { return(currentKey); } EditorGUILayout.BeginHorizontal(); GUILayout.Label("GGLocalization", EditorStyles.boldLabel); if (GUILayout.Button("Открыть", GUILayout.Width(70))) { SmartLocalizationWindow.ShowWindow(); } EditorGUILayout.EndHorizontal(); if (autoRefresh || parsedRootValues.Count == 0 || sortType != loadedObjectType) { RefreshList(sort, sortType); } EditorGUILayout.BeginHorizontal(); //GUILayout.Label("Режим сортировки: ", EditorStyles.miniLabel, GUILayout.Width(100)); if (sort) { GUILayout.Label("Режим сортировки: " + sortType.ToString() + " только.", EditorStyles.miniLabel); } else { GUILayout.Label("Режим сортировки: Показаны ВСЕ типы", EditorStyles.miniLabel); } EditorGUILayout.EndHorizontal(); 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("Обновить список", GUILayout.Width(130))) { RefreshList(sort, sortType); } return(currentKey); }
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> /// Gets the clean key. i.e the key without a type identifier "<type=TEXTURE>" etc. /// </summary> /// <returns> /// The clean key without the type identifier /// </returns> /// <param name='key'> /// The key language value /// </param> /// <param name='objectType'> /// The LocalizedObjectType of the key /// </param> public static string GetCleanKey(string key, LocalizedObjectType objectType) { int identifierLength = (keyTypeIdentifier + objectType.ToString() + ">").Length; if (objectType == LocalizedObjectType.STRING) { return(key); } else if (objectType == LocalizedObjectType.AUDIO || objectType == LocalizedObjectType.GAME_OBJECT || objectType == LocalizedObjectType.TEXTURE) { return(key.Substring(identifierLength)); } //if none of the above, return the key and send error message Debug.LogError("LocalizedObject.GetCleanKey(key) error!, object type is unknown! objectType:" + (int)objectType); return(key); }
/// <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> /// Gets the clean key. i.e the key without a type identifier "<type=TEXTURE>" etc. /// </summary> /// <returns> /// The clean key without the type identifier /// </returns> /// <param name='key'> /// The key language value /// </param> /// <param name='objectType'> /// The LocalizedObjectType of the key /// </param> public static string GetCleanKey(string key, LocalizedObjectType objectType) { int identifierLength = (keyTypeIdentifier + GetLocalizedObjectTypeStringValue(objectType) + endBracket).Length; if (objectType == LocalizedObjectType.String) { return(key); } else if (objectType == LocalizedObjectType.Audio || objectType == LocalizedObjectType.GameObject || objectType == LocalizedObjectType.Texture) { return(key.Substring(identifierLength)); } else if (objectType == LocalizedObjectType.TextAsset || objectType == LocalizedObjectType.Font) { return(key.Substring(identifierLength)); } //if none of the above, return the key and send error message Debug.LogError("LocalizedObject.GetCleanKey(key) error!, object type is unknown! objectType:" + (int)objectType); return(key); }
SerializableLocalizationObjectPair DrawRootPair(Rect position, SerializableLocalizationObjectPair item) { LocalizedObjectType newChangedType = (LocalizedObjectType)EditorGUI.Popup( listColumns.GetColumnPosition(position, "Type"), (int)item.changedValue.ObjectType, keyTypes); int index = changedRootValues.IndexOf(item); SerializableStringPair rootValue = changedRootKeys[index]; string newChangedValue = EditorGUI.TextField(listColumns.GetColumnPosition(position, "Key"), rootValue.changedValue); string newTextValue = EditorGUI.TextArea(listColumns.GetColumnPosition(position, "Comment"), item.changedValue.TextValue); if (newChangedType != changedRootValues[index].changedValue.ObjectType || newChangedValue != rootValue.changedValue || newTextValue != changedRootValues[index].changedValue.TextValue) { undoManager.ForceDirty(); changedRootValues[index].changedValue.ObjectType = newChangedType; rootValue.changedValue = newChangedValue; changedRootValues[index].changedValue.TextValue = newTextValue; } return(item); }
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 ---"); } } }
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 ---"); } } }
/// <summary> /// Refreshes the list containing the selectable keys /// </summary> /// <param name="sort">If the refreshed list should be sorted and filtered with a certain key type</param> /// <param name="sortType">The key type to use as filter</param> public static void RefreshList(bool sort, LocalizedObjectType sortType) { if (!Application.isPlaying) { parsedRootValues.Clear(); Dictionary <string, LocalizedObject> values = LanguageHandlerEditor.LoadParsedLanguageFile(null, true); 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 } } }
/// <summary> /// Gets the full key value with identifiers and everything /// </summary> /// <returns> /// The full key. /// </returns> /// <param name='parsedKey'> /// Parsed key. (Clean key originally from GetCleanKey) /// </param> public static string GetFullKey(string parsedKey, LocalizedObjectType objectType) { if(objectType == LocalizedObjectType.STRING) { //No identifier is returned for a string return parsedKey; } return (keyTypeIdentifier + objectType.ToString() + "]" + parsedKey); }
/// <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); } } }
/// <summary> /// Gets the clean key. i.e the key without a type identifier "<type=TEXTURE>" etc. /// </summary> /// <returns> /// The clean key without the type identifier /// </returns> /// <param name='key'> /// The key language value /// </param> /// <param name='objectType'> /// The LocalizedObjectType of the key /// </param> public static string GetCleanKey(string key, LocalizedObjectType objectType) { int identifierLength = (keyTypeIdentifier + objectType.ToString() + ">").Length; if(objectType == LocalizedObjectType.STRING) { return key; } else if(objectType == LocalizedObjectType.AUDIO || objectType == LocalizedObjectType.GAME_OBJECT || objectType == LocalizedObjectType.TEXTURE) { return key.Substring(identifierLength); } //if none of the above, return the key and send error message Debug.LogError("LocalizedObject.GetCleanKey(key) error!, object type is unknown! objectType:" + (int)objectType); return key; }
/// <summary> /// Gets the clean key. i.e the key without a type identifier "[type=TEXTURE]" etc. /// </summary> /// <returns> /// The clean key without the type identifier /// </returns> /// <param name='key'> /// The full key with the type identifier /// </param> public static string GetCleanKey(string key) { LocalizedObjectType objectType = GetLocalizedObjectType(key); return(GetCleanKey(key, objectType)); }
/// <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); } } }