Esempio n. 1
0
        private void InitLanguages()
        {
            if (!InitLanguagesFromApi)
            {
                TargetLanguages = Constants.BaiduTranslator.AvailableLanguages.Copy();
                SourceLanguages = new TranslationLanguageCollection {
                    { "", "Auto-detect" }
                };
                SourceLanguages.AddRange(TargetLanguages);
                return;
            }

            lock (_syncRoot)
            {
                if (_sourceLanguages == null || _targetLanguages == null)
                {
                    string[] codes = null;
                    string[] names = null;
                    try
                    {
                        codes = JsonConvert.DeserializeObject <string[]>(_connector.GetLanguagesForTranslate());
                        names = JsonConvert.DeserializeObject <string[]>(_connector.GetLanguageNames("en", codes));
                    }
                    catch
                    {
                        codes = new string[0];
                        names = new string[0];
                    }
                    var languages = new List <TranslationLanguage>(codes.Length);
                    for (int i = 0; i < codes.Length; i++)
                    {
                        languages.Add(new TranslationLanguage(codes[i], names[i]));
                    }
                    languages.Sort((a, b) => string.CompareOrdinal(a.Name, b.Name));
                    _targetLanguages = languages;
                    _sourceLanguages = new List <TranslationLanguage> {
                        new TranslationLanguage("", "Auto-detect")
                    };
                    _sourceLanguages.AddRange(_targetLanguages);
                }
            }
        }
 private void InitLanguages()
 {
     if (!InitLanguagesFromApi)
     {
         TargetLanguages = Constants.BingTranslator.AvailableLanguages.Copy();
         SourceLanguages = new TranslationLanguageCollection { { "", "Auto-detect" } };
         SourceLanguages.AddRange(TargetLanguages);
         return;
     }
     
     lock (_syncRoot)
     {
         if (_sourceLanguages == null || _targetLanguages == null)
         {
             string[] codes = null;
             string[] names = null;
             try
             {
                 codes = JsonConvert.DeserializeObject<string[]>(_connector.GetLanguagesForTranslate());
                 names = JsonConvert.DeserializeObject<string[]>(_connector.GetLanguageNames("en", codes));
             }
             catch
             {
                 codes = new string[0];
                 names = new string[0];
             }
             var languages = new List<TranslationLanguage>(codes.Length);
             for (int i = 0; i < codes.Length; i++)
             {
                 languages.Add(new TranslationLanguage(codes[i], names[i]));
             }
             languages.Sort((a, b) => string.CompareOrdinal(a.Name, b.Name));
             _targetLanguages = languages;
             _sourceLanguages = new List<TranslationLanguage> { new TranslationLanguage("", "Auto-detect") };
             _sourceLanguages.AddRange(_targetLanguages);
         }
     }
 }