コード例 #1
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());
            }
        }
コード例 #2
0
        public TranslationLanguageInfo[] GetSourceLanguages(string appID, string appID2)
        {
            if (_names == null)
            {
                try
                {
                    protectWSCall(
                        delegate
                    {
                        var at    = BingTranslationHelper.GetAccessToken(appID, appID2);
                        var names = BingTranslationHelper.GetLanguagesForTranslate(at);

                        var result = new List <TranslationLanguageInfo>();

                        // ReSharper disable LoopCanBeConvertedToQuery
                        foreach (var iso6391Code in names)
                        // ReSharper restore LoopCanBeConvertedToQuery
                        {
                            var ci = IntelligentGetCultureInfo(iso6391Code);

                            if (ci != null)
                            {
                                result.Add(
                                    new TranslationLanguageInfo
                                {
                                    LanguageCode     = iso6391Code,
                                    UserReadableName = ci.DisplayName,
                                });
                            }
                        }

                        _names = result.ToArray();
                    });
                }
                catch (WebException x)
                {
                    LogCentral.Current.LogError(@"Error while getting source languages. Ignoring.", x);
                    return(new TranslationLanguageInfo[] { });
                }
            }

            return(_names);
        }
コード例 #3
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);
            }
        }