using StardewValley; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Omegasis.HappyBirthday.Framework { /// /// A class which deals with handling different translations for Vocalization should other voice teams ever wish to voice act for that language. /// public class TranslationInfo { /// /// The list of all supported translations by this mod. /// public List translations; /// /// The current translation mode for the mod, so that it knows what files to load at the beginning of the game. /// public string currentTranslation; /// /// Holds the info for what translation has what file extension. /// public Dictionary translationFileInfo; public Dictionary translationCodes; /// /// Default constructor. /// public TranslationInfo() { translations = new List(); translationFileInfo = new Dictionary(); translationCodes = new Dictionary(); translations.Add("English"); translations.Add("Spanish"); translations.Add("Chinese"); translations.Add("Japanese"); translations.Add("Russian"); translations.Add("German"); translations.Add("Brazillian Portuguese"); currentTranslation = "English"; translationFileInfo.Add("English", ".json"); translationFileInfo.Add("Spanish", ".es-ES.json"); translationFileInfo.Add("Chinese", ".zh-CN.json"); translationFileInfo.Add("Japanese", ".ja-JP.json"); translationFileInfo.Add("Russian", ".ru-RU.json"); translationFileInfo.Add("German", ".de-DE.json"); translationFileInfo.Add("Brazillian Portuguese", ".pt-BR.json"); translationCodes.Add("English", LocalizedContentManager.LanguageCode.en); translationCodes.Add("Spanish", LocalizedContentManager.LanguageCode.es); translationCodes.Add("Chinese", LocalizedContentManager.LanguageCode.zh); translationCodes.Add("Japanese", LocalizedContentManager.LanguageCode.ja); translationCodes.Add("Russian", LocalizedContentManager.LanguageCode.ru); translationCodes.Add("German", LocalizedContentManager.LanguageCode.de); translationCodes.Add("Brazillian Portuguese", LocalizedContentManager.LanguageCode.pt); } public string getTranslationNameFromPath(string fullPath) { return Path.GetFileName(fullPath); } public void changeLocalizedContentManagerFromTranslation(string translation) { string tra = getTranslationNameFromPath(translation); bool f = translationCodes.TryGetValue(tra, out LocalizedContentManager.LanguageCode code); if (f == false) LocalizedContentManager.CurrentLanguageCode = LocalizedContentManager.LanguageCode.en; else LocalizedContentManager.CurrentLanguageCode = code; return; } public void resetLocalizationCode() { LocalizedContentManager.CurrentLanguageCode = LocalizedContentManager.LanguageCode.en; } /// /// Gets the proper file extension for the current translation. /// /// /// public string getFileExtentionForTranslation(string path) { /* bool f = translationFileInfo.TryGetValue(translation, out string value); if (f == false) return ".json"; else return value; */ string translation = Path.GetFileName(path); try { return translationFileInfo[translation]; } catch (Exception err) { //Vocalization.ModMonitor.Log(err.ToString()); //Vocalization.ModMonitor.Log("Attempted to get translation: " + translation); return ".json"; } } /// /// Gets the proper json for Buildings (aka Blueprints) from the data folder. /// /// /// public string getBuildingjsonForTranslation(string translation) { string buildings = "Blueprints"; return buildings + getFileExtentionForTranslation(translation); } /// /// Gets the proper json file for the name passed in. Combines the file name with it's proper translation extension. /// /// /// /// public string getjsonForTranslation(string jsonFileName, string translation) { return jsonFileName + getFileExtentionForTranslation(translation); } /// /// Loads an json file from StardewValley/Content /// /// /// /// /// public string LoadjsonFile(string jsonFileName, string key, string translation) { string json = jsonFileName + getFileExtentionForTranslation(translation); Dictionary loadedDict = Game1.content.Load>(json); string loaded; bool f = loadedDict.TryGetValue(key, out loaded); if (f == false) { //Vocalization.ModMonitor.Log("Big issue: Key not found in file:" + json + " " + key); return ""; } else return loaded; } /// /// Loads a string dictionary from a json file. /// /// /// /// public Dictionary LoadJsonFileDictionary(string jsonFileName, string translation) { string json = jsonFileName + getFileExtentionForTranslation(translation); Dictionary loadedDict = Game1.content.Load>(json); return loadedDict; } public virtual string LoadString(string path, string translation, object sub1, object sub2, object sub3) { string format = this.LoadString(path, translation); try { return string.Format(format, sub1, sub2, sub3); } catch (Exception ex) { } return format; } public virtual string LoadString(string path, string translation, object sub1, object sub2) { string format = this.LoadString(path, translation); try { return string.Format(format, sub1, sub2); } catch (Exception ex) { } return format; } public virtual string LoadString(string path, string translation, object sub1) { string format = this.LoadString(path, translation); try { return string.Format(format, sub1); } catch (Exception ex) { } return format; } public virtual string LoadString(string path, string translation) { string assetName; string key; this.parseStringPath(path, out assetName, out key); return LoadjsonFile(assetName, key, translation); } public virtual string LoadString(string path, string translation, params object[] substitutions) { string format = this.LoadString(path, translation); if (substitutions.Length != 0) { try { return string.Format(format, substitutions); } catch (Exception ex) { } } return format; } private void parseStringPath(string path, out string assetName, out string key) { int length = path.IndexOf(':'); assetName = path.Substring(0, length); key = path.Substring(length + 1, path.Length - length - 1); } } }