浏览代码

added feature to force split characters

gravydevsupreme 7 年之前
父节点
当前提交
158ec07552

+ 8 - 2
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -211,7 +211,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          {
             return job;
          }
-         
+
          foreach( var completedJob in _completedJobs )
          {
             if( completedJob.UntranslatedText == untranslatedText )
@@ -409,7 +409,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
          if( !string.IsNullOrEmpty( text ) && IsTranslatable( text ) && ShouldTranslate( ui ) && !IsCurrentlySetting( 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 ) )
@@ -602,6 +602,12 @@ namespace XUnity.AutoTranslator.Plugin.Core
             {
                _consecutiveErrors = 0;
 
+               if( Settings.ForceSplitTextAfterCharacters > 0 )
+               {
+                  translatedText = translatedText.SplitToLines( Settings.ForceSplitTextAfterCharacters, '\n', ' ', ' ' );
+               }
+
+
                job.TranslatedText = translatedText;
 
                if( !string.IsNullOrEmpty( translatedText ) )

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

@@ -32,9 +32,9 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
       public static bool IgnoreWhitespaceInDialogue;
       public static int MinDialogueChars;
       public static bool EnableSSL;
-
       public static string BaiduAppId;
       public static string BaiduAppSecret;
+      public static int ForceSplitTextAfterCharacters;
 
       public static void Configure()
       {
@@ -74,7 +74,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
          Delay = Config.Current.Preferences[ "Behaviour" ][ "Delay" ].GetOrDefault( 0f );
          MaxCharactersPerTranslation = Config.Current.Preferences[ "Behaviour" ][ "MaxCharactersPerTranslation" ].GetOrDefault( 150 );
          IgnoreWhitespaceInDialogue = Config.Current.Preferences[ "Behaviour" ][ "IgnoreWhitespaceInDialogue" ].GetOrDefault( true );
-         MinDialogueChars = Config.Current.Preferences[ "Behaviour" ][ "MinDialogueChars" ].GetOrDefault( 18 );
+         MinDialogueChars = Config.Current.Preferences[ "Behaviour" ][ "MinDialogueChars" ].GetOrDefault( 20 );
+         ForceSplitTextAfterCharacters = Config.Current.Preferences[ "Behaviour" ][ "ForceSplitTextAfterCharacters" ].GetOrDefault( 0 );
 
          BaiduAppId = Config.Current.Preferences[ "Baidu" ][ "BaiduAppId" ].GetOrDefault( "" );
          BaiduAppSecret = Config.Current.Preferences[ "Baidu" ][ "BaiduAppSecret" ].GetOrDefault( "" );

+ 27 - 0
src/XUnity.AutoTranslator.Plugin.Core/Extensions/StringExtensions.cs

@@ -22,6 +22,33 @@ namespace XUnity.AutoTranslator.Plugin.Core.Extensions
          }
       }
 
+      public static string SplitToLines( this string text, int maxStringLength, params char[] splitOnCharacters )
+      {
+         var sb = new StringBuilder();
+         var index = 0;
+
+         while( text.Length > index )
+         {
+            // start a new line, unless we've just started
+            if( index != 0 )
+               sb.Append( '\n' );
+
+            // get the next substring, else the rest of the string if remainder is shorter than `maxStringLength`
+            var splitAt = index + maxStringLength <= text.Length
+                ? text.Substring( index, maxStringLength ).LastIndexOfAny( splitOnCharacters )
+                : text.Length - index;
+
+            // if can't find split location, take `maxStringLength` characters
+            splitAt = ( splitAt == -1 ) ? maxStringLength : splitAt;
+
+            // add result to collection & increment index
+            sb.Append( text.Substring( index, splitAt ).Trim() );
+            index += splitAt;
+         }
+
+         return sb.ToString();
+      }
+
       public static string RemoveWhitespace( this string text )
       {
          // Japanese whitespace, wtf