Exemple #1
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;
     }
 }
        /// <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;
                }
            }
        }