Ejemplo n.º 1
0
        public ITranslation Translate(string stringToTranslate, ILanguage destLanguage, ILanguage sourceLanguage = null)
        {
            if (string.IsNullOrEmpty(stringToTranslate))
            {
                throw new ArgumentNullException("stringToTranslate");
            }

            if (sourceLanguage == null)
            {
                sourceLanguage = this.DetectLanguage(stringToTranslate);
            }

            ITranslation translation = BingLocanTranslator.GetTranslationFromCache(stringToTranslate, sourceLanguage, destLanguage);

            if (translation == null)
            {
                string translationResult = null;
                using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                    translationResult = client.Translate(
                        this.ApiKey,
                        stringToTranslate,
                        sourceLanguage.Language,
                        destLanguage.Language,
                        this.TranslateOptions.ContentType,
                        this.TranslateOptions.Category);
                }

                translation = new Translation(sourceLanguage, destLanguage, stringToTranslate, translationResult);
            }

            return(translation);
        }
Ejemplo n.º 2
0
        public TranslateArrayResponse[] TranslateSync(IEnumerable <string> stringsToTranslate, ILanguage destLanguage, ILanguage sourceLanguage = null)
        {
            if (stringsToTranslate == null)
            {
                throw new ArgumentNullException("stringsToTranslate");
            }

            // Convert stringsToTranslate to a list because I need to send them all at once
            //      to the translation engine. We can only get benefit of IEnumberable in one direction
            List <string> stringList = stringsToTranslate.ToList();

            if (sourceLanguage == null)
            {
                // detect language from the first string in the list to translate
                sourceLanguage = this.DetectLanguage(stringsToTranslate.First());
            }

            Guid translationGuid = Guid.NewGuid();


            // TODO: Do we want this to be done async? if so update this code
            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService())
            {
                return(client.TranslateArray(
                           this.ApiKey,
                           stringList.ToArray(),
                           sourceLanguage.Language,
                           destLanguage.Language,
                           this.TranslateOptions));
            }
        }
Ejemplo n.º 3
0
        public IEnumerable <ILanguage> GetSupportedLanguages()
        {
            if (this.supportedLanguages == null)
            {
                lock (lockSupportedLanguages) {
                    if (this.supportedLanguages == null)
                    {
                        // Get languages
                        using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                            IList <ILanguage> langList = new List <ILanguage>();

                            string[] languages = client.GetLanguagesForTranslate(this.ApiKey);
                            languages.ToList().ForEach(lang => {
                                langList.Add(
                                    new BaseLanguage(lang));
                            });

                            this.supportedLanguages = langList;
                        }
                    }
                }
            }

            return(this.supportedLanguages);
        }
Ejemplo n.º 4
0
        public void AddCustomTranslations(ILanguage sourceLanguage, ILanguage destLanguage, IDictionary<string,string>customTranslations)
        {
            if (sourceLanguage == null) { throw new ArgumentNullException("soureLanguage"); }
            if (destLanguage == null) { throw new ArgumentNullException("destLanguage"); }
            if (customTranslations == null) { throw new ArgumentNullException("customTranslations"); }

            // TODO: This could be done Async

            IList<BingTranslatorService.Translation> translationList = new List<BingTranslatorService.Translation>();
            foreach (string key in customTranslations.Keys) {
                if (!string.IsNullOrWhiteSpace(key)) {
                    BingTranslatorService.Translation translation = new BingTranslatorService.Translation();
                    translation.OriginalText = key;
                    translation.TranslatedText = customTranslations[key];
                    // make it less than 5 because that is the max value for machine translations
                    translation.Rating = this.CustomTranslationRating;
                    translation.RatingSpecified = true;
                    translation.Sequence = 0;
                    translation.SequenceSpecified = true;
                    translationList.Add(translation);
                }
            }

            // TODO: We should batch these into 100 because according to http://msdn.microsoft.com/en-us/library/ff512409.aspx that is the limit

            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                client.AddTranslationArray(this.ApiKey, translationList.ToArray(), sourceLanguage.Language, destLanguage.Language, this.TranslateOptions);
            }
        }
Ejemplo n.º 5
0
        public void TestAddTranslation()
        {
            IList <BingTranslatorService.Translation> translationList = new List <BingTranslatorService.Translation> {
                new BingTranslatorService.Translation {
                    OriginalText      = "Original text",
                    TranslatedText    = "custom translation here",
                    Rating            = 1,
                    RatingSpecified   = true,
                    Sequence          = 0,
                    SequenceSpecified = true
                }
            };

            BingTranslatorService.TranslateOptions translateOptions = new BingTranslatorService.TranslateOptions {
                Category    = "tech",
                ContentType = "text/plain",
                User        = "******"
            };

            string fromLanguage = "en";
            string toLanguage   = "pt";

            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                client.AddTranslationArray(
                    this.BingApiKey,
                    translationList.ToArray(),
                    fromLanguage,
                    toLanguage,
                    translateOptions);

                foreach (BingTranslatorService.Translation translation in translationList)
                {
                    string result = client.Translate(
                        this.BingApiKey,
                        translation.OriginalText,
                        fromLanguage,
                        toLanguage,
                        translateOptions.ContentType,
                        translateOptions.Category);
                    BingTranslatorService.GetTranslationsResponse allTranslations = client.GetTranslations(
                        this.BingApiKey,
                        translation.OriginalText,
                        fromLanguage,
                        toLanguage,
                        10,
                        true,
                        translateOptions);

                    IList <BingTranslatorService.TranslationMatch> translationMatches = allTranslations.Translations.ToList();
                }
            }
        }
Ejemplo n.º 6
0
        public void TestAddTranslation()
        {
            IList<BingTranslatorService.Translation> translationList = new List<BingTranslatorService.Translation>{
                    new BingTranslatorService.Translation {
                    OriginalText="Original text",
                    TranslatedText="custom translation here",
                    Rating=1,
                    RatingSpecified=true,
                    Sequence=0,
                    SequenceSpecified=true
                    }
                };

            BingTranslatorService.TranslateOptions translateOptions = new BingTranslatorService.TranslateOptions {
                Category = "tech",
                ContentType = "text/plain",
                User = "******"
            };

            string fromLanguage = "en";
            string toLanguage = "pt";

            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                client.AddTranslationArray(
                    this.BingApiKey,
                    translationList.ToArray(),
                    fromLanguage,
                    toLanguage,
                    translateOptions);

                foreach (BingTranslatorService.Translation translation in translationList) {
                    string result = client.Translate(
                        this.BingApiKey,
                        translation.OriginalText,
                        fromLanguage,
                        toLanguage,
                        translateOptions.ContentType,
                        translateOptions.Category);
                    BingTranslatorService.GetTranslationsResponse allTranslations = client.GetTranslations(
                        this.BingApiKey,
                        translation.OriginalText,
                        fromLanguage,
                        toLanguage,
                        10,
                        true,
                        translateOptions);

                    IList<BingTranslatorService.TranslationMatch> translationMatches = allTranslations.Translations.ToList();
                }
            }
        }
Ejemplo n.º 7
0
        public ILanguage DetectLanguage(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException("text");
            }

            ILanguage result = null;

            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                string lang = client.Detect(this.ApiKey, text);
                result = new BaseLanguage(lang);
            }
            return(result);
        }
Ejemplo n.º 8
0
        public void AddCustomTranslations(ILanguage sourceLanguage, ILanguage destLanguage, IDictionary <string, string> customTranslations)
        {
            if (sourceLanguage == null)
            {
                throw new ArgumentNullException("soureLanguage");
            }
            if (destLanguage == null)
            {
                throw new ArgumentNullException("destLanguage");
            }
            if (customTranslations == null)
            {
                throw new ArgumentNullException("customTranslations");
            }

            // TODO: This could be done Async

            IList <BingTranslatorService.Translation> translationList = new List <BingTranslatorService.Translation>();

            foreach (string key in customTranslations.Keys)
            {
                if (!string.IsNullOrWhiteSpace(key))
                {
                    BingTranslatorService.Translation translation = new BingTranslatorService.Translation();
                    translation.OriginalText   = key;
                    translation.TranslatedText = customTranslations[key];
                    // make it less than 5 because that is the max value for machine translations
                    translation.Rating            = this.CustomTranslationRating;
                    translation.RatingSpecified   = true;
                    translation.Sequence          = 0;
                    translation.SequenceSpecified = true;
                    translationList.Add(translation);
                }
            }

            // TODO: We should batch these into 100 because according to http://msdn.microsoft.com/en-us/library/ff512409.aspx that is the limit

            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                client.AddTranslationArray(this.ApiKey, translationList.ToArray(), sourceLanguage.Language, destLanguage.Language, this.TranslateOptions);
            }
        }
Ejemplo n.º 9
0
        public IAsyncContinuation Translate2(IEnumerable <string> stringsToTranslate, ILanguage destLanguage, ILanguage sourceLanguage = null)
        {
            if (stringsToTranslate == null)
            {
                throw new ArgumentNullException("stringsToTranslate");
            }

            // Convert stringsToTranslate to a list because I need to send them all at once
            //      to the translation engine. We can only get benefit of IEnumberable in one direction
            List <string> stringList = stringsToTranslate.ToList();

            if (sourceLanguage == null)
            {
                // detect language from the first string in the list to translate
                sourceLanguage = this.DetectLanguage(stringsToTranslate.First());
            }

            Guid translationGuid = Guid.NewGuid();

            IAsyncTranslationPayload asyncObj  = new AsyncTranslationPayload(Guid.NewGuid(), stringList, sourceLanguage, destLanguage, this);
            BingTranslatorUserState  userState = new BingTranslatorUserState {
                Id             = asyncObj.Id,
                SourceLanguage = sourceLanguage as BaseLanguage,
                DestLanguage   = destLanguage as BaseLanguage
            };

            // TODO: Do we want this to be done async? if so update this code
            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                client.TranslateArrayAsync(
                    this.ApiKey,
                    stringList.ToArray(),
                    sourceLanguage.Language,
                    destLanguage.Language,
                    this.TranslateOptions,
                    userState);
                client.TranslateArrayCompleted += HandleTranslationComplete;
            }

            return(asyncObj);
        }
Ejemplo n.º 10
0
        public ILanguage DetectLanguage(string text)
        {
            if (string.IsNullOrEmpty(text)) { throw new ArgumentNullException("text"); }

            ILanguage result = null;
            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                string lang = client.Detect(this.ApiKey, text);
                result = new BaseLanguage(lang);
            }
            return result;
        }
Ejemplo n.º 11
0
        public TranslateArrayResponse[] TranslateSync(IEnumerable<string> stringsToTranslate, ILanguage destLanguage, ILanguage sourceLanguage = null)
        {
            if (stringsToTranslate == null) { throw new ArgumentNullException("stringsToTranslate"); }

            // Convert stringsToTranslate to a list because I need to send them all at once
            //      to the translation engine. We can only get benefit of IEnumberable in one direction
            List<string> stringList = stringsToTranslate.ToList();

            if (sourceLanguage == null)
            {
                // detect language from the first string in the list to translate
                sourceLanguage = this.DetectLanguage(stringsToTranslate.First());
            }

            Guid translationGuid = Guid.NewGuid();

            // TODO: Do we want this to be done async? if so update this code
            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService())
            {
                return client.TranslateArray(
                    this.ApiKey,
                    stringList.ToArray(),
                    sourceLanguage.Language,
                    destLanguage.Language,
                    this.TranslateOptions);
            }
        }
Ejemplo n.º 12
0
        public IAsyncContinuation Translate2(IEnumerable<string> stringsToTranslate, ILanguage destLanguage, ILanguage sourceLanguage = null)
        {
            if (stringsToTranslate == null) { throw new ArgumentNullException("stringsToTranslate"); }

            // Convert stringsToTranslate to a list because I need to send them all at once
            //      to the translation engine. We can only get benefit of IEnumberable in one direction
            List<string> stringList = stringsToTranslate.ToList();

            if (sourceLanguage == null) {
                // detect language from the first string in the list to translate
                sourceLanguage = this.DetectLanguage(stringsToTranslate.First());
            }

            Guid translationGuid = Guid.NewGuid();

            IAsyncTranslationPayload asyncObj = new AsyncTranslationPayload(Guid.NewGuid(), stringList, sourceLanguage, destLanguage, this);
            BingTranslatorUserState userState = new BingTranslatorUserState {
                Id = asyncObj.Id,
                SourceLanguage = sourceLanguage as BaseLanguage,
                DestLanguage = destLanguage as BaseLanguage
            };

            // TODO: Do we want this to be done async? if so update this code
            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                client.TranslateArrayAsync(
                    this.ApiKey,
                    stringList.ToArray(),
                    sourceLanguage.Language,
                    destLanguage.Language,
                    this.TranslateOptions,
                    userState);
                client.TranslateArrayCompleted += HandleTranslationComplete;
            }

            return asyncObj;
        }
Ejemplo n.º 13
0
        public ITranslation Translate(string stringToTranslate, ILanguage destLanguage, ILanguage sourceLanguage = null)
        {
            if (string.IsNullOrEmpty(stringToTranslate)) { throw new ArgumentNullException("stringToTranslate"); }

            if (sourceLanguage == null) {
                sourceLanguage = this.DetectLanguage(stringToTranslate);
            }

            ITranslation translation = BingLocanTranslator.GetTranslationFromCache(stringToTranslate, sourceLanguage, destLanguage);
            if (translation == null) {
                string translationResult = null;
                using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                    translationResult = client.Translate(
                        this.ApiKey,
                        stringToTranslate,
                        sourceLanguage.Language,
                        destLanguage.Language,
                        this.TranslateOptions.ContentType,
                        this.TranslateOptions.Category);
                }

                translation = new Translation(sourceLanguage, destLanguage, stringToTranslate, translationResult);
            }

            return translation;
        }
Ejemplo n.º 14
0
        public IEnumerable<ILanguage> GetSupportedLanguages()
        {
            if (this.supportedLanguages == null) {
                lock (lockSupportedLanguages) {
                    if (this.supportedLanguages == null) {
                        // Get languages
                        using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                            IList<ILanguage> langList = new List<ILanguage>();

                            string[] languages = client.GetLanguagesForTranslate(this.ApiKey);
                            languages.ToList().ForEach(lang => {
                                langList.Add(
                                    new BaseLanguage(lang));
                            });

                            this.supportedLanguages = langList;
                        }
                    }
                }
            }

            return this.supportedLanguages;
        }