33using System . Globalization ;
44using System . IO ;
55using System . Linq ;
6- using System . Reflection ;
76using System . Threading ;
87using System . Threading . Tasks ;
98using System . Windows ;
@@ -35,13 +34,6 @@ public class Internationalization
3534 public Internationalization ( Settings settings )
3635 {
3736 _settings = settings ;
38- AddFlowLauncherLanguageDirectory ( ) ;
39- }
40-
41- private void AddFlowLauncherLanguageDirectory ( )
42- {
43- var directory = Path . Combine ( Constant . ProgramDirectory , Folder ) ;
44- _languageDirectories . Add ( directory ) ;
4537 }
4638
4739 public static void InitSystemLanguageCode ( )
@@ -72,35 +64,6 @@ public static void InitSystemLanguageCode()
7264 SystemLanguageCode = DefaultLanguageCode ;
7365 }
7466
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-
10467 /// <summary>
10568 /// Initialize language. Will change app language and plugin language based on settings.
10669 /// </summary>
@@ -116,11 +79,73 @@ public async Task InitializeLanguageAsync()
11679 // Get language by language code and change language
11780 var language = GetLanguageByLanguageCode ( languageCode ) ;
11881
82+ // Add Flow Launcher language directory
83+ AddFlowLauncherLanguageDirectory ( ) ;
84+
11985 // Add plugin language directories first so that we can load language files from plugins
12086 AddPluginLanguageDirectories ( ) ;
12187
88+ // Load default language resources
89+ LoadDefaultLanguage ( ) ;
90+
12291 // 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 ( ) ;
124149 }
125150
126151 /// <summary>
@@ -152,7 +177,7 @@ public void ChangeLanguage(string languageCode)
152177 private static Language GetLanguageByLanguageCode ( string languageCode )
153178 {
154179 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 ) ) ;
156181 if ( language == null )
157182 {
158183 API . LogError ( ClassName , $ "Language code can't be found <{ languageCode } >") ;
@@ -164,7 +189,7 @@ private static Language GetLanguageByLanguageCode(string languageCode)
164189 }
165190 }
166191
167- private async Task ChangeLanguageAsync ( Language language )
192+ private async Task ChangeLanguageAsync ( Language language , bool updateMetadata = true )
168193 {
169194 // Remove old language files and load language
170195 RemoveOldLanguageFiles ( ) ;
@@ -176,8 +201,11 @@ private async Task ChangeLanguageAsync(Language language)
176201 // Change culture info
177202 ChangeCultureInfo ( language . LanguageCode ) ;
178203
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+ }
181209 }
182210
183211 public static void ChangeCultureInfo ( string languageCode )
@@ -212,7 +240,7 @@ public bool PromptShouldUsePinyin(string languageCodeToSet)
212240
213241 // No other languages should show the following text so just make it hard-coded
214242 // "Do you want to search with pinyin?"
215- string text = languageToSet == AvailableLanguages . Chinese ? "是否启用拼音搜索?" : "是否啓用拼音搜索?" ;
243+ string text = languageToSet == AvailableLanguages . Chinese ? "是否启用拼音搜索?" : "是否啓用拼音搜索?" ;
216244
217245 if ( API . ShowMsgBox ( text , string . Empty , MessageBoxButton . YesNo ) == MessageBoxResult . No )
218246 return false ;
@@ -276,7 +304,7 @@ public static string GetTranslation(string key)
276304 }
277305 }
278306
279- private void UpdatePluginMetadataTranslations ( )
307+ public static void UpdatePluginMetadataTranslations ( )
280308 {
281309 // Update plugin metadata name & description
282310 foreach ( var p in PluginManager . GetTranslationPlugins ( ) )
0 commit comments