Prechádzať zdrojové kódy

bumped version

fixed toggle translation to properly handle KK dialogue.
fixed problem that could, in rare cases, cause a text that was already translated to be translated again
better implementation to support alternative language (FromLanguage)
gravydevsupreme 7 rokov pred
rodič
commit
9a535b1ba5

+ 3 - 3
src/XUnity.AutoTranslator.Plugin.BepIn/XUnity.AutoTranslator.Plugin.BepIn.csproj

@@ -2,9 +2,9 @@
 
    <PropertyGroup>
       <TargetFramework>net35</TargetFramework>
-      <AssemblyVersion>2.3.1.0</AssemblyVersion>
-      <Version>2.3.1</Version>
-      <FileVersion>2.3.1.0</FileVersion>
+      <AssemblyVersion>2.3.2.0</AssemblyVersion>
+      <Version>2.3.2</Version>
+      <FileVersion>2.3.2.0</FileVersion>
    </PropertyGroup>
 
    <ItemGroup>

+ 49 - 37
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -242,16 +242,24 @@ namespace XUnity.AutoTranslator.Plugin.Core
          _translations[ key ] = value;
          _translatedTexts.Add( value );
 
-         if( Settings.IgnoreWhitespaceInKeys )
+         if( Settings.IgnoreWhitespaceInDialogue )
          {
-            var newKey = key.RemoveWhitespace();
+            var newKey = key.ChangeToSingleLineForDialogue();
             _translations[ newKey ] = value;
          }
       }
 
+      private void AddNewTranslation( string key, string value )
+      {
+         lock( _writeToFileSync )
+         {
+            _newTranslations[ key ] = value;
+         }
+      }
+
       private bool TryGetTranslation( string key, out string value )
       {
-         return _translations.TryGetValue( key, out value ) || ( Settings.IgnoreWhitespaceInKeys && _translations.TryGetValue( key.RemoveWhitespace(), out value ) );
+         return _translations.TryGetValue( key, out value ) || ( Settings.IgnoreWhitespaceInDialogue && _translations.TryGetValue( key.RemoveWhitespace(), out value ) );
       }
 
       private string Any_TextChanged( object graphic, string text )
@@ -342,6 +350,10 @@ namespace XUnity.AutoTranslator.Plugin.Core
                   info?.UnresizeUI( ui );
                }
             }
+            catch( Exception e )
+            {
+               Console.WriteLine( "[ERROR][XUnity.AutoTranslator]: An error occurred while setting text on a component." + Environment.NewLine + e );
+            }
             finally
             {
                UGUIHooks.TextChanged += UguiTextEvents_OnTextChanged;
@@ -448,11 +460,12 @@ namespace XUnity.AutoTranslator.Plugin.Core
       {
          // Get the trimmed text
          text = ( text ?? GetText( ui ) ).Trim();
-         info?.Reset( text );
 
          // Ensure that we actually want to translate this text and its owning UI element. 
          if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslate( ui ) && !IsAlreadyTranslating( info ) )
          {
+            info?.Reset( text );
+
             // if we already have translation loaded in our _translatios dictionary, simply load it and set text
             string translation;
             if( TryGetTranslation( text, out translation ) )
@@ -495,7 +508,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
                            {
                               _ongoingOperations.Remove( ui );
 
-                              if( !string.IsNullOrEmpty( stabilizedText ) )
+                              if( !string.IsNullOrEmpty( stabilizedText ) && IsTranslatable( stabilizedText ) )
                               {
                                  info?.Reset( stabilizedText );
 
@@ -644,28 +657,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
                }
             }
 
-            //StartCoroutine( AutoTranslateClient.TranslateByWWW( job.UntranslatedText.ChangeToSingleLineForDialogue(), Settings.FromLanguage, Settings.Language, translatedText =>
-            //{
-            //   _consecutiveErrors = 0;
-
-            //   job.TranslatedText = translatedText;
-
-            //   if( !string.IsNullOrEmpty( translatedText ) )
-            //   {
-            //      lock( _writeToFileSync )
-            //      {
-            //         _newTranslations[ job.UntranslatedText ] = translatedText;
-            //      }
-            //   }
-
-            //   _completedJobs.Add( job );
-            //},
-            //() =>
-            //{
-            //   _consecutiveErrors++;
-            //} ) );
-
-            StartCoroutine( AutoTranslateClient.TranslateByWWW( job.UntranslatedText.ChangeToSingleLineForDialogue(), Settings.FromLanguage, Settings.Language, translatedText =>
+            job.UntranslatedDialogueText = job.UntranslatedText.ChangeToSingleLineForDialogue();
+            StartCoroutine( AutoTranslateClient.TranslateByWWW( job.UntranslatedDialogueText, Settings.FromLanguage, Settings.Language, translatedText =>
             {
                _consecutiveErrors = 0;
 
@@ -673,10 +666,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
                if( !string.IsNullOrEmpty( translatedText ) )
                {
-                  lock( _writeToFileSync )
-                  {
-                     _newTranslations[ job.UntranslatedText ] = translatedText;
-                  }
+                  AddNewTranslation( Settings.IgnoreWhitespaceInDialogue ? job.UntranslatedDialogueText : job.UntranslatedText, translatedText );
                }
 
                _completedJobs.Add( job );
@@ -775,11 +765,22 @@ namespace XUnity.AutoTranslator.Plugin.Core
             foreach( var kvp in ObjectExtensions.GetAllRegisteredObjects() )
             {
                var ui = kvp.Key;
-               var info = (TranslationInfo)kvp.Value;
+               try
+               {
+                  if( ( ui as Component )?.gameObject?.activeSelf ?? false )
+                  {
+                     var info = (TranslationInfo)kvp.Value;
 
-               if( info != null && info.IsTranslated )
+                     if( info != null && info.IsTranslated )
+                     {
+                        SetText( ui, info.TranslatedText, true, info );
+                     }
+                  }
+               }
+               catch( Exception )
                {
-                  SetText( ui, info.TranslatedText, true, info );
+                  // not super pretty, no...
+                  ObjectExtensions.Remove( ui );
                }
             }
          }
@@ -789,11 +790,22 @@ namespace XUnity.AutoTranslator.Plugin.Core
             foreach( var kvp in ObjectExtensions.GetAllRegisteredObjects() )
             {
                var ui = kvp.Key;
-               var info = (TranslationInfo)kvp.Value;
+               try
+               {
+                  if( ( ui as Component )?.gameObject?.activeSelf ?? false )
+                  {
+                     var info = (TranslationInfo)kvp.Value;
 
-               if( info != null )
+                     if( info != null && info.IsTranslated )
+                     {
+                        SetText( ui, info.OriginalText, true, info );
+                     }
+                  }
+               }
+               catch( Exception )
                {
-                  SetText( ui, info.OriginalText, false, info );
+                  // not super pretty, no...
+                  ObjectExtensions.Remove( ui );
                }
             }
          }

+ 5 - 2
src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs

@@ -29,7 +29,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
       public static bool EnableNGUI;
       public static bool EnableTextMeshPro;
       public static bool AllowPluginHookOverride;
-      public static bool IgnoreWhitespaceInKeys;
+      public static bool IgnoreWhitespaceInDialogue;
+      public static int MinDialogueChars;
       public static bool EnableSSL;
 
       public static void Configure()
@@ -42,7 +43,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
          OutputFile = Config.Current.Preferences[ "AutoTranslator" ][ "OutputFile" ].GetOrDefault( @"Translation\_AutoGeneratedTranslations.{lang}.txt" );
          MaxCharactersPerTranslation = Config.Current.Preferences[ "AutoTranslator" ][ "MaxCharactersPerTranslation" ].GetOrDefault( 150 );
          EnablePrintHierarchy = Config.Current.Preferences[ "AutoTranslator" ][ "EnablePrintHierarchy" ].GetOrDefault( false );
-         IgnoreWhitespaceInKeys = Config.Current.Preferences[ "AutoTranslator" ][ "IgnoreWhitespaceInKeys" ].GetOrDefault( true );
+
+         IgnoreWhitespaceInDialogue = Config.Current.Preferences[ "AutoTranslator" ][ "IgnoreWhitespaceInDialogue" ].GetOrDefault( true );
+         MinDialogueChars = Config.Current.Preferences[ "AutoTranslator" ][ "MinDialogueChars" ].GetOrDefault( 18 );
 
          EnableIMGUI = Config.Current.Preferences[ "AutoTranslator" ][ "EnableIMGUI" ].GetOrDefault( true );
          EnableUGUI = Config.Current.Preferences[ "AutoTranslator" ][ "EnableUGUI" ].GetOrDefault( true );

+ 1 - 1
src/XUnity.AutoTranslator.Plugin.Core/Constants/PluginInfo.cs

@@ -12,6 +12,6 @@ namespace XUnity.AutoTranslator.Plugin.Core.Constants
 
       public const string Name = "XUnity Auto Translator";
 
-      public const string Version = "2.3.0";
+      public const string Version = "2.3.2";
    }
 }

+ 8 - 0
src/XUnity.AutoTranslator.Plugin.Core/Extensions/ObjectExtensions.cs

@@ -57,5 +57,13 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
             return DynamicFields.ToList();
          }
       }
+
+      public static void Remove( object obj )
+      {
+         lock( Sync )
+         {
+            DynamicFields.Remove( obj );
+         }
+      }
    }
 }

+ 4 - 8
src/XUnity.AutoTranslator.Plugin.Core/Extensions/StringExtensions.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using XUnity.AutoTranslator.Plugin.Core.Configuration;
 
 namespace XUnity.AutoTranslator.Plugin.Core.Extensions
 {
@@ -9,10 +10,10 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
    {
       public static string ChangeToSingleLineForDialogue( this string that )
       {
-         if( that.Length > 18 ) // long strings often indicate dialog
+         if( that.Length > Settings.MinDialogueChars ) // long strings often indicate dialog
          {
             // Always change dialogue into one line. Otherwise translation services gets confused.
-            return that.RemoveNewlines();
+            return that.RemoveWhitespace();
          }
          else
          {
@@ -20,15 +21,10 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
          }
       }
 
-      public static string RemoveNewlines( this string text )
-      {
-         return text.Replace( "\n", "" ).Replace( "\r", "" );
-      }
-
       public static string RemoveWhitespace( this string text )
       {
          // Japanese whitespace, wtf
-         return text.RemoveNewlines().Replace( " ", "" ).Replace( " ", "" );
+         return text.Replace( "\n", "" ).Replace( "\r", "" ).Replace( " ", "" ).Replace( " ", "" );
       }
 
       public static string UnescapeJson( this string str )

+ 2 - 2
src/XUnity.AutoTranslator.Plugin.Core/TranslationJob.cs

@@ -13,8 +13,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
       public string UntranslatedText { get; set; }
 
-      public string TranslatedText { get; set; }
+      public string UntranslatedDialogueText { get; set; }
 
-      public int Failures { get; set; }
+      public string TranslatedText { get; set; }
    }
 }

+ 1 - 1
src/XUnity.AutoTranslator.Plugin.Core/Utilities/TextHelper.cs

@@ -7,7 +7,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Utilities
 {
    public static class TextHelper
    {
-      private static readonly Dictionary<string, Func<string, bool>> LanguageSymbolChecks = new Dictionary<string, Func<string, bool>>()
+      private static readonly Dictionary<string, Func<string, bool>> LanguageSymbolChecks = new Dictionary<string, Func<string, bool>>( StringComparer.OrdinalIgnoreCase )
       {
          { "ja", ContainsJapaneseSymbols },
          { "ja-JP", ContainsJapaneseSymbols },

+ 3 - 3
src/XUnity.AutoTranslator.Plugin.Core/XUnity.AutoTranslator.Plugin.Core.csproj

@@ -2,9 +2,9 @@
 
   <PropertyGroup>
     <TargetFramework>net35</TargetFramework>
-    <AssemblyVersion>2.3.1.0</AssemblyVersion>
-    <FileVersion>2.3.1.0</FileVersion>
-    <Version>2.3.1</Version>
+    <AssemblyVersion>2.3.2.0</AssemblyVersion>
+    <FileVersion>2.3.2.0</FileVersion>
+    <Version>2.3.2</Version>
   </PropertyGroup>
 
   <ItemGroup>

+ 3 - 3
src/XUnity.AutoTranslator.Plugin.IPA/XUnity.AutoTranslator.Plugin.IPA.csproj

@@ -2,9 +2,9 @@
 
    <PropertyGroup>
       <TargetFramework>net35</TargetFramework>
-      <AssemblyVersion>2.3.1.0</AssemblyVersion>
-      <FileVersion>2.3.1.0</FileVersion>
-      <Version>2.3.1</Version>
+      <AssemblyVersion>2.3.2.0</AssemblyVersion>
+      <FileVersion>2.3.2.0</FileVersion>
+      <Version>2.3.2</Version>
    </PropertyGroup>
 
    <ItemGroup>