|
@@ -31,8 +31,6 @@ namespace BingTranslate
|
|
private static readonly string HttpsTranslateUserSite = "https://www.bing.com/translator";
|
|
private static readonly string HttpsTranslateUserSite = "https://www.bing.com/translator";
|
|
private static readonly string RequestTemplate = "&text={0}&from={1}&to={2}";
|
|
private static readonly string RequestTemplate = "&text={0}&from={1}&to={2}";
|
|
private static readonly Random RandomNumbers = new Random();
|
|
private static readonly Random RandomNumbers = new Random();
|
|
- private static readonly string TranslationSeparator = "---";
|
|
|
|
- private static readonly string TranslationSeparatorWithNewlines = "\r\n---\r\n";
|
|
|
|
|
|
|
|
private static readonly string[] Accepts = new string[] { "*/*" };
|
|
private static readonly string[] Accepts = new string[] { "*/*" };
|
|
private static readonly string[] AcceptLanguages = new string[] { null, "en-US,en;q=0.9", "en-US", "en" };
|
|
private static readonly string[] AcceptLanguages = new string[] { null, "en-US,en;q=0.9", "en-US", "en" };
|
|
@@ -64,8 +62,6 @@ namespace BingTranslate
|
|
|
|
|
|
public override string FriendlyName => "Bing Translator";
|
|
public override string FriendlyName => "Bing Translator";
|
|
|
|
|
|
- public override int MaxTranslationsPerRequest => 10;
|
|
|
|
-
|
|
|
|
public override void Initialize( IInitializationContext context )
|
|
public override void Initialize( IInitializationContext context )
|
|
{
|
|
{
|
|
// Configure service points / service point manager
|
|
// Configure service points / service point manager
|
|
@@ -102,11 +98,9 @@ namespace BingTranslate
|
|
address = string.Format( HttpsServicePointTemplateUrl, _ig, _iid, _translationCount );
|
|
address = string.Format( HttpsServicePointTemplateUrl, _ig, _iid, _translationCount );
|
|
}
|
|
}
|
|
|
|
|
|
- var requestData = string.Join( TranslationSeparatorWithNewlines, context.UntranslatedTexts );
|
|
|
|
-
|
|
|
|
var data = string.Format(
|
|
var data = string.Format(
|
|
RequestTemplate,
|
|
RequestTemplate,
|
|
- Uri.EscapeDataString( requestData ),
|
|
|
|
|
|
+ Uri.EscapeDataString( context.UntranslatedText ),
|
|
context.SourceLanguage,
|
|
context.SourceLanguage,
|
|
context.DestinationLanguage );
|
|
context.DestinationLanguage );
|
|
|
|
|
|
@@ -131,37 +125,9 @@ namespace BingTranslate
|
|
if( code != 200 ) context.Fail( "Bad response code received from service: " + code );
|
|
if( code != 200 ) context.Fail( "Bad response code received from service: " + code );
|
|
|
|
|
|
var token = obj[ "translationResponse" ].ToString();
|
|
var token = obj[ "translationResponse" ].ToString();
|
|
- var allTranslatedText = JsonHelper.Unescape( token.Substring( 1, token.Length - 2 ) );
|
|
|
|
-
|
|
|
|
- if( context.UntranslatedTexts.Length > 1 )
|
|
|
|
- {
|
|
|
|
- var translatedTexts = allTranslatedText
|
|
|
|
- .Split( new[] { TranslationSeparator }, StringSplitOptions.RemoveEmptyEntries )
|
|
|
|
- .Select( x => x.Trim( ' ', '\r', '\n' ) )
|
|
|
|
- .ToArray();
|
|
|
|
-
|
|
|
|
- if( translatedTexts.Length != context.UntranslatedTexts.Length ) context.Fail( "Batch operation received incorrect number of translations." );
|
|
|
|
-
|
|
|
|
- // lets fix the translation line breaks
|
|
|
|
- for( int i = 0 ; i < translatedTexts.Length ; i++ )
|
|
|
|
- {
|
|
|
|
- var translatedText = translatedTexts[ i ];
|
|
|
|
- var untranslatedText = context.UntranslatedTexts[ i ];
|
|
|
|
|
|
+ var translatedText = JsonHelper.Unescape( token.Substring( 1, token.Length - 2 ) );
|
|
|
|
|
|
- if( !untranslatedText.Contains( "\n" ) )
|
|
|
|
- {
|
|
|
|
- translatedText = translatedText.Replace( "\r\n", " " ).Replace( "\n", " " ).Replace( " ", " " );
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- translatedTexts[ i ] = translatedText;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- context.Complete( translatedTexts );
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- context.Complete( allTranslatedText );
|
|
|
|
- }
|
|
|
|
|
|
+ context.Complete( translatedText );
|
|
}
|
|
}
|
|
|
|
|
|
private XUnityWebRequest CreateWebSiteRequest()
|
|
private XUnityWebRequest CreateWebSiteRequest()
|