3
3
using System . Globalization ;
4
4
using System . IO ;
5
5
using System . Linq ;
6
- using System . Reflection ;
7
6
using System . Threading ;
8
7
using System . Threading . Tasks ;
9
8
using System . Windows ;
@@ -35,13 +34,6 @@ public class Internationalization
35
34
public Internationalization ( Settings settings )
36
35
{
37
36
_settings = settings ;
38
- AddFlowLauncherLanguageDirectory ( ) ;
39
- }
40
-
41
- private void AddFlowLauncherLanguageDirectory ( )
42
- {
43
- var directory = Path . Combine ( Constant . ProgramDirectory , Folder ) ;
44
- _languageDirectories . Add ( directory ) ;
45
37
}
46
38
47
39
public static void InitSystemLanguageCode ( )
@@ -72,35 +64,6 @@ public static void InitSystemLanguageCode()
72
64
SystemLanguageCode = DefaultLanguageCode ;
73
65
}
74
66
75
- private void AddPluginLanguageDirectories ( )
76
- {
77
- foreach ( var plugin in PluginManager . GetTranslationPlugins ( ) )
78
- {
79
- var location = Assembly . GetAssembly ( plugin . Plugin . GetType ( ) ) . Location ;
80
- var dir = Path . GetDirectoryName ( location ) ;
81
- if ( dir != null )
82
- {
83
- var pluginThemeDirectory = Path . Combine ( dir , Folder ) ;
84
- _languageDirectories . Add ( pluginThemeDirectory ) ;
85
- }
86
- else
87
- {
88
- API . LogError ( ClassName , $ "Can't find plugin path <{ location } > for <{ plugin . Metadata . Name } >") ;
89
- }
90
- }
91
-
92
- LoadDefaultLanguage ( ) ;
93
- }
94
-
95
- private void LoadDefaultLanguage ( )
96
- {
97
- // Removes language files loaded before any plugins were loaded.
98
- // Prevents the language Flow started in from overwriting English if the user switches back to English
99
- RemoveOldLanguageFiles ( ) ;
100
- LoadLanguage ( AvailableLanguages . English ) ;
101
- _oldResources . Clear ( ) ;
102
- }
103
-
104
67
/// <summary>
105
68
/// Initialize language. Will change app language and plugin language based on settings.
106
69
/// </summary>
@@ -116,11 +79,73 @@ public async Task InitializeLanguageAsync()
116
79
// Get language by language code and change language
117
80
var language = GetLanguageByLanguageCode ( languageCode ) ;
118
81
82
+ // Add Flow Launcher language directory
83
+ AddFlowLauncherLanguageDirectory ( ) ;
84
+
119
85
// Add plugin language directories first so that we can load language files from plugins
120
86
AddPluginLanguageDirectories ( ) ;
121
87
88
+ // Load default language resources
89
+ LoadDefaultLanguage ( ) ;
90
+
122
91
// Change language
123
- await ChangeLanguageAsync ( language ) ;
92
+ await ChangeLanguageAsync ( language , false ) ;
93
+ }
94
+
95
+ private void AddFlowLauncherLanguageDirectory ( )
96
+ {
97
+ // Check if Flow Launcher language directory exists
98
+ var directory = Path . Combine ( Constant . ProgramDirectory , Folder ) ;
99
+ if ( ! Directory . Exists ( directory ) )
100
+ {
101
+ API . LogError ( ClassName , $ "Flow Launcher language directory can't be found <{ directory } >") ;
102
+ return ;
103
+ }
104
+
105
+ // Check if the language directory contains default language file
106
+ if ( ! File . Exists ( Path . Combine ( directory , DefaultFile ) ) )
107
+ {
108
+ API . LogError ( ClassName , $ "Default language file can't be found in path <{ directory } >") ;
109
+ return ;
110
+ }
111
+
112
+ _languageDirectories . Add ( directory ) ;
113
+ }
114
+
115
+ private void AddPluginLanguageDirectories ( )
116
+ {
117
+ foreach ( var pluginsDir in PluginManager . Directories )
118
+ {
119
+ if ( ! Directory . Exists ( pluginsDir ) ) continue ;
120
+
121
+ // Enumerate all top directories in the plugin directory
122
+ foreach ( var dir in Directory . GetDirectories ( pluginsDir ) )
123
+ {
124
+ // Check if the directory contains a language folder
125
+ var pluginLanguageDir = Path . Combine ( dir , Folder ) ;
126
+ if ( ! Directory . Exists ( pluginLanguageDir ) ) continue ;
127
+
128
+ // Check if the language directory contains default language file
129
+ if ( File . Exists ( Path . Combine ( pluginLanguageDir , DefaultFile ) ) )
130
+ {
131
+ // Add the plugin language directory to the list
132
+ _languageDirectories . Add ( pluginLanguageDir ) ;
133
+ }
134
+ else
135
+ {
136
+ API . LogError ( ClassName , $ "Can't find default language file in path <{ pluginLanguageDir } >") ;
137
+ }
138
+ }
139
+ }
140
+ }
141
+
142
+ private void LoadDefaultLanguage ( )
143
+ {
144
+ // Removes language files loaded before any plugins were loaded.
145
+ // Prevents the language Flow started in from overwriting English if the user switches back to English
146
+ RemoveOldLanguageFiles ( ) ;
147
+ LoadLanguage ( AvailableLanguages . English ) ;
148
+ _oldResources . Clear ( ) ;
124
149
}
125
150
126
151
/// <summary>
@@ -152,7 +177,7 @@ public void ChangeLanguage(string languageCode)
152
177
private static Language GetLanguageByLanguageCode ( string languageCode )
153
178
{
154
179
var lowercase = languageCode . ToLower ( ) ;
155
- var language = AvailableLanguages . GetAvailableLanguages ( ) . FirstOrDefault ( o => o . LanguageCode . ToLower ( ) == lowercase ) ;
180
+ var language = AvailableLanguages . GetAvailableLanguages ( ) . FirstOrDefault ( o => o . LanguageCode . Equals ( lowercase , StringComparison . CurrentCultureIgnoreCase ) ) ;
156
181
if ( language == null )
157
182
{
158
183
API . LogError ( ClassName , $ "Language code can't be found <{ languageCode } >") ;
@@ -164,7 +189,7 @@ private static Language GetLanguageByLanguageCode(string languageCode)
164
189
}
165
190
}
166
191
167
- private async Task ChangeLanguageAsync ( Language language )
192
+ private async Task ChangeLanguageAsync ( Language language , bool updateMetadata = true )
168
193
{
169
194
// Remove old language files and load language
170
195
RemoveOldLanguageFiles ( ) ;
@@ -176,8 +201,11 @@ private async Task ChangeLanguageAsync(Language language)
176
201
// Change culture info
177
202
ChangeCultureInfo ( language . LanguageCode ) ;
178
203
179
- // Raise event for plugins after culture is set
180
- await Task . Run ( UpdatePluginMetadataTranslations ) ;
204
+ if ( updateMetadata )
205
+ {
206
+ // Raise event for plugins after culture is set
207
+ await Task . Run ( UpdatePluginMetadataTranslations ) ;
208
+ }
181
209
}
182
210
183
211
public static void ChangeCultureInfo ( string languageCode )
@@ -212,7 +240,7 @@ public bool PromptShouldUsePinyin(string languageCodeToSet)
212
240
213
241
// No other languages should show the following text so just make it hard-coded
214
242
// "Do you want to search with pinyin?"
215
- string text = languageToSet == AvailableLanguages . Chinese ? "是否启用拼音搜索?" : "是否啓用拼音搜索?" ;
243
+ string text = languageToSet == AvailableLanguages . Chinese ? "是否启用拼音搜索?" : "是否啓用拼音搜索?" ;
216
244
217
245
if ( API . ShowMsgBox ( text , string . Empty , MessageBoxButton . YesNo ) == MessageBoxResult . No )
218
246
return false ;
@@ -276,7 +304,7 @@ public static string GetTranslation(string key)
276
304
}
277
305
}
278
306
279
- private void UpdatePluginMetadataTranslations ( )
307
+ public static void UpdatePluginMetadataTranslations ( )
280
308
{
281
309
// Update plugin metadata name & description
282
310
foreach ( var p in PluginManager . GetTranslationPlugins ( ) )
0 commit comments