Exemple #1
0
        /// <summary>
        /// Get the translation of an xliff file using the ETS API.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="languageDirection"></param>
        /// <param name="xliffFile"></param>
        /// <returns></returns>
        public static string GetTranslation(TranslationOptions options,
                                            LanguagePair languageDirection,
                                            Xliff xliffFile)
        {
            Log.Logger.Trace("");
            var text         = xliffFile.ToString();
            var queryString  = HttpUtility.ParseQueryString(string.Empty);
            var encodedInput = text.Base64Encode();

            lock (optionsLock)
            {
                if (options.ApiVersion == APIVersion.Unknown)
                {
                    SetETSApiVersion(options);
                }
            }

            if (options.ApiVersion == APIVersion.v1)
            {
                queryString["sourceLanguageId"] = languageDirection.SourceCulture.ToETSCode();
                queryString["targetLanguageId"] = languageDirection.TargetCulture.ToETSCode();
                queryString["text"]             = encodedInput;
            }
            else
            {
                // If LPPreferences doesn't contain the target language (source is always the same), figure out the
                // preferred LP. Previously set preferred LPs will stay, so this won't get run each time if you have
                // multiple LPs.
                if (!options.LPPreferences.ContainsKey(languageDirection.TargetCulture))
                {
                    options.SetPreferredLanguages(new LanguagePair[] { languageDirection });
                    if (!options.LPPreferences.ContainsKey(languageDirection.TargetCulture))
                    {
                        throw new Exception("There are no language pairs currently accessible via ETS.");
                    }
                }
                queryString["languagePairId"] = options.LPPreferences[languageDirection.TargetCulture].LanguagePairId;
                queryString["input"]          = encodedInput;
            }
            queryString["inputFormat"] = "application/x-xliff";

            Log.Logger.Debug("Sending translation request for: {0}", encodedInput);
            string jsonResult;

            try
            {
                jsonResult = ContactETSServer(ETSPost, options, "translations/quick", queryString);
            }
            catch (Exception e)
            {
                Log.Logger.Error($"{Constants.Translation}: {e.Message}\n {e.StackTrace}\n Encoded Input: {encodedInput}");
                throw;
            }

            var encodedTranslation = JsonConvert.DeserializeObject <ETSTranslationOutput>(jsonResult).Translation;
            var decodedTranslation = encodedTranslation.Base64Decode();

            Log.Logger.Debug("Resultant translation is: {0}", encodedTranslation);
            return(decodedTranslation);
        }
        /// <summary>
        /// Get the translation of an xliff file using the MTEdge API.
        /// </summary>
        public static string GetTranslation(TranslationOptions options,
                                            LanguagePair languageDirection,
                                            Xliff xliffFile)
        {
            var text         = xliffFile.ToString();
            var queryString  = new Dictionary <string, string>();
            var encodedInput = text.Base64Encode();

            lock (optionsLock)
            {
                if (options.ApiVersion == APIVersion.Unknown)
                {
                    SetMtEdgeApiVersion(options);
                }
            }

            if (options.ApiVersion == APIVersion.v1)
            {
                queryString["sourceLanguageId"] = languageDirection.SourceCulture.ToMTEdgeCode();
                queryString["targetLanguageId"] = languageDirection.TargetCulture.ToMTEdgeCode();
                queryString["text"]             = encodedInput;
            }
            else
            {
                // If LPPreferences doesn't contain the target language (source is always the same), figure out the
                // preferred LP. Previously set preferred LPs will stay, so this won't get run each time if you have
                // multiple LPs.
                if (!options.LPPreferences.ContainsKey(languageDirection.TargetCulture))
                {
                    options.SetPreferredLanguages(new[] { languageDirection });
                    if (!options.LPPreferences.ContainsKey(languageDirection.TargetCulture))
                    {
                        throw new Exception("There are no language pairs currently accessible via MtEdge.");
                    }
                }
                queryString["languagePairId"] = options.LPPreferences[languageDirection.TargetCulture].LanguagePairId;
                if (options.LPPreferences[languageDirection.TargetCulture].DictionaryId != null)
                {
                    if (!options.LPPreferences[languageDirection.TargetCulture].DictionaryId.Equals(Constants.NoDictionary))
                    {
                        queryString["dictionaryIds"] = options.LPPreferences[languageDirection.TargetCulture].DictionaryId;
                    }
                }

                queryString["input"] = encodedInput;
            }
            queryString["inputFormat"] = "application/x-xliff";

            string jsonResult;

            try
            {
                jsonResult = Translate(queryString, options, "translations/quick");
            }
            catch (Exception e)
            {
                _logger.Error($"{Constants.Translation}: {e.Message}\n {e.StackTrace}\n Encoded Input: {encodedInput}");
                throw;
            }

            var encodedTranslation = JsonConvert.DeserializeObject <SDLMTEdgeTranslationOutput>(jsonResult).Translation;
            var decodedTranslation = encodedTranslation.Base64Decode();

            _logger.Debug("Resultant translation is: {0}", encodedTranslation);
            return(decodedTranslation);
        }