Example #1
0
 public void OnTranslation(TranslateOperationResult result)
 {
     if (Translation != null)
     {
         Translation(result);
     }
 }
 /// <summary>
 /// Translate a text
 /// </summary>
 /// <param name="sourceLanguage">source language</param>
 /// <param name="destLanguage">target language</param>
 /// <param name="text">requested text</param>
 /// <returns>A result object</returns>
 public string Translate(string sourceLanguage, string destLanguage, string text)
 {
     try
     {
         LocalTranslationCacheItem cacheItem = Cache.TryGetValue(sourceLanguage, destLanguage, text);
         if (null != cacheItem)
         {
             TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, cacheItem.TranslationText, null, true);
             RaiseOnTranslation(result);
             return cacheItem.TranslationText;
         }
         else
         {
             GoogleTranslator translator = new GoogleTranslator(sourceLanguage, destLanguage, text);
             translator.Translate();
             TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, translator.Translation, null);
             Cache.Add(sourceLanguage, destLanguage, text, translator.Translation);
             RaiseOnTranslation(result);
             return translator.Translation;    
         }
     }
     catch (Exception exception)
     {
         TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Error, text, null, exception);
         RaiseOnTranslation(result);
         throw exception;
     }
 }
Example #3
0
 /// <summary>
 /// Translate a text
 /// </summary>
 /// <param name="sourceLanguage">source language</param>
 /// <param name="destLanguage">target language</param>
 /// <param name="text">requested text</param>
 /// <returns>A result object</returns>
 public string Translate(string sourceLanguage, string destLanguage, string text)
 {
     try
     {
         LocalTranslationCacheItem cacheItem = Cache.TryGetValue(sourceLanguage, destLanguage, text);
         if (null != cacheItem)
         {
             TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, cacheItem.TranslationText, null, true);
             RaiseOnTranslation(result);
             return(cacheItem.TranslationText);
         }
         else
         {
             GoogleTranslator translator = new GoogleTranslator(sourceLanguage, destLanguage, text);
             translator.Translate();
             TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, translator.Translation, null);
             Cache.Add(sourceLanguage, destLanguage, text, translator.Translation);
             RaiseOnTranslation(result);
             return(translator.Translation);
         }
     }
     catch (Exception exception)
     {
         TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Error, text, null, exception);
         RaiseOnTranslation(result);
         throw exception;
     }
 }
 private void RaiseOnTranslation(TranslateOperationResult result)
 {
     foreach (var item in Repeators)
     {
         try
         {
             item.OnTranslation(result);
         }
         catch (Exception exception)
         {
             System.Diagnostics.Trace.WriteLine("Exception occured :" + exception.Message);
         }
     }
 }
        /// <summary>
        /// Translate a text (should be async in a real-life scenario)
        /// </summary>
        /// <param name="sourceLanguage">source language</param>
        /// <param name="destLanguage">target language</param>
        /// <param name="text">requested text</param>
        /// <returns>A result object</returns>
        public string Translate(string sourceLanguage, string destLanguage, string text)
        {
            if (String.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            lock (_lock)
            {
                try
                {
                    string source = null;
                    string dest   = null;
                    if (!LanguageModeMap.TryGetValue(sourceLanguage, out source))
                    {
                        throw new ArgumentOutOfRangeException("Unkown language: " + sourceLanguage);
                    }
                    if (!LanguageModeMap.TryGetValue(destLanguage, out dest))
                    {
                        throw new ArgumentOutOfRangeException("Unkown language" + destLanguage);
                    }

                    text = text.Trim();

                    LocalTranslationCacheItem cacheItem = Cache.TryGetValue(source, dest, text);
                    if (null != cacheItem)
                    {
                        TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, cacheItem.TranslationText, null, true);
                        RaiseOnTranslation(result);
                        return(cacheItem.TranslationText);
                    }
                    else
                    {
                        string translatedText           = TranslateText(text, String.Format("{0}|{1}", source, dest));
                        TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, translatedText, null);
                        Cache.Add(source, dest, text, translatedText);
                        RaiseOnTranslation(result);
                        return(translatedText);
                    }
                }
                catch (Exception exception)
                {
                    TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Error, text, null, exception);
                    RaiseOnTranslation(result);
                    throw exception;
                }
            }
        }
 private void RaiseOnTranslation(TranslateOperationResult result)
 {
     foreach (var item in Reapters)
     {
         try
         {
             item.OnTranslation(result);
         }
         catch (Exception exception)
         {
             System.Diagnostics.Trace.WriteLine("Exception occured :" + exception.Message);
         }            
     }
 }
Example #7
0
 /// <summary>
 /// This method was called from the IPC Service instance for state updates
 /// This is a very stupid solution because the service is calling the view directly and wait until its finished
 /// Never do this in a real-life scenario because the view can block/slow down the service
 /// </summary>
 /// <param name="result">operation state</param>
 private void ServiceOnTranslation(TranslateOperationResult result)
 {
     if (this.InvokeRequired)
     {
         Action<TranslateOperationResult> invoker = ServiceOnTranslation;
         invoker.Invoke(result);
     }
     else
     {
         switch (result.State)
         {
             case TranslateOperationState.Sucseed:
                 DisplayTranslationMessage(result.Requested, result.Result, result.Cached);
                 break;
             case TranslateOperationState.Error:
                 DisplayFailedMessage(result.Exception);
                 break;
             default:
                 throw new IndexOutOfRangeException("state");
         }
     }
 }
        /// <summary>
        /// Translate a text (should be async in a real-life scenario)
        /// </summary>
        /// <param name="sourceLanguage">source language</param>
        /// <param name="destLanguage">target language</param>
        /// <param name="text">requested text</param>
        /// <returns>A result object</returns>
        public string Translate(string sourceLanguage, string destLanguage, string text)
        {
            if (String.IsNullOrWhiteSpace(text))
                return text;

            lock (_lock)
            {
                try
                {
                    string source = null;
                    string dest = null;
                    if (!LanguageModeMap.TryGetValue(sourceLanguage, out source))
                        throw new ArgumentOutOfRangeException("Unkown language: " + sourceLanguage);
                    if (!LanguageModeMap.TryGetValue(destLanguage, out dest))
                        throw new ArgumentOutOfRangeException("Unkown language" + destLanguage);

                    text = text.Trim();

                    LocalTranslationCacheItem cacheItem = Cache.TryGetValue(source, dest, text);
                    if (null != cacheItem)
                    {
                        TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, cacheItem.TranslationText, null, true);
                        RaiseOnTranslation(result);
                        return cacheItem.TranslationText;
                    }
                    else
                    {
                        string translatedText = TranslateText(text, String.Format("{0}|{1}", source, dest));
                        TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Sucseed, text, translatedText, null);
                        Cache.Add(source, dest, text, translatedText);
                        RaiseOnTranslation(result);
                        return translatedText;
                    }
                }
                catch (Exception exception)
                {
                    TranslateOperationResult result = new TranslateOperationResult(TranslateOperationState.Error, text, null, exception);
                    RaiseOnTranslation(result);
                    throw exception;
                }
            }
        }
Example #9
0
 /// <summary>
 /// Helper method to perform a status update in UI thread
 /// </summary>
 private void ServiceOnTranslationInUIThread()
 {
     if (null != TranslationResult)
     { 
         ServiceOnTranslation(TranslationResult);
         TranslationResult = null;
     }
 }
Example #10
0
 /// <summary>
 /// This Trigger was called from the IPC Service Instance for status update
 /// </summary>
 /// <param name="result"></param>
 private void ServiceOnTranslation(TranslateOperationResult result)
 {
     if (this.InvokeRequired)
     {
         TranslationResult = result;
         MethodInvoker uiThreadHandler = new MethodInvoker(ServiceOnTranslationInUIThread);
         this.Invoke(uiThreadHandler);
     }
     else
     {
         switch (result.State)
         {
             case TranslateOperationState.Sucseed:
                 
                 int imageIndex = 1;
                 if(result.Requested.Trim().Equals(result.Result, StringComparison.InvariantCultureIgnoreCase))
                     imageIndex = 2;
                 DisplayMessage(string.Format("Translation sucseed \"{0}\"=>\"{1}\"; FromLocalCache={2}", result.Requested, result.Result, result.Cached), imageIndex);
                 break;
             case TranslateOperationState.Error:
                 DisplayMessage(string.Format("Translation failed because {0}" , result.Exception.ToString()), 3);
                 break;
             default:
                 throw new IndexOutOfRangeException("state");
         }
     }
 }