Пример #1
0
        protected internal void HandleTranslationComplete(object sender, TranslateArrayCompletedEventArgs e)
        {
            // look up the id from e
            if (e.UserState == null)
            {
                string message = string.Format("UserState from async translation is null, expected it to be an istance of BingTranslatorUserState.");
                throw new UnexpectedStateException(message);
            }

            // BingTranslatorUserState userState = new BingTranslatorUserState(e.UserState as object[]);
            BingTranslatorUserState userState = e.UserState as BingTranslatorUserState;
            // see if there are any registered handlers
            List <IAsyncTranslationComplete> handlers = null;

            this.AsyncTranslationHandlers.TryGetValue(userState.Id, out handlers);

            // remove the id from the dictionary then call the events, they may take time to execute
            //      that is why they are removed first
            if (handlers != null)
            {
                lock (lockAsyncTranslationHandlers) {
                    this.AsyncTranslationHandlers[userState.Id] = null;
                }

                // create the object that will be passed to the handlers
                List <ITranslation> translationResult = new List <ITranslation>();

                foreach (IAsyncTranslationComplete handler in handlers)
                {
                    List <string> stringsToTranslate = handler.TranslationAsync.StringsToTranslate;
                    if (stringsToTranslate.Count != e.Result.Length)
                    {
                        string message = string.Format(
                            "There was a mis-match between the count of strings to translate [{0}] and the translated strings [{1}].",
                            stringsToTranslate.Count,
                            e.Result.Length);
                        throw new UnexpectedStateException(message);
                    }

                    List <ITranslation> translations = new List <ITranslation>(stringsToTranslate.Count);

                    for (int i = 0; i < stringsToTranslate.Count; i++)
                    {
                        translations.Add(
                            new Translation(
                                handler.TranslationAsync.SourceLanguage,
                                handler.TranslationAsync.DestLanguage,
                                stringsToTranslate[i],
                                e.Result[i].TranslatedText));
                    }

                    handler.OnTranslationComplete(handler.TranslationAsync, translations);
                }
            }
        }
Пример #2
0
        private void client_TranslateArrayCompleted(object sender, TranslateArrayCompletedEventArgs e)
        {
            object[] state    = (object[])e.UserState;
            var      fileName = state[0].ToString();
            var      dic      = (Dictionary <string, string>)state[1];
            var      keys     = (List <string>)state[2];

            for (var i = 0; i < e.Result.Length; i++)
            {
                dic.Add(keys[i], e.Result[i].TranslatedText);
            }

            CreateResxFile(fileName, dic);
        }
Пример #3
0
        protected internal void HandleTranslationComplete(object sender, TranslateArrayCompletedEventArgs e)
        {
            // look up the id from e
            if (e.UserState == null) {
                string message = string.Format("UserState from async translation is null, expected it to be an istance of BingTranslatorUserState.");
                throw new UnexpectedStateException(message);
            }

            // BingTranslatorUserState userState = new BingTranslatorUserState(e.UserState as object[]);
            BingTranslatorUserState userState = e.UserState as BingTranslatorUserState;
            // see if there are any registered handlers
            List<IAsyncTranslationComplete> handlers = null;
            this.AsyncTranslationHandlers.TryGetValue(userState.Id, out handlers);

            // remove the id from the dictionary then call the events, they may take time to execute
            //      that is why they are removed first
            if (handlers != null) {
                lock (lockAsyncTranslationHandlers) {
                    this.AsyncTranslationHandlers[userState.Id] = null;
                }

                // create the object that will be passed to the handlers
                List<ITranslation> translationResult = new List<ITranslation>();

                foreach (IAsyncTranslationComplete handler in handlers) {
                    List<string> stringsToTranslate = handler.TranslationAsync.StringsToTranslate;
                    if (stringsToTranslate.Count != e.Result.Length) {
                        string message = string.Format(
                            "There was a mis-match between the count of strings to translate [{0}] and the translated strings [{1}].",
                            stringsToTranslate.Count,
                            e.Result.Length);
                        throw new UnexpectedStateException(message);
                    }

                    List<ITranslation> translations = new List<ITranslation>(stringsToTranslate.Count);

                    for (int i = 0; i < stringsToTranslate.Count; i++) {
                        translations.Add(
                            new Translation(
                                handler.TranslationAsync.SourceLanguage,
                                handler.TranslationAsync.DestLanguage,
                                stringsToTranslate[i],
                                e.Result[i].TranslatedText));
                    }

                    handler.OnTranslationComplete(handler.TranslationAsync,translations);
                }
            }
        }
Пример #4
0
        private void client_TranslateArrayCompleted(object sender, TranslateArrayCompletedEventArgs e)
        {
            object[] state = (object[])e.UserState;
            var fileName = state[0].ToString();
            var dic = (Dictionary<string, string>)state[1];
            var keys = (List<string>)state[2];

            for (var i = 0; i < e.Result.Length; i++)
            {
                dic.Add(keys[i], e.Result[i].TranslatedText);
            }

            CreateResxFile(fileName, dic);
        }