OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween / Bing.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 // 
10 // This file is part of OpenTween.
11 // 
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 // 
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details. 
21 // 
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 #nullable enable
28
29 using System;
30 using System.Collections.Generic;
31 using System.Net.Http;
32 using System.Text;
33 using System.Threading;
34 using System.Threading.Tasks;
35 using OpenTween.Api;
36 using OpenTween.Connection;
37
38 namespace OpenTween
39 {
40     public class Bing
41     {
42         private static readonly List<string> LanguageTable = new List<string>
43         {
44             "af",
45             "sq",
46             "ar-sa",
47             "ar-iq",
48             "ar-eg",
49             "ar-ly",
50             "ar-dz",
51             "ar-ma",
52             "ar-tn",
53             "ar-om",
54             "ar-ye",
55             "ar-sy",
56             "ar-jo",
57             "ar-lb",
58             "ar-kw",
59             "ar-ae",
60             "ar-bh",
61             "ar-qa",
62             "eu",
63             "bg",
64             "be",
65             "ca",
66             "zh-tw",
67             "zh-cn",
68             "zh-hk",
69             "zh-sg",
70             "hr",
71             "cs",
72             "da",
73             "nl",
74             "nl-be",
75             "en",
76             "en-us",
77             "en-gb",
78             "en-au",
79             "en-ca",
80             "en-nz",
81             "en-ie",
82             "en-za",
83             "en-jm",
84             "en",
85             "en-bz",
86             "en-tt",
87             "et",
88             "fo",
89             "fa",
90             "fi",
91             "fr",
92             "fr-be",
93             "fr-ca",
94             "fr-ch",
95             "fr-lu",
96             "gd",
97             "ga",
98             "de",
99             "de-ch",
100             "de-at",
101             "de-lu",
102             "de-li",
103             "el",
104             "he",
105             "hi",
106             "hu",
107             "is",
108             "id",
109             "it",
110             "it-ch",
111             "ja",
112             "ko",
113             "ko",
114             "lv",
115             "lt",
116             "mk",
117             "ms",
118             "mt",
119             "no",
120             "no",
121             "pl",
122             "pt-br",
123             "pt",
124             "rm",
125             "ro",
126             "ro-mo",
127             "ru",
128             "ru-mo",
129             "sz",
130             "sr",
131             "sr",
132             "sk",
133             "sl",
134             "sb",
135             "es",
136             "es-mx",
137             "es-gt",
138             "es-cr",
139             "es-pa",
140             "es-do",
141             "es-ve",
142             "es-co",
143             "es-pe",
144             "es-ar",
145             "es-ec",
146             "es-cl",
147             "es-uy",
148             "es-py",
149             "es-bo",
150             "es-sv",
151             "es-hn",
152             "es-ni",
153             "es-pr",
154             "sx",
155             "sv",
156             "sv-fi",
157             "th",
158             "ts",
159             "tn",
160             "tr",
161             "uk",
162             "ur",
163             "ve",
164             "vi",
165             "xh",
166             "ji",
167             "zu",
168         };
169
170         private readonly MicrosoftTranslatorApi translatorApi;
171
172         public Bing()
173             : this(null)
174         {
175         }
176
177         public Bing(HttpClient? http)
178             => this.translatorApi = new MicrosoftTranslatorApi(http);
179
180         /// <summary>
181         /// Microsoft Translator API を使用した翻訳を非同期に行います
182         /// </summary>
183         /// <exception cref="HttpRequestException"/>
184         public async Task<string> TranslateAsync(string text, string? langFrom, string langTo)
185             => await this.translatorApi.TranslateAsync(text, langTo, langFrom)
186                 .ConfigureAwait(false);
187
188         public static string GetLanguageEnumFromIndex(int index)
189             => LanguageTable[index];
190
191         public static int GetIndexFromLanguageEnum(string lang)
192             => LanguageTable.IndexOf(lang);
193     }
194 }