small bug fixes
This commit is contained in:
@@ -0,0 +1,269 @@
|
||||
using cfs_tracker.Helpers;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Resources;
|
||||
|
||||
namespace cfs_tracker.Services;
|
||||
|
||||
public sealed class LocalizationService : INotifyPropertyChanged
|
||||
{
|
||||
public const string SystemLanguageCode = "system";
|
||||
public const string GermanLanguageCode = "de";
|
||||
public const string EnglishLanguageCode = "en";
|
||||
|
||||
private const string LanguagePreferenceKey = "app_language";
|
||||
|
||||
private static readonly ResourceManager ResourceManager =
|
||||
new("cfs_tracker.Resources.Localization.AppStrings", typeof(LocalizationService).Assembly);
|
||||
|
||||
private static readonly CultureInfo GermanCulture = new("de-DE");
|
||||
private static readonly CultureInfo EnglishCulture = new("en-GB");
|
||||
|
||||
private readonly CultureInfo _systemCulture;
|
||||
|
||||
private string _selectedLanguageCode = SystemLanguageCode;
|
||||
private CultureInfo _effectiveCulture;
|
||||
|
||||
public static LocalizationService? Current { get; private set; }
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
public event EventHandler? LanguageChanged;
|
||||
|
||||
public LocalizationService()
|
||||
{
|
||||
Current ??= this;
|
||||
_systemCulture = CaptureSystemCulture();
|
||||
_effectiveCulture = ResolveEffectiveCulture(SystemLanguageCode);
|
||||
}
|
||||
|
||||
public string SelectedLanguageCode => _selectedLanguageCode;
|
||||
|
||||
public CultureInfo EffectiveCulture => _effectiveCulture;
|
||||
|
||||
public string EffectiveLanguageCode => NormalizeLanguageCode(_effectiveCulture.TwoLetterISOLanguageName);
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
ApplyLanguage(Preferences.Default.Get(LanguagePreferenceKey, SystemLanguageCode), persistSelection: false);
|
||||
}
|
||||
|
||||
public bool SetSelectedLanguage(string? languageCode)
|
||||
{
|
||||
var normalized = NormalizeLanguageCode(languageCode);
|
||||
return ApplyLanguage(normalized, persistSelection: true);
|
||||
}
|
||||
|
||||
public CultureInfo GetCultureForSelection(string? languageCode)
|
||||
{
|
||||
return ResolveEffectiveCulture(NormalizeLanguageCode(languageCode));
|
||||
}
|
||||
|
||||
public string GetString(string key) => GetString(key, _effectiveCulture);
|
||||
|
||||
public string GetLanguageDisplayName(string? languageCode)
|
||||
{
|
||||
var normalized = NormalizeLanguageCode(languageCode);
|
||||
|
||||
return normalized switch
|
||||
{
|
||||
GermanLanguageCode => GetString("Language_Name_German"),
|
||||
EnglishLanguageCode => GetString("Language_Name_English"),
|
||||
_ => string.Format(
|
||||
_effectiveCulture,
|
||||
GetString("Language_SystemOption"),
|
||||
GetLanguageDisplayNameForCulture(ResolveEffectiveSystemCulture(), _effectiveCulture))
|
||||
};
|
||||
}
|
||||
|
||||
public string GetSystemLanguageDisplayName()
|
||||
{
|
||||
return GetLanguageDisplayNameForCulture(ResolveEffectiveSystemCulture(), _effectiveCulture);
|
||||
}
|
||||
|
||||
public static string Translate(string key)
|
||||
{
|
||||
return Current?.GetString(key) ?? GetString(key, ResolveFallbackCulture());
|
||||
}
|
||||
|
||||
public static string Format(string key, params object[] args)
|
||||
{
|
||||
var culture = GetCurrentCulture();
|
||||
return string.Format(culture, Translate(key), args);
|
||||
}
|
||||
|
||||
public static CultureInfo GetCurrentCulture()
|
||||
{
|
||||
return Current?.EffectiveCulture ?? ResolveFallbackCulture();
|
||||
}
|
||||
|
||||
public static string GetString(string key, CultureInfo culture)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
return string.Empty;
|
||||
|
||||
return ResourceManager.GetString(key, culture)
|
||||
?? ResourceManager.GetString(key, GermanCulture)
|
||||
?? key;
|
||||
}
|
||||
|
||||
public static string NormalizeLanguageCode(string? languageCode)
|
||||
{
|
||||
var normalized = (languageCode ?? string.Empty).Trim().ToLowerInvariant();
|
||||
|
||||
return normalized switch
|
||||
{
|
||||
GermanLanguageCode => GermanLanguageCode,
|
||||
EnglishLanguageCode => EnglishLanguageCode,
|
||||
_ => SystemLanguageCode
|
||||
};
|
||||
}
|
||||
|
||||
public static string GetDefaultStatusText(string appearanceKey, CultureInfo? culture = null)
|
||||
{
|
||||
var resourceKey = appearanceKey switch
|
||||
{
|
||||
AppearanceKeys.StatusGreenText => "Status_Default_Green",
|
||||
AppearanceKeys.StatusYellowText => "Status_Default_Yellow",
|
||||
AppearanceKeys.StatusRedText => "Status_Default_Red",
|
||||
AppearanceKeys.StatusGrayText => "Status_Default_Gray",
|
||||
_ => string.Empty
|
||||
};
|
||||
|
||||
if (string.IsNullOrWhiteSpace(resourceKey))
|
||||
return string.Empty;
|
||||
|
||||
return GetString(resourceKey, culture ?? GetCurrentCulture());
|
||||
}
|
||||
|
||||
public static string NormalizeStatusTextForCulture(string appearanceKey, string? currentValue, CultureInfo targetCulture)
|
||||
{
|
||||
if (IsKnownDefaultStatusText(appearanceKey, currentValue))
|
||||
return GetDefaultStatusText(appearanceKey, targetCulture);
|
||||
|
||||
return (currentValue ?? string.Empty).Trim();
|
||||
}
|
||||
|
||||
public static bool IsKnownDefaultStatusText(string appearanceKey, string? value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
return true;
|
||||
|
||||
var trimmed = value.Trim();
|
||||
foreach (var culture in GetSupportedCultures())
|
||||
{
|
||||
if (string.Equals(trimmed, GetDefaultStatusText(appearanceKey, culture), StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsKnownTranslation(string key, string? value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
return false;
|
||||
|
||||
var trimmed = value.Trim();
|
||||
foreach (var culture in GetSupportedCultures())
|
||||
{
|
||||
if (string.Equals(trimmed, GetString(key, culture), StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsNoActivityLabel(string? value) => IsKnownTranslation("Activity_None", value);
|
||||
|
||||
private bool ApplyLanguage(string? languageCode, bool persistSelection)
|
||||
{
|
||||
var normalized = NormalizeLanguageCode(languageCode);
|
||||
var nextCulture = ResolveEffectiveCulture(normalized);
|
||||
|
||||
if (persistSelection)
|
||||
Preferences.Default.Set(LanguagePreferenceKey, normalized);
|
||||
|
||||
var selectionChanged = !string.Equals(_selectedLanguageCode, normalized, StringComparison.OrdinalIgnoreCase);
|
||||
var cultureChanged = !string.Equals(_effectiveCulture.Name, nextCulture.Name, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
_selectedLanguageCode = normalized;
|
||||
_effectiveCulture = nextCulture;
|
||||
ApplyCulture(nextCulture);
|
||||
|
||||
if (selectionChanged)
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedLanguageCode)));
|
||||
|
||||
if (selectionChanged || cultureChanged)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EffectiveCulture)));
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EffectiveLanguageCode)));
|
||||
LanguageChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
return cultureChanged;
|
||||
}
|
||||
|
||||
private CultureInfo ResolveEffectiveCulture(string normalizedLanguageCode)
|
||||
{
|
||||
return normalizedLanguageCode switch
|
||||
{
|
||||
GermanLanguageCode => GermanCulture,
|
||||
EnglishLanguageCode => EnglishCulture,
|
||||
_ => ResolveEffectiveSystemCulture()
|
||||
};
|
||||
}
|
||||
|
||||
private CultureInfo ResolveEffectiveSystemCulture()
|
||||
{
|
||||
return _systemCulture.TwoLetterISOLanguageName.ToLowerInvariant() switch
|
||||
{
|
||||
GermanLanguageCode => _systemCulture,
|
||||
EnglishLanguageCode => _systemCulture,
|
||||
_ => EnglishCulture
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetLanguageDisplayNameForCulture(CultureInfo culture, CultureInfo uiCulture)
|
||||
{
|
||||
return culture.TwoLetterISOLanguageName.ToLowerInvariant() switch
|
||||
{
|
||||
GermanLanguageCode => GetString("Language_Name_German", uiCulture),
|
||||
_ => GetString("Language_Name_English", uiCulture)
|
||||
};
|
||||
}
|
||||
|
||||
private static void ApplyCulture(CultureInfo culture)
|
||||
{
|
||||
CultureInfo.CurrentCulture = culture;
|
||||
CultureInfo.CurrentUICulture = culture;
|
||||
CultureInfo.DefaultThreadCurrentCulture = culture;
|
||||
CultureInfo.DefaultThreadCurrentUICulture = culture;
|
||||
}
|
||||
|
||||
private static CultureInfo CaptureSystemCulture()
|
||||
{
|
||||
var current = CultureInfo.CurrentUICulture;
|
||||
|
||||
return string.IsNullOrWhiteSpace(current.Name)
|
||||
? GermanCulture
|
||||
: current;
|
||||
}
|
||||
|
||||
private static CultureInfo ResolveFallbackCulture()
|
||||
{
|
||||
var current = CultureInfo.CurrentUICulture;
|
||||
|
||||
return current.TwoLetterISOLanguageName.ToLowerInvariant() switch
|
||||
{
|
||||
GermanLanguageCode => current,
|
||||
EnglishLanguageCode => current,
|
||||
_ => EnglishCulture
|
||||
};
|
||||
}
|
||||
|
||||
private static IEnumerable<CultureInfo> GetSupportedCultures()
|
||||
{
|
||||
yield return GermanCulture;
|
||||
yield return EnglishCulture;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user