Skip to content

Commit

Permalink
Version 4.18.0
Browse files Browse the repository at this point in the history
 * FEATURE - Improved AutoResize command to enable min/max font size control
 * FEATURE - Added translation file directive allowing control of tranlations/resizing based on resolution size
 * FEATURE - Added new '_Postprocessors.txt' file that can make substitutions to the text returned from the translation endpoint
 * MISC - Changed default value of EnableTranslationScoping to True
 * BUG FIX - Fixed bug where GoogleTranslate could no longer be properly selected a translate endpoint
  • Loading branch information
randoman committed Jul 31, 2021
1 parent e01fdd3 commit 596a1ac
Show file tree
Hide file tree
Showing 26 changed files with 2,391 additions and 55 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
### 4.17.0
### 4.18.0
* FEATURE - Improved AutoResize command to enable min/max font size control
* FEATURE - Added translation file directive allowing control of tranlations/resizing based on resolution size
* FEATURE - Added new '_Postprocessors.txt' file that can make substitutions to the text returned from the translation endpoint
* MISC - Changed default value of EnableTranslationScoping to True
* BUG FIX - Fixed bug where GoogleTranslate could no longer be properly selected a translate endpoint

### 4.17.0
* FEATURE - Additional public APIs in relation to SimpleTextTranslationCache
* CHANGE - Default endpoint changed to GoogleTranslateV2
* MISC - Improved error logging in GoogleTranslate
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ Directory=Translation\{Lang}\Text ;Directory t
OutputFile=Translation\{Lang}\Text\_AutoGeneratedTranslations.txt ;File to insert generated translations into. Can use placeholders: {GameExeName}, {Lang}
SubstitutionFile=Translation\{Lang}\Text\_Substitutions.txt ;File that contains substitution applied before translations. Can use placeholders: {GameExeName}, {Lang}
PreprocessorsFile=Translation\{Lang}\Text\_Preprocessors.txt ;File that contains preprocessors to be applied before sending a text to a translator. Can use placeholders: {GameExeName}, {Lang}
PostprocessorsFile=Translation\{Lang}\Text\_Postprocessors.txt ;File that contains postprocessors to be applied after receiving a text from a translator. Can use placeholders: {GameExeName}, {Lang}

[TextFrameworks]
EnableUGUI=True ;Enable or disable UGUI translation
Expand Down Expand Up @@ -403,11 +404,13 @@ After the text has been translated by the configured service, `ForceSplitTextAft

The main reason that this type of handling can make or break a translation really comes down to whether or not whitespace is removed from the source text before sending it to the endpoint. Most endpoints (such as GoogleTranslate) consider text on multiple lines seperately, which can often result in terrible translation if an unnecessary newline is included.

#### Text pre-processing
#### Text post/pre-processing
While proper whitespace handling goes a long way in ensuring better translations, it is not always enough.

The `PreprocessorsFile` allows defining entries that modifies the text just before it is sent to the translator.

The `PostprocessorsFile` allows defining entries that modifies the translated text just after it is received from the translator.

#### UI Resizing
Often when performing a translation on a text component, the resulting text is larger than the original. This often means that there is not enough room in the text component for the result. This section describes ways to remedy that by changing important parameters of the text components.

Expand Down Expand Up @@ -657,7 +660,8 @@ The following types of commands exists:
* `ChangeFontSize(int size)`: Where the size if the new size of the font
* `IgnoreFontSize()`: This can be used to reset font resize behavior that was set on a very 'non-specific' path.
* Commands that control auto-resizing:
* `AutoResize(bool enabled)`: Where enabled control if auto-resize behaviour should be enabled.
* `AutoResize(bool enabled, minSize, maxSize)`: Where enabled control if auto-resize behaviour should be enabled. The two last parameters are optional.
* minSize, maxSize possible values: [keep, none, any number]
* Commands that control the line spacing (UGUI only):
* `UGUI_ChangeLineSpacingByPercentage(float percentage)`
* `UGUI_ChangeLineSpacing(float lineSpacing)`
Expand All @@ -680,6 +684,8 @@ The translation files support the following directives:
* `#unset level 1,2,3` tells the plugin that translations following this line in this file should not be applied in scenes with ID 1, 2 or 3. If no levels are set, all specified translations are global.
* `#set exe game1,game2` tells the plugin that translations following this line in this file may only be applied when the game is run through an executable with the name game1 or game2.
* `#unset exe game1,game2` tells the plugin that translations following this line in this file should not be applied when the game is run through an executable with the name game1 or game2. If no exes are set, all specified translations are global.
* `#set required-resolution height > 1280 && width > 720` tells the plugin that translations following this line in this file should only be applied if the resolution is greater than specified. Current implementation only handles the resolution used by the game at startup.
* `#unset required-resolution` tells the plugin to ignore previously specified `#set required-resolution` directive.

For this to work, the following configuration option must be changed, as it defaults to `False`:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void CreateClientAndHandler()
_handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

_client = new HttpClient( _handler, true );
_client.DefaultRequestHeaders.UserAgent.Add( new ProductInfoHeaderValue( "XUnity", "4.17.0" ) );
_client.DefaultRequestHeaders.UserAgent.Add( new ProductInfoHeaderValue( "XUnity", "4.18.0" ) );
_client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue( "*/*" ) );
}

Expand Down
2 changes: 1 addition & 1 deletion src/Translators/GoogleTranslate/GoogleTranslateEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public GoogleTranslateEndpoint()
_cookieContainer = new CookieContainer();
}

public override string Id => KnownTranslateEndpointNames.GoogleTranslateV2;
public override string Id => "GoogleTranslate";

public override string FriendlyName => "Google! Translate";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static long LongRandom( long min, long max, Random rand )
return ( Math.Abs( longRand % ( max - min ) ) + min );
}

public override string Id => "GoogleTranslateV2";
public override string Id => KnownTranslateEndpointNames.GoogleTranslateV2;

public override string FriendlyName => "Google! Translate (v2)";

Expand Down
2 changes: 1 addition & 1 deletion src/XUnity.AutoTranslator.Patcher/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override string Version
{
get
{
return "4.17.0";
return "4.18.0";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<Version>4.17.0</Version>
<Version>4.18.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
22 changes: 21 additions & 1 deletion src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal static class Settings

public static Dictionary<string, string> Replacements = new Dictionary<string, string>();
public static Dictionary<string, string> Preprocessors = new Dictionary<string, string>();
public static Dictionary<string, string> Postprocessors = new Dictionary<string, string>();
//public static List<RegexTranslationSplitter> Patterns = new List<RegexTranslationSplitter>();

public static bool SimulateError = false;
Expand Down Expand Up @@ -69,12 +70,14 @@ internal static class Settings
public static string OutputFile;
public static string SubstitutionFile;
public static string PreprocessorsFile;
public static string PostprocessorsFile;
public static string TranslationDirectory;
public static int MaxCharactersPerTranslation;
public static bool EnableConsole;
public static string AutoTranslationsFilePath;
public static string SubstitutionFilePath;
public static string PreprocessorsFilePath;
public static string PostprocessorsFilePath;
public static string TranslationsPath;
public static string TexturesPath;
public static string TranslatorsPath;
Expand Down Expand Up @@ -157,6 +160,9 @@ internal static class Settings
public static bool CopyToClipboard;
public static int MaxClipboardCopyCharacters;

internal static int ScreenWidth;
internal static int ScreenHeight;

public static void Configure()
{
try
Expand All @@ -183,6 +189,18 @@ public static void Configure()
ApplicationName = string.Empty;
}

try
{
var res = Screen.currentResolution;
ScreenWidth = res.width;
ScreenHeight = res.height;
XuaLogger.AutoTranslator.Debug( "Screen resolution determine to be: " + ScreenWidth + "x" + ScreenHeight );
}
catch( Exception e )
{
XuaLogger.AutoTranslator.Warn( e, "An error occurred while trying to determine the game resolution." );
}

ServiceEndpoint = PluginEnvironment.Current.Preferences.GetOrDefault( "Service", "Endpoint", KnownTranslateEndpointNames.GoogleTranslateV2 );
FallbackServiceEndpoint = PluginEnvironment.Current.Preferences.GetOrDefault( "Service", "FallbackEndpoint", string.Empty );

Expand All @@ -193,6 +211,7 @@ public static void Configure()
OutputFile = PluginEnvironment.Current.Preferences.GetOrDefault( "Files", "OutputFile", Path.Combine( "Translation", Path.Combine( "{Lang}", Path.Combine( "Text", "_AutoGeneratedTranslations.txt" ) ) ) );
SubstitutionFile = PluginEnvironment.Current.Preferences.GetOrDefault( "Files", "SubstitutionFile", Path.Combine( "Translation", Path.Combine( "{Lang}", Path.Combine( "Text", "_Substitutions.txt" ) ) ) );
PreprocessorsFile = PluginEnvironment.Current.Preferences.GetOrDefault( "Files", "PreprocessorsFile", Path.Combine( "Translation", Path.Combine( "{Lang}", Path.Combine( "Text", "_Preprocessors.txt" ) ) ) );
PostprocessorsFile = PluginEnvironment.Current.Preferences.GetOrDefault( "Files", "PostprocessorsFile", Path.Combine( "Translation", Path.Combine( "{Lang}", Path.Combine( "Text", "_Postprocessors.txt" ) ) ) );

EnableIMGUI = PluginEnvironment.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableIMGUI", false );
EnableUGUI = PluginEnvironment.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableUGUI", true );
Expand Down Expand Up @@ -229,7 +248,7 @@ public static void Configure()
CacheWhitespaceDifferences = PluginEnvironment.Current.Preferences.GetOrDefault( "Behaviour", "CacheWhitespaceDifferences", false );
GenerateStaticSubstitutionTranslations = PluginEnvironment.Current.Preferences.GetOrDefault( "Behaviour", "GenerateStaticSubstitutionTranslations", false );
GeneratePartialTranslations = PluginEnvironment.Current.Preferences.GetOrDefault( "Behaviour", "GeneratePartialTranslations", false );
EnableTranslationScoping = PluginEnvironment.Current.Preferences.GetOrDefault( "Behaviour", "EnableTranslationScoping", false );
EnableTranslationScoping = PluginEnvironment.Current.Preferences.GetOrDefault( "Behaviour", "EnableTranslationScoping", true );
EnableSilentMode = PluginEnvironment.Current.Preferences.GetOrDefault( "Behaviour", "EnableSilentMode", true );
BlacklistedIMGUIPlugins = PluginEnvironment.Current.Preferences.GetOrDefault( "Behaviour", "BlacklistedIMGUIPlugins", string.Empty )
?.Split( new[] { ';' }, StringSplitOptions.RemoveEmptyEntries )
Expand Down Expand Up @@ -309,6 +328,7 @@ public static void Configure()
AutoTranslationsFilePath = Path.Combine( PluginEnvironment.Current.TranslationPath, OutputFile.UseCorrectDirectorySeparators() ).Parameterize();
SubstitutionFilePath = Path.Combine( PluginEnvironment.Current.TranslationPath, SubstitutionFile.UseCorrectDirectorySeparators() ).Parameterize();
PreprocessorsFilePath = Path.Combine( PluginEnvironment.Current.TranslationPath, PreprocessorsFile.UseCorrectDirectorySeparators() ).Parameterize();
PostprocessorsFilePath = Path.Combine( PluginEnvironment.Current.TranslationPath, PostprocessorsFile.UseCorrectDirectorySeparators() ).Parameterize();

TranslationsPath = Path.Combine( PluginEnvironment.Current.TranslationPath, Settings.TranslationDirectory.UseCorrectDirectorySeparators() ).Parameterize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public static class PluginData
/// <summary>
/// Gets the version of the plugin.
/// </summary>
public const string Version = "4.17.0";
public const string Version = "4.18.0";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private void SendConfigurationIfRequired()
var request = new ConfigurationMessage
{
Id = id,
Config = ConfigForExternalProcess,
Config = configForExternalProcess,
};
var payload = ExtProtocolConvert.Encode( request );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,11 @@ private string PostProcessTranslation( UntranslatedText key, string translatedTe
translatedText = RomanizationHelper.PostProcess( translatedText, Settings.TranslationPostProcessing );
}

foreach( var kvp in Settings.Postprocessors )
{
translatedText = translatedText.Replace( kvp.Key, kvp.Value );
}

if( Settings.ForceSplitTextAfterCharacters > 0 )
{
translatedText = translatedText.SplitToLines( Settings.ForceSplitTextAfterCharacters, '\n', ' ', ' ' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,37 @@ public static void LoadTranslations()
Directory.CreateDirectory( Settings.TranslationsPath );
var substitutionFile = new FileInfo( Settings.SubstitutionFilePath ).FullName;
var preprocessorsFile = new FileInfo( Settings.PreprocessorsFilePath ).FullName;
var postprocessorsFile = new FileInfo( Settings.PostprocessorsFilePath ).FullName;

LoadTranslationsInFile( substitutionFile, true, false );
LoadTranslationsInFile( preprocessorsFile, false, true );
LoadTranslationsInFile( substitutionFile, true, false, false );
LoadTranslationsInFile( preprocessorsFile, false, true, false );
LoadTranslationsInFile( postprocessorsFile, false, false, true );
}

private static void LoadTranslationsInFile( string fullFileName, bool isSubstitutionFile, bool isPreprocessorFile )
private static void LoadTranslationsInFile( string fullFileName, bool isSubstitutionFile, bool isPreprocessorFile, bool isPostprocessorsFile )
{
var fileExists = File.Exists( fullFileName );
if( fileExists || isSubstitutionFile || isPreprocessorFile )
if( fileExists )
{
if( fileExists )
using( var stream = File.OpenRead( fullFileName ) )
{
using( var stream = File.OpenRead( fullFileName ) )
{
LoadTranslationsInStream( stream, fullFileName, isSubstitutionFile, isPreprocessorFile );
}
LoadTranslationsInStream( stream, fullFileName, isSubstitutionFile, isPreprocessorFile, isPostprocessorsFile );
}
else if( isSubstitutionFile || isPreprocessorFile )
{
var fi = new FileInfo( fullFileName );
Directory.CreateDirectory( fi.Directory.FullName );
}
else
{
var fi = new FileInfo( fullFileName );
Directory.CreateDirectory( fi.Directory.FullName );

using( var stream = File.Create( fullFileName ) )
{
stream.Write( new byte[] { 0xEF, 0xBB, 0xBF }, 0, 3 ); // UTF-8 BOM
stream.Close();
}
using( var stream = File.Create( fullFileName ) )
{
stream.Write( new byte[] { 0xEF, 0xBB, 0xBF }, 0, 3 ); // UTF-8 BOM
stream.Close();
}
}
}

private static void LoadTranslationsInStream( Stream stream, string fullFileName, bool isSubstitutionFile, bool isPreprocessorFile )
private static void LoadTranslationsInStream( Stream stream, string fullFileName, bool isSubstitutionFile, bool isPreprocessorFile, bool isPostProcessorFile )
{
if( !Settings.EnableSilentMode ) XuaLogger.AutoTranslator.Debug( $"Loading texts: {fullFileName}." );

Expand All @@ -64,7 +63,7 @@ private static void LoadTranslationsInStream( Stream stream, string fullFileName
string[] translations = reader.ReadToEnd().Split( new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries );
foreach( string translatioOrDirective in translations )
{
if( context.IsExecutable( Settings.ApplicationName ) )
if( context.IsApplicable() )
{
string[] kvp = translatioOrDirective.Split( TranslationSplitters, StringSplitOptions.None );
if( kvp.Length == 2 )
Expand All @@ -85,6 +84,10 @@ private static void LoadTranslationsInStream( Stream stream, string fullFileName
{
Settings.Preprocessors[ key ] = value;
}
else if( isPostProcessorFile )
{
Settings.Postprocessors[ key ] = value;
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/XUnity.AutoTranslator.Plugin.Core/TextTranslationCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,13 @@ internal void LoadTranslationFiles()
var mainTranslationFile = new FileInfo( Settings.AutoTranslationsFilePath ).FullName;
var substitutionFile = new FileInfo( Settings.SubstitutionFilePath ).FullName;
var preprocessorsFile = new FileInfo( Settings.PreprocessorsFilePath ).FullName;
var postprocessorsFile = new FileInfo( Settings.PostprocessorsFilePath ).FullName;

if( _pluginDirectory == null )
{
LoadTranslationsInFile( mainTranslationFile, true );
}
foreach( var fullFileName in GetTranslationFiles().Except( new[] { mainTranslationFile, substitutionFile, preprocessorsFile }, StringComparer.OrdinalIgnoreCase ) )
foreach( var fullFileName in GetTranslationFiles().Except( new[] { mainTranslationFile, substitutionFile, preprocessorsFile, postprocessorsFile }, StringComparer.OrdinalIgnoreCase ) )
{
try
{
Expand Down Expand Up @@ -489,7 +490,7 @@ private void LoadTranslationsInStream( Stream stream, string fullFileName, bool
}
}

if( context.IsExecutable( Settings.ApplicationName ) )
if( context.IsApplicable() )
{
string[] kvp = translatioOrDirective.Split( TranslationSplitters, StringSplitOptions.None );
if( kvp.Length == 2 )
Expand Down
Loading

0 comments on commit 596a1ac

Please sign in to comment.