33using System . Drawing ;
44using System . IO ;
55using System . Linq ;
6+ using System . Threading ;
67using System . Windows . Forms ;
78
89using GoogleTranslateServiceLibrary ;
@@ -47,6 +48,11 @@ public Form1()
4748
4849 private void BtnTranslate_Click ( object sender , EventArgs e )
4950 {
51+ if ( String . IsNullOrWhiteSpace ( textBoxFrom . Text ) )
52+ {
53+ return ;
54+ }
55+
5056 toolStripStatusLabel1 . Text = String . Empty ;
5157
5258 // Step: validation
@@ -57,17 +63,32 @@ private void BtnTranslate_Click(object sender, EventArgs e)
5763 return ;
5864 }
5965
60- if ( String . IsNullOrWhiteSpace ( textBoxFrom . Text )
61- || comboBoxFrom . SelectedValue == null
62- || comboBoxTo . SelectedValue == null )
66+ SupportedLanguage ? selectedLangFrom = comboBoxFrom . SelectedItem as SupportedLanguage ;
67+ SupportedLanguage ? selectedLangTo = comboBoxTo . SelectedItem as SupportedLanguage ;
68+
69+ if ( selectedLangFrom == null || ! selectedLangFrom . Enabled )
6370 {
71+ toolStripStatusLabel1 . Text = "[ERR] " + MyConstants . SourceLanguageNotSupportedMessage ;
72+
73+ return ;
74+ }
75+
76+ if ( selectedLangTo == null || ! selectedLangTo . Enabled )
77+ {
78+ toolStripStatusLabel1 . Text = "[ERR] " + MyConstants . TargetLanguageNotSupportedMessage ;
79+
6480 return ;
6581 }
6682
67- string langFrom = comboBoxFrom . SelectedValue . ToString ( ) ! ;
68- string langTo = comboBoxTo . SelectedValue . ToString ( ) ! ;
83+ string langFrom = selectedLangFrom . Code ;
84+ string langTo = selectedLangTo . Code ;
6985 string originalText = textBoxFrom . Text ;
7086
87+ if ( langTo == MyConstants . AutoDetectLanguage . Code )
88+ {
89+ langTo = Thread . CurrentThread . CurrentCulture . TwoLetterISOLanguageName ;
90+ }
91+
7192 TranslateService . Translate (
7293 _currentTranslateService ,
7394 originalText ,
@@ -123,6 +144,9 @@ private void TranslateServiceCheckBox_CheckedChanged(object? sender, EventArgs e
123144
124145 private void InitializeLanguageComboBoxes ( ITranslateService currentTranslateService )
125146 {
147+ string ? boxFromPrevSelectedValue = comboBoxFrom . SelectedValue ? . ToString ( ) ;
148+ string ? boxToPrevSelectedValue = comboBoxTo . SelectedValue ? . ToString ( ) ;
149+
126150 string [ ] serviceLangs = currentTranslateService . GetServiceHeader ( ) . SupportedLanguages ;
127151
128152 SupportedLanguage [ ] supportedLanguages = MyConstants . SupportedLanguage
@@ -153,12 +177,17 @@ private void InitializeLanguageComboBoxes(ITranslateService currentTranslateServ
153177 comboBoxTo . SelectedIndexChanged += ComboBox_SelectedIndexChanged ! ;
154178 comboBoxTo . DataSource = new BindingSource ( ) { DataSource = supportedLanguages } ;
155179
156- // Temporary realization
180+ // Восстановление выбранных ранее языков
181+ // (даже если они теперь недоступны)
182+ if ( ! String . IsNullOrWhiteSpace ( boxFromPrevSelectedValue ) )
183+ {
184+ comboBoxFrom . SelectedValue = boxFromPrevSelectedValue ;
185+ }
157186
158- # if DEBUG
159- comboBoxFrom . Text = "English" ;
160- comboBoxTo . Text = "Russian" ;
161- #endif
187+ if ( ! String . IsNullOrWhiteSpace ( boxToPrevSelectedValue ) )
188+ {
189+ comboBoxTo . SelectedValue = boxToPrevSelectedValue ;
190+ }
162191 }
163192
164193 private void InitializeTranslateServices ( )
0 commit comments