From 0ec0d74484bb109516b3ec038f66f30e89560d6f Mon Sep 17 00:00:00 2001 From: Samuel Meenzen Date: Mon, 1 Jul 2024 22:53:37 +0200 Subject: [PATCH] fix: ignore empty translations --- .../MudTranslationsInterceptor.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/MudBlazor.Translations/MudTranslationsInterceptor.cs b/src/MudBlazor.Translations/MudTranslationsInterceptor.cs index a5a190f..8bab790 100644 --- a/src/MudBlazor.Translations/MudTranslationsInterceptor.cs +++ b/src/MudBlazor.Translations/MudTranslationsInterceptor.cs @@ -4,14 +4,14 @@ namespace MudBlazor.Translations; /// -/// Provides community translations for the MudBlazor component library. +/// Provides crowdsourced translations for the MudBlazor component library. /// public class MudTranslationsInterceptor : ILocalizationInterceptor { public LocalizedString Handle(string key, params object[] arguments) { // rewrite upstream key to match overrides - key = key switch + string fixedKey = key switch { "MudDataGrid.=" => "MudDataGrid.Equal", "MudDataGrid.!=" => "MudDataGrid.NotEqual", @@ -22,13 +22,28 @@ public LocalizedString Handle(string key, params object[] arguments) _ => key }; - string? translation = LanguageResource.ResourceManager.GetString(key, CultureInfo.CurrentCulture); + bool notFound = false; + string? translation = LanguageResource.ResourceManager.GetString(fixedKey, CultureInfo.CurrentCulture); + + // Weblate likes to create empty stubs for missing translations, so we need to ignore those and + // use english as a fallback. + if (!Equals(CultureInfo.CurrentCulture, CultureInfo.InvariantCulture) && string.IsNullOrWhiteSpace(translation)) + { + translation = LanguageResource.ResourceManager.GetString(fixedKey, CultureInfo.InvariantCulture); + notFound = true; + } if (translation is not null && arguments.Length > 0) { translation = string.Format(translation, arguments); } - return new LocalizedString(key, translation ?? key, translation == null); + if (translation is null) + { + translation = key; + notFound = true; + } + + return new LocalizedString(key, translation, notFound); } }