コード例 #1
0
        public string[] TranslateArray(
            string appID,
            string appID2,
            string[] texts,
            string sourceLanguageCode,
            string destinationLanguageCode,
            string[] wordsToProtect,
            string[] wordsToRemove)
        {
            var dic =
                new List <MyTuple <string, string> >
            {
                new MyTuple <string, string>(@"source", sourceLanguageCode),
                new MyTuple <string, string>(@"target", destinationLanguageCode),
            };

            // ReSharper disable LoopCanBeConvertedToQuery
            foreach (var text in texts)
            // ReSharper restore LoopCanBeConvertedToQuery
            {
                dic.Add(
                    new MyTuple <string, string>(
                        @"q",
                        TranslationHelper.ProtectWords(
                            TranslationHelper.RemoveWords(text, wordsToRemove), wordsToProtect)));
            }

            var json =
                makeRestCallPost(
                    appID,
                    @"https://www.googleapis.com/language/translate/v2",
                    dic);

            // --

            var result = new List <string>();

            var nodes = json.SelectNodes(@"/root/data/translations/translatedText");

            if (nodes != null)
            {
// ReSharper disable LoopCanBeConvertedToQuery
                foreach (XmlNode xmlNode in nodes)
// ReSharper restore LoopCanBeConvertedToQuery
                {
                    var translatedText = xmlNode.InnerText;

                    result.Add(
                        TranslationHelper.UnprotectWords(
                            translatedText,
                            wordsToProtect));
                }
            }

            return(result.ToArray());
        }
コード例 #2
0
        public string[] TranslateArray(
            string appID,
            string appID2,
            string[] texts,
            string sourceLanguageCode,
            string destinationLanguageCode,
            string[] wordsToProtect,
            string[] wordsToRemove)
        {
            if (texts == null || texts.Length <= 0)
            {
                return(texts);
            }
            else
            {
                var result = new List <string>();

                protectWSCall(
                    delegate
                {
                    var at  = BingTranslationHelper.GetAccessToken(appID, appID2);
                    var raw =
                        BingTranslationHelper.TranslateArray(
                            at,
                            sourceLanguageCode,
                            destinationLanguageCode,
                            TranslationHelper.ProtectWords(
                                TranslationHelper.RemoveWords(
                                    texts,
                                    wordsToRemove),
                                wordsToProtect));

                    foreach (var response in raw)
                    {
                        if (StringExtensionMethods.IsNullOrWhiteSpace(response.Error))
                        {
                            result.Add(TranslationHelper.UnprotectWords(response.Translation, wordsToProtect));
                        }
                        else
                        {
                            result.Add(FileGroup.DefaultTranslationErrorPrefix + @" " + response.Error);
                        }
                    }
                });

                return(result.ToArray());
            }
        }
コード例 #3
0
        public string Translate(
            string appID,
            string appID2,
            string text,
            string sourceLanguageCode,
            string destinationLanguageCode,
            string[] wordsToProtect,
            string[] wordsToRemove)
        {
            var json =
                makeRestCallPost(
                    appID,
                    @"https://www.googleapis.com/language/translate/v2",
                    new List <MyTuple <string, string> >
            {
                new MyTuple <string, string>(@"source", sourceLanguageCode),
                new MyTuple <string, string>(@"target", destinationLanguageCode),
                new MyTuple <string, string>(
                    @"q",
                    TranslationHelper.ProtectWords(
                        TranslationHelper.RemoveWords(text, wordsToRemove), wordsToProtect))
            });

            //var responseStatusNode = json.SelectSingleNode(@"/root/data/responseStatus");
            //var statusCode = ConvertHelper.ToInt32(
            //    responseStatusNode == null
            //        ? string.Empty
            //        : responseStatusNode.InnerText);

            var translatedTextNode = json.SelectSingleNode(@"/root/data/translations/translatedText");

            //var responseDetailsNode = json.SelectSingleNode(@"/root/data/responseDetails");

            return
                (TranslationHelper.UnprotectWords(
                     //statusCode == 200
                     (translatedTextNode == null ? string.Empty : translatedTextNode.InnerText),
                     //: (responseDetailsNode == null ? string.Empty : responseDetailsNode.InnerText),
                     wordsToProtect));
        }
コード例 #4
0
        public string Translate(
            string appID,
            string appID2,
            string text,
            string sourceLanguageCode,
            string destinationLanguageCode,
            string[] wordsToProtect,
            string[] wordsToRemove)
        {
            if (StringExtensionMethods.IsNullOrWhiteSpace(text))
            {
                return(text);
            }
            else
            {
                var result = string.Empty;

                protectWSCall(
                    delegate
                {
                    var at = BingTranslationHelper.GetAccessToken(appID, appID2);
                    result =
                        TranslationHelper.UnprotectWords(
                            BingTranslationHelper.Translate(
                                at,
                                sourceLanguageCode,
                                destinationLanguageCode,
                                TranslationHelper.ProtectWords(
                                    TranslationHelper.RemoveWords(
                                        text,
                                        wordsToRemove),
                                    wordsToProtect)).Translation,
                            wordsToProtect);
                });

                return(result);
            }
        }