2019-12-02 12:47:31 +08:00
using System ;
2018-09-19 09:04:38 +08:00
using System.Collections.Generic ;
2019-12-03 11:33:32 +08:00
using System.IO ;
2019-12-02 12:47:31 +08:00
using System.Linq ;
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
{
2019-12-02 12:47:31 +08:00
public enum LanguageName
{
English ,
Spanish ,
Chinese ,
Japanese ,
Russian ,
German ,
Portuguese ,
Italian ,
French ,
Korean ,
Turkish ,
Hungarian
}
2019-12-03 11:10:56 +08:00
public enum FileType
{
XNB ,
JSON
}
2019-12-02 12:47:31 +08:00
/ * * * * * * * * *
* * Accessors
* * * * * * * * * /
/// <summary>The language names supported by this mod.</summary>
public LanguageName [ ] LanguageNames { get ; } = ( from LanguageName language in Enum . GetValues ( typeof ( LanguageName ) ) select language ) . ToArray ( ) ;
2018-09-19 09:04:38 +08:00
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>
2019-12-02 12:47:31 +08:00
public LanguageName CurrentTranslation { get ; set ; } = LanguageName . English ;
2018-09-19 09:04:38 +08:00
2018-12-30 18:00:05 +08:00
/// <summary>Holds the info for what translation has what file extension.</summary>
2019-12-02 12:47:31 +08:00
public Dictionary < LanguageName , string > TranslationFileExtensions { get ; } = new Dictionary < LanguageName , string > ( ) ;
public Dictionary < LanguageName , LocalizedContentManager . LanguageCode > TranslationCodes { get ; } = new Dictionary < LanguageName , LocalizedContentManager . LanguageCode > ( ) ;
2018-09-19 09:04:38 +08:00
2018-12-30 18:00:05 +08:00
2019-12-02 12:47:31 +08:00
/ * * * * * * * * *
* * Public methods
* * * * * * * * * /
/// <summary>Construct an instance.</summary>
2018-09-19 09:04:38 +08:00
public TranslationInfo ( )
{
2019-12-03 11:10:56 +08:00
this . TranslationFileExtensions . Add ( LanguageName . English , "en-US" ) ;
this . TranslationFileExtensions . Add ( LanguageName . Spanish , "es-ES" ) ;
this . TranslationFileExtensions . Add ( LanguageName . Chinese , "zh-CN" ) ;
this . TranslationFileExtensions . Add ( LanguageName . Japanese , "ja-JP" ) ;
this . TranslationFileExtensions . Add ( LanguageName . Russian , "ru-RU" ) ;
this . TranslationFileExtensions . Add ( LanguageName . German , "de-DE" ) ;
this . TranslationFileExtensions . Add ( LanguageName . Portuguese , "pt-BR" ) ;
2019-12-02 12:47:31 +08:00
//1.3 languages.
2019-12-03 11:10:56 +08:00
this . TranslationFileExtensions . Add ( LanguageName . Italian , "it-IT" ) ;
this . TranslationFileExtensions . Add ( LanguageName . French , "fr-FR" ) ;
this . TranslationFileExtensions . Add ( LanguageName . Hungarian , "hu-HU" ) ;
this . TranslationFileExtensions . Add ( LanguageName . Turkish , "tr-TR" ) ;
this . TranslationFileExtensions . Add ( LanguageName . Korean , "ko-KR" ) ;
2019-12-02 12:47:31 +08:00
this . TranslationCodes . Add ( LanguageName . English , LocalizedContentManager . LanguageCode . en ) ;
this . TranslationCodes . Add ( LanguageName . Spanish , LocalizedContentManager . LanguageCode . es ) ;
this . TranslationCodes . Add ( LanguageName . Chinese , LocalizedContentManager . LanguageCode . zh ) ;
this . TranslationCodes . Add ( LanguageName . Japanese , LocalizedContentManager . LanguageCode . ja ) ;
this . TranslationCodes . Add ( LanguageName . Russian , LocalizedContentManager . LanguageCode . ru ) ;
this . TranslationCodes . Add ( LanguageName . German , LocalizedContentManager . LanguageCode . de ) ;
this . TranslationCodes . Add ( LanguageName . Portuguese , LocalizedContentManager . LanguageCode . pt ) ;
//1.3 languages
this . TranslationCodes . Add ( LanguageName . Italian , LocalizedContentManager . LanguageCode . it ) ;
this . TranslationCodes . Add ( LanguageName . French , LocalizedContentManager . LanguageCode . fr ) ;
this . TranslationCodes . Add ( LanguageName . Hungarian , LocalizedContentManager . LanguageCode . hu ) ;
this . TranslationCodes . Add ( LanguageName . Turkish , LocalizedContentManager . LanguageCode . tr ) ;
this . TranslationCodes . Add ( LanguageName . Korean , LocalizedContentManager . LanguageCode . ko ) ;
2018-09-19 09:04:38 +08:00
}
2019-12-03 11:10:56 +08:00
/// <summary>
/// Gets the current SDV translation code.
/// </summary>
/// <returns></returns>
public LocalizedContentManager . LanguageCode getCurrrentLanguageCode ( )
2018-09-19 09:04:38 +08:00
{
2019-12-03 11:10:56 +08:00
return this . TranslationCodes [ this . CurrentTranslation ] ;
2018-09-19 09:04:38 +08:00
}
2019-12-03 12:54:30 +08:00
public void setTranslationFromLanguageCode ( LocalizedContentManager . LanguageCode code )
{
foreach ( var v in this . TranslationCodes )
{
if ( v . Value . Equals ( code ) )
{
this . CurrentTranslation = v . Key ;
HappyBirthday . ModHelper . WriteConfig < ModConfig > ( HappyBirthday . Config ) ;
return ;
}
}
}
2018-12-30 18:00:05 +08:00
/// <summary>Gets the proper file extension for the current translation.</summary>
2019-12-02 12:47:31 +08:00
/// <param name="language">The translation language name.</param>
2019-12-03 11:10:56 +08:00
public string getFileExtentionForTranslation ( LanguageName language , FileType File )
2019-12-02 12:47:31 +08:00
{
2018-09-19 09:04:38 +08:00
try
{
2019-12-03 11:10:56 +08:00
if ( language = = LanguageName . English )
{
return this . getFileExtensionForFileType ( File ) ;
}
2019-12-05 10:14:45 +08:00
return "." + this . TranslationFileExtensions [ language ] + this . getFileExtensionForFileType ( File ) ;
2018-09-19 09:04:38 +08:00
}
2019-12-02 12:47:31 +08:00
catch ( Exception err )
2018-09-19 09:04:38 +08:00
{
2019-12-02 12:47:31 +08:00
Omegasis . HappyBirthday . HappyBirthday . ModMonitor . Log ( err . ToString ( ) ) ;
Omegasis . HappyBirthday . HappyBirthday . ModMonitor . Log ( $"Attempted to get translation: {language}" ) ;
return ".xnb" ;
2018-09-19 09:04:38 +08:00
}
}
2019-12-03 11:10:56 +08:00
public string getFileExtensionForFileType ( FileType Type )
{
2019-12-03 12:54:30 +08:00
if ( Type = = FileType . JSON )
2019-12-03 11:10:56 +08:00
{
return ".json" ;
}
else
{
return ".xnb" ;
}
}
2018-09-19 09:04:38 +08:00
2019-12-02 12:47:31 +08:00
public string getFileExtentionForDirectory ( LanguageName language )
2018-09-19 09:04:38 +08:00
{
2019-12-02 12:47:31 +08:00
try
{
2019-12-03 12:54:30 +08:00
string s = this . TranslationFileExtensions [ language ] ;
2019-12-02 12:47:31 +08:00
return s ;
}
catch ( Exception err )
{
Omegasis . HappyBirthday . HappyBirthday . ModMonitor . Log ( err . ToString ( ) ) ;
Omegasis . HappyBirthday . HappyBirthday . ModMonitor . Log ( $"Attempted to get translation: {language}" ) ;
2019-12-03 11:10:56 +08:00
return "" ;
2019-12-02 12:47:31 +08:00
}
2018-09-19 09:04:38 +08:00
}
2019-12-03 11:10:56 +08:00
/// <summary>
/// Gets the json file for the translation.
/// </summary>
/// <param name="FileName"></param>
/// <param name="language"></param>
/// <returns></returns>
2019-12-03 12:54:30 +08:00
public string getJSONForTranslation ( string FileName , LanguageName language )
2019-12-02 12:47:31 +08:00
{
2019-12-03 12:54:30 +08:00
if ( language ! = LanguageName . English )
2019-12-03 11:10:56 +08:00
{
2019-12-07 12:59:05 +08:00
return FileName + this . getFileExtentionForTranslation ( language , FileType . JSON ) ;
2019-12-03 11:10:56 +08:00
}
else
{
2019-12-03 12:54:30 +08:00
return FileName + this . getFileExtentionForTranslation ( language , FileType . JSON ) ;
2019-12-03 11:10:56 +08:00
}
2019-12-02 12:47:31 +08:00
}
2018-09-19 09:04:38 +08:00
2019-12-02 12:47:31 +08:00
/// <summary>Loads an XNB file from StardewValley/Content</summary>
public string LoadStringFromXNBFile ( string xnbFileName , string key , LanguageName language )
2018-09-19 09:04:38 +08:00
{
2019-12-03 11:10:56 +08:00
string xnb = xnbFileName + this . getFileExtentionForTranslation ( language , FileType . XNB ) ;
2019-12-02 12:47:31 +08:00
Dictionary < string , string > loadedDict = Game1 . content . Load < Dictionary < string , string > > ( xnb ) ;
2018-09-19 09:04:38 +08:00
2019-12-02 12:47:31 +08:00
if ( ! loadedDict . TryGetValue ( key , out string loaded ) )
2018-09-19 09:04:38 +08:00
{
2019-12-02 12:47:31 +08:00
Omegasis . HappyBirthday . HappyBirthday . ModMonitor . Log ( "Big issue: Key not found in file:" + xnb + " " + key ) ;
2018-09-19 09:04:38 +08:00
return "" ;
}
2019-12-02 12:47:31 +08:00
return loaded ;
2018-09-19 09:04:38 +08:00
}
2019-12-03 11:10:56 +08:00
public virtual string LoadString ( string path , LanguageName language )
2018-09-19 09:04:38 +08:00
{
2019-12-03 11:10:56 +08:00
this . parseStringPath ( path , out string assetName , out string key ) ;
2018-09-19 09:04:38 +08:00
2019-12-03 11:10:56 +08:00
return this . LoadStringFromXNBFile ( assetName , key , language ) ;
2018-09-19 09:04:38 +08:00
}
2019-12-03 11:10:56 +08:00
private void parseStringPath ( string path , out string assetName , out string key )
2018-09-19 09:04:38 +08:00
{
2019-12-03 11:10:56 +08:00
int length = path . IndexOf ( ':' ) ;
assetName = path . Substring ( 0 , length ) ;
key = path . Substring ( length + 1 , path . Length - length - 1 ) ;
2018-09-19 09:04:38 +08:00
}
2019-12-03 11:10:56 +08:00
/// <summary>
/// Gets a translated string from the the dictionary with the proper translation; Returns an empty string if this fails somehow.
/// </summary>
/// <param name="Language"></param>
/// <param name="Key"></param>
/// <returns></returns>
public string getTranslatedString ( LocalizedContentManager . LanguageCode Language , string Key )
2018-09-19 09:04:38 +08:00
{
try
{
2019-12-03 11:10:56 +08:00
return HappyBirthday . Instance . messages . translatedStrings [ Language ] [ Key ] ;
2018-09-19 09:04:38 +08:00
2019-12-03 11:10:56 +08:00
}
2019-12-03 12:54:30 +08:00
catch ( Exception err )
2019-12-03 11:10:56 +08:00
{
return "" ;
}
2018-09-19 09:04:38 +08:00
}
2019-12-03 11:10:56 +08:00
public string getTranslatedString ( string Key )
2018-09-19 09:04:38 +08:00
{
2019-12-14 08:00:06 +08:00
if ( string . IsNullOrEmpty ( Key ) ) return "" ;
2019-12-03 11:33:32 +08:00
if ( Key . Equals ( "Birthday" ) )
{
2019-12-14 08:00:06 +08:00
string s = Game1 . content . LoadString ( "Strings\\UI:Profile_Birthday" ) ;
2019-12-03 12:18:43 +08:00
return s ;
2019-12-03 11:33:32 +08:00
}
2019-12-03 12:18:43 +08:00
if ( Key . Equals ( "Spring" ) | | Key . Equals ( "spring" ) )
2019-12-03 11:33:32 +08:00
{
2019-12-03 12:54:30 +08:00
string file = Path . Combine ( "Strings" , "StringsFromCSFiles" ) ;
2019-12-03 11:33:32 +08:00
return HappyBirthday . Config . translationInfo . LoadStringFromXNBFile ( file , "Utility.cs.5680" , HappyBirthday . Config . translationInfo . CurrentTranslation ) ;
}
2019-12-03 12:18:43 +08:00
if ( Key . Equals ( "Summer" ) | | Key . Equals ( "summer" ) )
2019-12-03 11:33:32 +08:00
{
string file = Path . Combine ( "Strings" , "StringsFromCSFiles" ) ;
return HappyBirthday . Config . translationInfo . LoadStringFromXNBFile ( file , "Utility.cs.5681" , HappyBirthday . Config . translationInfo . CurrentTranslation ) ;
}
2019-12-03 12:18:43 +08:00
if ( Key . Equals ( "Fall" ) | | Key . Equals ( "fall" ) )
2019-12-03 11:33:32 +08:00
{
string file = Path . Combine ( "Strings" , "StringsFromCSFiles" ) ;
return HappyBirthday . Config . translationInfo . LoadStringFromXNBFile ( file , "Utility.cs.5682" , HappyBirthday . Config . translationInfo . CurrentTranslation ) ;
}
2019-12-03 12:18:43 +08:00
if ( Key . Equals ( "Winter" ) | | Key . Equals ( "winter" ) )
2019-12-03 11:33:32 +08:00
{
string file = Path . Combine ( "Strings" , "StringsFromCSFiles" ) ;
return HappyBirthday . Config . translationInfo . LoadStringFromXNBFile ( file , "Utility.cs.5683" , HappyBirthday . Config . translationInfo . CurrentTranslation ) ;
}
2019-12-03 11:10:56 +08:00
try
{
return HappyBirthday . Instance . messages . translatedStrings [ this . getCurrrentLanguageCode ( ) ] [ Key ] ;
2018-09-19 09:04:38 +08:00
2019-12-03 11:10:56 +08:00
}
catch ( Exception err )
2018-09-19 09:04:38 +08:00
{
2019-12-03 11:10:56 +08:00
return "" ;
2018-09-19 09:04:38 +08:00
}
}
}
}