2018-09-19 09:04:38 +08:00
using System.Collections.Generic ;
using System.IO ;
2018-12-30 18:00:05 +08:00
using StardewValley ;
2018-09-19 09:04:38 +08:00
namespace Omegasis.HappyBirthday.Framework
{
2018-12-30 18:00:05 +08:00
/// <summary>A class which deals with handling different translations for Vocalization should other voice teams ever wish to voice act for that language.</summary>
2018-09-19 09:04:38 +08:00
public class TranslationInfo
{
2018-12-30 18:00:05 +08:00
/// <summary>The list of all supported translations by this mod.</summary>
2018-09-19 09:04:38 +08:00
public List < string > translations ;
2018-12-30 18:00:05 +08:00
/// <summary>The current translation mode for the mod, so that it knows what files to load at the beginning of the game.</summary>
2018-09-19 09:04:38 +08:00
public string currentTranslation ;
2018-12-30 18:00:05 +08:00
/// <summary>Holds the info for what translation has what file extension.</summary>
2018-09-19 09:04:38 +08:00
public Dictionary < string , string > translationFileInfo ;
public Dictionary < string , LocalizedContentManager . LanguageCode > translationCodes ;
2018-12-30 18:00:05 +08:00
/// <summary>Construct an instance..</summary>
2018-09-19 09:04:38 +08:00
public TranslationInfo ( )
{
2018-12-30 18:00:05 +08:00
this . translations = new List < string > ( ) ;
this . translationFileInfo = new Dictionary < string , string > ( ) ;
this . translationCodes = new Dictionary < string , LocalizedContentManager . LanguageCode > ( ) ;
this . translations . Add ( "English" ) ;
this . translations . Add ( "Spanish" ) ;
this . translations . Add ( "Chinese" ) ;
this . translations . Add ( "Japanese" ) ;
this . translations . Add ( "Russian" ) ;
this . translations . Add ( "German" ) ;
this . translations . Add ( "Brazillian Portuguese" ) ;
this . currentTranslation = "English" ;
this . translationFileInfo . Add ( "English" , ".json" ) ;
this . translationFileInfo . Add ( "Spanish" , ".es-ES.json" ) ;
this . translationFileInfo . Add ( "Chinese" , ".zh-CN.json" ) ;
this . translationFileInfo . Add ( "Japanese" , ".ja-JP.json" ) ;
this . translationFileInfo . Add ( "Russian" , ".ru-RU.json" ) ;
this . translationFileInfo . Add ( "German" , ".de-DE.json" ) ;
this . translationFileInfo . Add ( "Brazillian Portuguese" , ".pt-BR.json" ) ;
this . translationCodes . Add ( "English" , LocalizedContentManager . LanguageCode . en ) ;
this . translationCodes . Add ( "Spanish" , LocalizedContentManager . LanguageCode . es ) ;
this . translationCodes . Add ( "Chinese" , LocalizedContentManager . LanguageCode . zh ) ;
this . translationCodes . Add ( "Japanese" , LocalizedContentManager . LanguageCode . ja ) ;
this . translationCodes . Add ( "Russian" , LocalizedContentManager . LanguageCode . ru ) ;
this . translationCodes . Add ( "German" , LocalizedContentManager . LanguageCode . de ) ;
this . translationCodes . Add ( "Brazillian Portuguese" , LocalizedContentManager . LanguageCode . pt ) ;
2018-09-19 09:04:38 +08:00
}
public string getTranslationNameFromPath ( string fullPath )
{
return Path . GetFileName ( fullPath ) ;
}
public void changeLocalizedContentManagerFromTranslation ( string translation )
{
2018-12-30 18:00:05 +08:00
string tra = this . getTranslationNameFromPath ( translation ) ;
bool f = this . translationCodes . TryGetValue ( tra , out LocalizedContentManager . LanguageCode code ) ;
if ( ! f ) LocalizedContentManager . CurrentLanguageCode = LocalizedContentManager . LanguageCode . en ;
2018-09-19 09:04:38 +08:00
else LocalizedContentManager . CurrentLanguageCode = code ;
return ;
}
public void resetLocalizationCode ( )
{
LocalizedContentManager . CurrentLanguageCode = LocalizedContentManager . LanguageCode . en ;
}
2018-12-30 18:00:05 +08:00
/// <summary>Gets the proper file extension for the current translation.</summary>
2018-09-19 09:04:38 +08:00
/// <param name="path"></param>
public string getFileExtentionForTranslation ( string path )
{
/ *
bool f = translationFileInfo . TryGetValue ( translation , out string value ) ;
2018-12-30 18:00:05 +08:00
if ( ! f ) return ".json" ;
2018-09-19 09:04:38 +08:00
else return value ;
* /
string translation = Path . GetFileName ( path ) ;
try
{
2018-12-30 18:00:05 +08:00
return this . translationFileInfo [ translation ] ;
2018-09-19 09:04:38 +08:00
}
2018-12-30 18:00:05 +08:00
catch
2018-09-19 09:04:38 +08:00
{
2018-12-07 08:13:41 +08:00
HappyBirthday . ModMonitor . Log ( "WTF SOMETHING IS WRONG!" , StardewModdingAPI . LogLevel . Warn ) ;
2018-09-19 09:04:38 +08:00
//Vocalization.ModMonitor.Log(err.ToString());
//Vocalization.ModMonitor.Log("Attempted to get translation: " + translation);
return ".json" ;
}
}
2018-12-30 18:00:05 +08:00
/// <summary>Gets the proper json for Buildings (aka Blueprints) from the data folder.</summary>
2018-09-19 09:04:38 +08:00
public string getBuildingjsonForTranslation ( string translation )
{
string buildings = "Blueprints" ;
2018-12-30 18:00:05 +08:00
return buildings + this . getFileExtentionForTranslation ( translation ) ;
2018-09-19 09:04:38 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Gets the proper json file for the name passed in. Combines the file name with it's proper translation extension.</summary>
2018-09-19 09:04:38 +08:00
/// <param name="jsonFileName"></param>
/// <param name="translation"></param>
public string getjsonForTranslation ( string jsonFileName , string translation )
{
2018-12-30 18:00:05 +08:00
return jsonFileName + this . getFileExtentionForTranslation ( translation ) ;
2018-09-19 09:04:38 +08:00
}
2018-12-30 18:00:05 +08:00
/// <summary>Loads an json file from StardewValley/Content</summary>
2018-09-19 09:04:38 +08:00
/// <param name="jsonFileName"></param>
/// <param name="key"></param>
/// <param name="translation"></param>
public string LoadjsonFile ( string jsonFileName , string key , string translation )
{
2018-12-30 18:00:05 +08:00
string json = jsonFileName + this . getFileExtentionForTranslation ( translation ) ;
2018-09-19 09:04:38 +08:00
Dictionary < string , string > loadedDict = Game1 . content . Load < Dictionary < string , string > > ( json ) ;
2018-12-30 18:00:05 +08:00
bool f = loadedDict . TryGetValue ( key , out string loaded ) ;
if ( ! f )
2018-09-19 09:04:38 +08:00
{
//Vocalization.ModMonitor.Log("Big issue: Key not found in file:" + json + " " + key);
return "" ;
}
else return loaded ;
}
2018-12-30 18:00:05 +08:00
/// <summary>Loads a string dictionary from a json file.</summary>
2018-09-19 09:04:38 +08:00
/// <param name="jsonFileName"></param>
/// <param name="translation"></param>
2018-12-30 18:00:05 +08:00
public Dictionary < string , string > LoadJsonFileDictionary ( string jsonFileName , string translation )
2018-09-19 09:04:38 +08:00
{
2018-12-30 18:00:05 +08:00
string json = jsonFileName + this . getFileExtentionForTranslation ( translation ) ;
2018-09-19 09:04:38 +08:00
Dictionary < string , string > loadedDict = Game1 . content . Load < Dictionary < string , string > > ( 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 ) ;
}
2018-12-30 18:00:05 +08:00
catch { }
2018-09-19 09:04:38 +08:00
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 ) ;
}
2018-12-30 18:00:05 +08:00
catch { }
2018-09-19 09:04:38 +08:00
return format ;
}
public virtual string LoadString ( string path , string translation , object sub1 )
{
string format = this . LoadString ( path , translation ) ;
try
{
return string . Format ( format , sub1 ) ;
}
2018-12-30 18:00:05 +08:00
catch { }
2018-09-19 09:04:38 +08:00
return format ;
}
public virtual string LoadString ( string path , string translation )
{
2018-12-30 18:00:05 +08:00
this . parseStringPath ( path , out string assetName , out string key ) ;
return this . LoadjsonFile ( assetName , key , translation ) ;
2018-09-19 09:04:38 +08:00
}
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 ) ;
}
2018-12-30 18:00:05 +08:00
catch { }
2018-09-19 09:04:38 +08:00
}
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 ) ;
}
}
}