internal void InvokeAfterItemAutoTranslation(AutoTranslationResult result)
        {
            EventHandler <AutoTranslationEventArgs <AutoTranslationResult> > handler = AfterItemAutoTranslation;

            if (handler != null)
            {
                handler(this, new AutoTranslationEventArgs <AutoTranslationResult>(result));
            }
        }
Esempio n. 2
0
        public void Translate(IEnumerable <AutoTranslationItem> translationItems, string from, string to)
        {
            if (translationItems == null)
            {
                throw new ArgumentNullException("translationItems");
            }
            if (string.IsNullOrEmpty(from))
            {
                throw new ArgumentNullException("from");
            }
            if (string.IsNullOrEmpty(to))
            {
                throw new ArgumentNullException("to");
            }

            var translator = new LanguageServiceClient();
            var options    = new TranslateOptions();

            foreach (var translationItemsChunk in translationItems.GetChunks(ChunkSize))
            {
                var texts = from p in translationItemsChunk select p.Text.Length <= 2000 ? p.Text : p.Text.Substring(0, 2000);   // 2000 is used because this is the current limit of ms translation services

                // send for translation
                var textsArray = texts.ToArray();
                TranslateArrayResponse[] microsoftTranslatorResponses = translator.TranslateArray(AppId, textsArray, from, to, options);

                // convert the response into our TranslationResult array
                var results     = new List <AutoTranslationResult>();
                var tempCounter = 0;
                foreach (var translationItem in translationItemsChunk)
                {
                    var microsoftTranslatorResponse = microsoftTranslatorResponses[tempCounter];
                    var translationResult           = new AutoTranslationResult
                    {
                        Error        = microsoftTranslatorResponse.Error,
                        Key          = translationItem.Key,
                        Text         = microsoftTranslatorResponse.TranslatedText,
                        OriginalText = translationItem.Text
                    };
                    results.Add(translationResult);

                    tempCounter++;
                }

                // invoke event with the translation
                this.InvokeTranslationReceived(new TranslationReceivedEventArgs(results.ToArray()));

                if (this.stopRequested)
                {
                    break;
                }
            }
        }