Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix(i18n): publish language strings in one resource change (#540)

SeoJCalivea2747d25e735
diff --git a/src/Filee.App/Services/LocalizationService.cs b/src/Filee.App/Services/LocalizationService.cs
index 809f650f..0f6ff2e2 100644
--- a/src/Filee.App/Services/LocalizationService.cs
+++ b/src/Filee.App/Services/LocalizationService.cs
@@ -71,9 +71,11 @@ public sealed class LocalizationService : ILocalizer
CultureInfo.CurrentUICulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
- // Every key is published (English fallback for missing translations) so DynamicResource never breaks.
- foreach (var key in _english.Keys.Concat(_current.Keys).Distinct())
- _resources[key] = this[key];
+ // Every key is published (English fallback for missing translations) so DynamicResource never breaks. All in
+ // one SetItems: setting the keys one by one raises a resource change per key, and each reaches every control of
+ // every window still alive.
+ _resources.SetItems(_english.Keys.Concat(_current.Keys).Distinct()
+ .Select(key => new KeyValuePair<object, object?>(key, this[key])));
_initialized = true;
LanguageChanged?.Invoke(this, EventArgs.Empty);
diff --git a/tests/Filee.App.Tests/LocalizationResourceTests.cs b/tests/Filee.App.Tests/LocalizationResourceTests.cs
index 1b73b92d..34e44fb9 100644
--- a/tests/Filee.App.Tests/LocalizationResourceTests.cs
+++ b/tests/Filee.App.Tests/LocalizationResourceTests.cs
@@ -130,4 +130,62 @@ public class LocalizationResourceTests
Assert.Equal("Aus", toggle.OffContent);
window.Close();
}
+
+ /// <summary>
+ /// A language switch publishes its strings in one resource change: a change per key reached every control of every
+ /// window still alive, and made the later App tests minutes slower (#540).
+ /// </summary>
+ [AvaloniaFact]
+ public void A_language_switch_publishes_its_strings_in_one_resource_change()
+ {
+ TestServices.EnsureInitialized("en");
+ var app = Application.Current!;
+ var changes = 0;
+ void Count(object? sender, ResourcesChangedEventArgs e) => changes++;
+ app.ResourcesChanged += Count;
+ try
+ {
+ AppHost.Get<LocalizationService>().SetLanguage("ko");
+ Assert.Equal(1, changes);
+ Assert.True(app.TryGetResource("home.title", null, out var title));
+ Assert.Equal("홈", title);
+ }
+ finally
+ {
+ app.ResourcesChanged -= Count;
+ TestServices.EnsureInitialized("en");
+ }
+ }
+
+ /// <summary>A switch before the dictionary is attached still takes effect, and attaching later shows the last language.</summary>
+ [AvaloniaFact]
+ public void A_language_set_before_attaching_shows_once_attached()
+ {
+ TestServices.EnsureInitialized("en");
+ var app = Application.Current!;
+ var merged = app.Resources.MergedDictionaries;
+ var count = merged.Count;
+ var localization = new LocalizationService();
+ var changed = 0;
+ localization.LanguageChanged += (_, _) => changed++;
+ try
+ {
+ localization.SetLanguage("ko");
+ Assert.Equal("ko", localization.Language);
+ Assert.Equal("ko", System.Globalization.CultureInfo.CurrentUICulture.Name);
+ Assert.Equal("홈", localization["home.title"]);
+ Assert.Equal(1, changed);
+
+ localization.Attach(app);
+ Assert.Equal(count + 1, merged.Count);
+ Assert.True(app.TryGetResource("home.title", null, out var title));
+ Assert.Equal("홈", title);
+ }
+ finally
+ {
+ while (merged.Count > count)
+ merged.RemoveAt(merged.Count - 1);
+ TestServices.EnsureInitialized("en");
+ }
+ }
}