/// <summary> /// Creates the initial root language file /// </summary> public static void CreateRootResourceFile() { //Add a dummy value so that the user will see how everything works Dictionary<string,string> baseDictionary = new Dictionary<string, string>(); baseDictionary.Add("MyFirst.Key", "MyFirstValue"); SaveLanguageFile(baseDictionary, LocalizationWorkspace.RootLanguageFilePath()); }
/// <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); } } }