From 4be3f5f1f9ff8bd0333033d6ad9c99711da03f96 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Mon, 4 May 2026 20:26:39 +0200 Subject: Add Accept-Language header support for per-request localization --- .../Localization/Core/en-US.json | 34 +-- .../Localization/LocalizationManager.cs | 250 +++++++++++++-------- 2 files changed, 163 insertions(+), 121 deletions(-) (limited to 'Emby.Server.Implementations/Localization') diff --git a/Emby.Server.Implementations/Localization/Core/en-US.json b/Emby.Server.Implementations/Localization/Core/en-US.json index 9b5049c8c7..ff674bd0d0 100644 --- a/Emby.Server.Implementations/Localization/Core/en-US.json +++ b/Emby.Server.Implementations/Localization/Core/en-US.json @@ -1,45 +1,29 @@ { - "Albums": "Albums", "AppDeviceValues": "App: {0}, Device: {1}", - "Application": "Application", "Artists": "Artists", "AuthenticationSucceededWithUserName": "{0} successfully authenticated", "Books": "Books", - "CameraImageUploadedFrom": "A new camera image has been uploaded from {0}", - "Channels": "Channels", "ChapterNameValue": "Chapter {0}", "Collections": "Collections", "Default": "Default", - "DeviceOfflineWithName": "{0} has disconnected", - "DeviceOnlineWithName": "{0} is connected", "External": "External", "FailedLoginAttemptWithUserName": "Failed login attempt from {0}", "Favorites": "Favorites", "Folders": "Folders", "Forced": "Forced", "Genres": "Genres", - "HeaderAlbumArtists": "Album artists", "HeaderContinueWatching": "Continue Watching", - "HeaderFavoriteAlbums": "Favorite Albums", - "HeaderFavoriteArtists": "Favorite Artists", "HeaderFavoriteEpisodes": "Favorite Episodes", "HeaderFavoriteShows": "Favorite Shows", - "HeaderFavoriteSongs": "Favorite Songs", "HeaderLiveTV": "Live TV", "HeaderNextUp": "Next Up", - "HeaderRecordingGroups": "Recording Groups", "HearingImpaired": "Hearing Impaired", "HomeVideos": "Home Videos", "Inherit": "Inherit", - "ItemAddedWithName": "{0} was added to the library", - "ItemRemovedWithName": "{0} was removed from the library", "LabelIpAddressValue": "IP address: {0}", "LabelRunningTimeValue": "Running time: {0}", "Latest": "Latest", - "MessageApplicationUpdated": "Jellyfin Server has been updated", - "MessageApplicationUpdatedTo": "Jellyfin Server has been updated to {0}", - "MessageNamedServerConfigurationUpdatedWithValue": "Server configuration section {0} has been updated", - "MessageServerConfigurationUpdated": "Server configuration has been updated", + "LyricDownloadFailureFromForItem": "Lyrics failed to download from {0} for {1}", "MixedContent": "Mixed content", "Movies": "Movies", "Music": "Music", @@ -66,24 +50,15 @@ "NotificationOptionVideoPlaybackStopped": "Video playback stopped", "Original": "Original", "Photos": "Photos", - "Playlists": "Playlists", - "Plugin": "Plugin", "PluginInstalledWithName": "{0} was installed", "PluginUninstalledWithName": "{0} was uninstalled", "PluginUpdatedWithName": "{0} was updated", - "ProviderValue": "Provider: {0}", "ScheduledTaskFailedWithName": "{0} failed", - "ScheduledTaskStartedWithName": "{0} started", - "ServerNameNeedsToBeRestarted": "{0} needs to be restarted", "Shows": "Shows", - "Songs": "Songs", "StartupEmbyServerIsLoading": "Jellyfin Server is loading. Please try again shortly.", "SubtitleDownloadFailureFromForItem": "Subtitles failed to download from {0} for {1}", - "Sync": "Sync", - "System": "System", "TvShows": "TV Shows", "Undefined": "Undefined", - "User": "User", "UserCreatedWithName": "User {0} has been created", "UserDeletedWithName": "User {0} has been deleted", "UserDownloadingItemWithValues": "{0} is downloading {1}", @@ -91,11 +66,8 @@ "UserOfflineFromDevice": "{0} has disconnected from {1}", "UserOnlineFromDevice": "{0} is online from {1}", "UserPasswordChangedWithName": "Password has been changed for user {0}", - "UserPolicyUpdatedWithName": "User policy has been updated for {0}", "UserStartedPlayingItemWithValues": "{0} is playing {1} on {2}", "UserStoppedPlayingItemWithValues": "{0} has finished playing {1} on {2}", - "ValueHasBeenAddedToLibrary": "{0} has been added to your media library", - "ValueSpecialEpisodeName": "Special - {0}", "VersionNumber": "Version {0}", "TasksMaintenanceCategory": "Maintenance", "TasksLibraryCategory": "Library", @@ -121,8 +93,8 @@ "TaskUpdatePluginsDescription": "Downloads and installs updates for plugins that are configured to update automatically.", "TaskCleanTranscode": "Clean Transcode Directory", "TaskCleanTranscodeDescription": "Deletes transcode files more than one day old.", - "TaskRefreshChannels": "Refresh Channels", - "TaskRefreshChannelsDescription": "Refreshes internet channel information.", + "TasksRefreshChannels": "Refresh Channels", + "TasksRefreshChannelsDescription": "Refreshes internet channel information.", "TaskDownloadMissingLyrics": "Download missing lyrics", "TaskDownloadMissingLyricsDescription": "Downloads lyrics for songs", "TaskDownloadMissingSubtitles": "Download missing subtitles", diff --git a/Emby.Server.Implementations/Localization/LocalizationManager.cs b/Emby.Server.Implementations/Localization/LocalizationManager.cs index d8797e612b..2a7a3388aa 100644 --- a/Emby.Server.Implementations/Localization/LocalizationManager.cs +++ b/Emby.Server.Implementations/Localization/LocalizationManager.cs @@ -3,10 +3,12 @@ using System.Collections.Concurrent; using System.Collections.Frozen; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Text.Json; +using System.Threading; using System.Threading.Tasks; using Jellyfin.Extensions; using Jellyfin.Extensions.Json; @@ -26,21 +28,36 @@ namespace Emby.Server.Implementations.Localization private const string RatingsPath = "Emby.Server.Implementations.Localization.Ratings."; private const string CulturesPath = "Emby.Server.Implementations.Localization.iso6392.txt"; private const string CountriesPath = "Emby.Server.Implementations.Localization.countries.json"; + private const string CoreResourcePrefix = "Emby.Server.Implementations.Localization.Core."; private static readonly Assembly _assembly = typeof(LocalizationManager).Assembly; private static readonly string[] _unratedValues = ["n/a", "unrated", "not rated", "nr"]; + /// + /// Gets the mapping from BCP-47 hyphenated culture codes to Jellyfin's underscore-based codes. + /// + public static readonly FrozenDictionary Bcp47ToJellyfinMap = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["es-419"] = "es_419", + ["es-DO"] = "es_DO", + ["ur-PK"] = "ur_PK" + }.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase); + private readonly IServerConfigurationManager _configurationManager; private readonly ILogger _logger; private readonly Dictionary> _allParentalRatings = new(StringComparer.OrdinalIgnoreCase); - private readonly ConcurrentDictionary> _dictionaries = new(StringComparer.OrdinalIgnoreCase); + private static readonly AsyncLocal?> _requestCultureFallback = new(); + + private readonly ConcurrentDictionary> _cultureOnlyDictionaries = new(StringComparer.OrdinalIgnoreCase); private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; private readonly ConcurrentDictionary _cultureCache = new(StringComparer.OrdinalIgnoreCase); private List _cultures = []; + private static readonly IReadOnlyList _localizationOptions = BuildLocalizationOptions(); + private FrozenDictionary _iso6392BtoT = null!; /// @@ -54,6 +71,41 @@ namespace Emby.Server.Implementations.Localization { _configurationManager = configurationManager; _logger = logger; + + _configurationManager.ConfigurationUpdated += OnConfigurationUpdated; + } + + /// + /// Gets or sets the per-request culture fallback chain resolved from Accept-Language. + /// Each entry is a Jellyfin culture code (e.g. "de", "nl", "en-US") in priority order. + /// + public static IReadOnlyList? RequestCultureFallback + { + get => _requestCultureFallback.Value; + set => _requestCultureFallback.Value = value; + } + + /// + /// Checks whether a translation resource file exists for the given culture code. + /// + /// The culture code to check (e.g. "de", "pt-BR", "es_419"). + /// true if an embedded translation resource exists for the culture. + public static bool HasTranslation(string culture) + { + var resourceName = CoreResourcePrefix + GetResourceFilename(culture); + return _assembly.GetManifestResourceInfo(resourceName) is not null; + } + + private static void OnConfigurationUpdated(object? sender, EventArgs e) + { + if (sender is IServerConfigurationManager configManager) + { + var uiCulture = configManager.Configuration.UICulture; + if (!string.IsNullOrEmpty(uiCulture)) + { + CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(uiCulture); + } + } } /// @@ -419,6 +471,27 @@ namespace Emby.Server.Implementations.Localization /// public string GetLocalizedString(string phrase) + { + var fallback = _requestCultureFallback.Value; + if (fallback is not null) + { + foreach (var culture in fallback) + { + var dict = GetLocalizationDictionary(culture); + if (dict.TryGetValue(phrase, out var value)) + { + return value; + } + } + + return phrase; + } + + return GetLocalizedString(phrase, CultureInfo.CurrentUICulture.Name); + } + + /// + public string GetServerLocalizedString(string phrase) { return GetLocalizedString(phrase, _configurationManager.Configuration.UICulture); } @@ -436,6 +509,12 @@ namespace Emby.Server.Implementations.Localization culture = DefaultCulture; } + // Normalize BCP-47 hyphenated codes to Jellyfin's underscore-based codes + if (Bcp47ToJellyfinMap.TryGetValue(culture, out var mapped)) + { + culture = mapped; + } + var dictionary = GetLocalizationDictionary(culture); if (dictionary.TryGetValue(phrase, out var value)) @@ -443,6 +522,15 @@ namespace Emby.Server.Implementations.Localization return value; } + if (!string.Equals(culture, DefaultCulture, StringComparison.OrdinalIgnoreCase)) + { + var fallback = GetLocalizationDictionary(DefaultCulture); + if (fallback.TryGetValue(phrase, out var fallbackValue)) + { + return fallbackValue; + } + } + return phrase; } @@ -450,26 +538,17 @@ namespace Emby.Server.Implementations.Localization { ArgumentException.ThrowIfNullOrEmpty(culture); - const string Prefix = "Core"; - - return _dictionaries.GetOrAdd( + return _cultureOnlyDictionaries.GetOrAdd( culture, - static (key, localizationManager) => localizationManager.GetDictionary(Prefix, key, DefaultCulture + ".json").GetAwaiter().GetResult(), - this); - } - - private async Task> GetDictionary(string prefix, string culture, string baseFilename) - { - ArgumentException.ThrowIfNullOrEmpty(culture); - - var dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); - - var namespaceName = GetType().Namespace + "." + prefix; - - await CopyInto(dictionary, namespaceName + "." + baseFilename).ConfigureAwait(false); - await CopyInto(dictionary, namespaceName + "." + GetResourceFilename(culture)).ConfigureAwait(false); + static (key, localizationManager) => + { + var dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); + var namespaceName = localizationManager.GetType().Namespace + ".Core"; + localizationManager.CopyInto(dictionary, namespaceName + "." + GetResourceFilename(key)).GetAwaiter().GetResult(); - return dictionary; + return dictionary; + }, + this); } private async Task CopyInto(IDictionary dictionary, string resourcePath) @@ -508,77 +587,68 @@ namespace Emby.Server.Implementations.Localization /// public IEnumerable GetLocalizationOptions() { - yield return new LocalizationOption("Afrikaans", "af"); - yield return new LocalizationOption("العربية", "ar"); - yield return new LocalizationOption("Беларуская", "be"); - yield return new LocalizationOption("Български", "bg-BG"); - yield return new LocalizationOption("বাংলা (বাংলাদেশ)", "bn"); - yield return new LocalizationOption("Català", "ca"); - yield return new LocalizationOption("Čeština", "cs"); - yield return new LocalizationOption("Cymraeg", "cy"); - yield return new LocalizationOption("Dansk", "da"); - yield return new LocalizationOption("Deutsch", "de"); - yield return new LocalizationOption("English (United Kingdom)", "en-GB"); - yield return new LocalizationOption("English", "en-US"); - yield return new LocalizationOption("Ελληνικά", "el"); - yield return new LocalizationOption("Esperanto", "eo"); - yield return new LocalizationOption("Español", "es"); - yield return new LocalizationOption("Español americano", "es_419"); - yield return new LocalizationOption("Español (Argentina)", "es-AR"); - yield return new LocalizationOption("Español (Dominicana)", "es_DO"); - yield return new LocalizationOption("Español (México)", "es-MX"); - yield return new LocalizationOption("Eesti", "et"); - yield return new LocalizationOption("Basque", "eu"); - yield return new LocalizationOption("فارسی", "fa"); - yield return new LocalizationOption("Suomi", "fi"); - yield return new LocalizationOption("Filipino", "fil"); - yield return new LocalizationOption("Français", "fr"); - yield return new LocalizationOption("Français (Canada)", "fr-CA"); - yield return new LocalizationOption("Galego", "gl"); - yield return new LocalizationOption("Schwiizerdütsch", "gsw"); - yield return new LocalizationOption("עִבְרִית", "he"); - yield return new LocalizationOption("हिन्दी", "hi"); - yield return new LocalizationOption("Hrvatski", "hr"); - yield return new LocalizationOption("Magyar", "hu"); - yield return new LocalizationOption("Bahasa Indonesia", "id"); - yield return new LocalizationOption("Íslenska", "is"); - yield return new LocalizationOption("Italiano", "it"); - yield return new LocalizationOption("日本語", "ja"); - yield return new LocalizationOption("Qazaqşa", "kk"); - yield return new LocalizationOption("한국어", "ko"); - yield return new LocalizationOption("Lietuvių", "lt"); - yield return new LocalizationOption("Latviešu", "lv"); - yield return new LocalizationOption("Македонски", "mk"); - yield return new LocalizationOption("മലയാളം", "ml"); - yield return new LocalizationOption("मराठी", "mr"); - yield return new LocalizationOption("Bahasa Melayu", "ms"); - yield return new LocalizationOption("Norsk bokmål", "nb"); - yield return new LocalizationOption("नेपाली", "ne"); - yield return new LocalizationOption("Nederlands", "nl"); - yield return new LocalizationOption("Norsk nynorsk", "nn"); - yield return new LocalizationOption("ਪੰਜਾਬੀ", "pa"); - yield return new LocalizationOption("Polski", "pl"); - yield return new LocalizationOption("Pirate", "pr"); - yield return new LocalizationOption("Português", "pt"); - yield return new LocalizationOption("Português (Brasil)", "pt-BR"); - yield return new LocalizationOption("Português (Portugal)", "pt-PT"); - yield return new LocalizationOption("Românește", "ro"); - yield return new LocalizationOption("Русский", "ru"); - yield return new LocalizationOption("Slovenčina", "sk"); - yield return new LocalizationOption("Slovenščina", "sl-SI"); - yield return new LocalizationOption("Shqip", "sq"); - yield return new LocalizationOption("Српски", "sr"); - yield return new LocalizationOption("Svenska", "sv"); - yield return new LocalizationOption("தமிழ்", "ta"); - yield return new LocalizationOption("తెలుగు", "te"); - yield return new LocalizationOption("ภาษาไทย", "th"); - yield return new LocalizationOption("Türkçe", "tr"); - yield return new LocalizationOption("Українська", "uk"); - yield return new LocalizationOption("اُردُو", "ur_PK"); - yield return new LocalizationOption("Tiếng Việt", "vi"); - yield return new LocalizationOption("汉语 (简体字)", "zh-CN"); - yield return new LocalizationOption("漢語 (繁體字)", "zh-TW"); - yield return new LocalizationOption("廣東話 (香港)", "zh-HK"); + return _localizationOptions; + } + + private static IReadOnlyList BuildLocalizationOptions() + { + var options = new List(); + var prefix = CoreResourcePrefix; + + foreach (var resource in _assembly.GetManifestResourceNames()) + { + if (!resource.StartsWith(prefix, StringComparison.Ordinal) + || !resource.EndsWith(".json", StringComparison.Ordinal)) + { + continue; + } + + // Extract culture code from resource name: "...Core.de.json" -> "de", "...Core.pt-BR.json" -> "pt-BR" + var code = resource[prefix.Length..^5]; + + // Skip the base language file — en-US is added explicitly below + if (code.Equals(DefaultCulture, StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + var displayName = GetDisplayName(code); + options.Add(new LocalizationOption(displayName, code)); + } + + // Ensure en-US is always present + options.Add(new LocalizationOption("English", DefaultCulture)); + + options.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase)); + return options; + } + + private static string GetDisplayName(string cultureCode) + { + // Handle Jellyfin-specific codes that aren't valid CultureInfo names + if (Bcp47ToJellyfinMap.Values.Contains(cultureCode)) + { + // Convert underscore to hyphen for CultureInfo lookup + var normalized = cultureCode.Replace('_', '-'); + try + { + return CultureInfo.GetCultureInfo(normalized).NativeName; + } + catch (CultureNotFoundException) + { + return cultureCode; + } + } + + try + { + return CultureInfo.GetCultureInfo(cultureCode).NativeName; + } + catch (CultureNotFoundException) + { + // Custom/novelty codes like "pr" (Pirate) — fall back to code itself + return cultureCode; + } } /// -- cgit v1.2.3 From 5cfb379aa63689435077c8f1ebc10c98f625238c Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Mon, 4 May 2026 21:57:11 +0200 Subject: Use native middleware --- .../HttpServer/WebSocketConnection.cs | 28 ++--- .../HttpServer/WebSocketManager.cs | 4 +- .../Localization/LocalizationManager.cs | 63 ++++------ .../Middleware/AcceptLanguageMiddleware.cs | 137 --------------------- Jellyfin.Server/Startup.cs | 14 ++- .../Localization/LocalizationManagerTests.cs | 86 ++----------- 6 files changed, 54 insertions(+), 278 deletions(-) delete mode 100644 Jellyfin.Server/Middleware/AcceptLanguageMiddleware.cs (limited to 'Emby.Server.Implementations/Localization') diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs index 6dc6d9d289..17070c39ba 100644 --- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs +++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs @@ -1,6 +1,5 @@ using System; using System.Buffers; -using System.Collections.Generic; using System.Globalization; using System.IO.Pipelines; using System.Net; @@ -9,7 +8,6 @@ using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; -using Emby.Server.Implementations.Localization; using Jellyfin.Extensions.Json; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Net.WebSocketMessages; @@ -72,12 +70,6 @@ namespace Emby.Server.Implementations.HttpServer /// public IPAddress? RemoteEndPoint { get; } - /// - /// Gets or initializes the culture fallback chain captured from the - /// Accept-Language header of the upgrade request. - /// - public IReadOnlyList? RequestCultureFallback { get; init; } - /// /// Gets or initializes the UI culture name captured from the upgrade request. /// @@ -98,22 +90,18 @@ namespace Emby.Server.Implementations.HttpServer /// public void ApplyRequestCulture() { - if (RequestCultureFallback is not null) + if (string.IsNullOrEmpty(RequestUICulture)) { - LocalizationManager.RequestCultureFallback = RequestCultureFallback; + return; } - if (!string.IsNullOrEmpty(RequestUICulture)) + try { - try - { - CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(RequestUICulture); - } - catch (CultureNotFoundException) - { - // Jellyfin culture codes (e.g. "es_419") aren't always valid .NET cultures — - // skip setting CurrentUICulture; RequestCultureFallback above carries the chain. - } + CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(RequestUICulture); + } + catch (CultureNotFoundException) + { + // Codes that aren't valid .NET cultures are ignored. } } diff --git a/Emby.Server.Implementations/HttpServer/WebSocketManager.cs b/Emby.Server.Implementations/HttpServer/WebSocketManager.cs index 3b5f6d1d09..dcdfda5472 100644 --- a/Emby.Server.Implementations/HttpServer/WebSocketManager.cs +++ b/Emby.Server.Implementations/HttpServer/WebSocketManager.cs @@ -8,7 +8,6 @@ using System.Globalization; using System.Linq; using System.Net.WebSockets; using System.Threading.Tasks; -using Emby.Server.Implementations.Localization; using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Net; using Microsoft.AspNetCore.Http; @@ -50,7 +49,7 @@ namespace Emby.Server.Implementations.HttpServer WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false); - // Capture the culture context set by AcceptLanguageMiddleware so it can be + // Capture the culture set by RequestLocalizationMiddleware so it can be // restored both when processing incoming messages and when periodic // listeners produce server-initiated payloads on background tasks. var connection = new WebSocketConnection( @@ -59,7 +58,6 @@ namespace Emby.Server.Implementations.HttpServer authorizationInfo, context.GetNormalizedRemoteIP()) { - RequestCultureFallback = LocalizationManager.RequestCultureFallback, RequestUICulture = CultureInfo.CurrentUICulture.Name }; connection.OnReceive = result => diff --git a/Emby.Server.Implementations/Localization/LocalizationManager.cs b/Emby.Server.Implementations/Localization/LocalizationManager.cs index 2a7a3388aa..5c2376ea00 100644 --- a/Emby.Server.Implementations/Localization/LocalizationManager.cs +++ b/Emby.Server.Implementations/Localization/LocalizationManager.cs @@ -8,7 +8,6 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text.Json; -using System.Threading; using System.Threading.Tasks; using Jellyfin.Extensions; using Jellyfin.Extensions.Json; @@ -32,10 +31,9 @@ namespace Emby.Server.Implementations.Localization private static readonly Assembly _assembly = typeof(LocalizationManager).Assembly; private static readonly string[] _unratedValues = ["n/a", "unrated", "not rated", "nr"]; - /// - /// Gets the mapping from BCP-47 hyphenated culture codes to Jellyfin's underscore-based codes. - /// - public static readonly FrozenDictionary Bcp47ToJellyfinMap = new Dictionary(StringComparer.OrdinalIgnoreCase) + // Maps BCP-47 hyphenated culture codes (set by ASP.NET Core's RequestLocalizationMiddleware + // and used as CurrentUICulture.Name) to Jellyfin's underscore-based resource file codes. + private static readonly FrozenDictionary _bcp47ToJellyfinMap = new Dictionary(StringComparer.OrdinalIgnoreCase) { ["es-419"] = "es_419", ["es-DO"] = "es_DO", @@ -47,8 +45,6 @@ namespace Emby.Server.Implementations.Localization private readonly Dictionary> _allParentalRatings = new(StringComparer.OrdinalIgnoreCase); - private static readonly AsyncLocal?> _requestCultureFallback = new(); - private readonly ConcurrentDictionary> _cultureOnlyDictionaries = new(StringComparer.OrdinalIgnoreCase); private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; @@ -76,24 +72,28 @@ namespace Emby.Server.Implementations.Localization } /// - /// Gets or sets the per-request culture fallback chain resolved from Accept-Language. - /// Each entry is a Jellyfin culture code (e.g. "de", "nl", "en-US") in priority order. + /// Gets the supported UI cultures. /// - public static IReadOnlyList? RequestCultureFallback + /// A list of objects covering every embedded translation. + public static IList GetSupportedUICultures() { - get => _requestCultureFallback.Value; - set => _requestCultureFallback.Value = value; - } + var cultures = new List(); + foreach (var option in _localizationOptions) + { + // Resource files use underscores for some variants (e.g. es_419); + // CultureInfo only accepts hyphenated BCP-47 codes. + var code = option.Value.Replace('_', '-'); + try + { + cultures.Add(CultureInfo.GetCultureInfo(code)); + } + catch (CultureNotFoundException) + { + // Skip novelty codes (e.g. "pr" Pirate, "jbo" Lojban) that .NET cannot resolve. + } + } - /// - /// Checks whether a translation resource file exists for the given culture code. - /// - /// The culture code to check (e.g. "de", "pt-BR", "es_419"). - /// true if an embedded translation resource exists for the culture. - public static bool HasTranslation(string culture) - { - var resourceName = CoreResourcePrefix + GetResourceFilename(culture); - return _assembly.GetManifestResourceInfo(resourceName) is not null; + return cultures; } private static void OnConfigurationUpdated(object? sender, EventArgs e) @@ -472,21 +472,6 @@ namespace Emby.Server.Implementations.Localization /// public string GetLocalizedString(string phrase) { - var fallback = _requestCultureFallback.Value; - if (fallback is not null) - { - foreach (var culture in fallback) - { - var dict = GetLocalizationDictionary(culture); - if (dict.TryGetValue(phrase, out var value)) - { - return value; - } - } - - return phrase; - } - return GetLocalizedString(phrase, CultureInfo.CurrentUICulture.Name); } @@ -510,7 +495,7 @@ namespace Emby.Server.Implementations.Localization } // Normalize BCP-47 hyphenated codes to Jellyfin's underscore-based codes - if (Bcp47ToJellyfinMap.TryGetValue(culture, out var mapped)) + if (_bcp47ToJellyfinMap.TryGetValue(culture, out var mapped)) { culture = mapped; } @@ -626,7 +611,7 @@ namespace Emby.Server.Implementations.Localization private static string GetDisplayName(string cultureCode) { // Handle Jellyfin-specific codes that aren't valid CultureInfo names - if (Bcp47ToJellyfinMap.Values.Contains(cultureCode)) + if (_bcp47ToJellyfinMap.Values.Contains(cultureCode)) { // Convert underscore to hyphen for CultureInfo lookup var normalized = cultureCode.Replace('_', '-'); diff --git a/Jellyfin.Server/Middleware/AcceptLanguageMiddleware.cs b/Jellyfin.Server/Middleware/AcceptLanguageMiddleware.cs deleted file mode 100644 index 57390ae005..0000000000 --- a/Jellyfin.Server/Middleware/AcceptLanguageMiddleware.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Emby.Server.Implementations.Localization; -using MediaBrowser.Controller.Configuration; -using Microsoft.AspNetCore.Http; - -namespace Jellyfin.Server.Middleware; - -/// -/// Middleware that resolves the Accept-Language request header -/// to an ordered list of Jellyfin-supported cultures, sets the fallback chain -/// on , and writes -/// the Content-Language response header. -/// -public class AcceptLanguageMiddleware -{ - private readonly RequestDelegate _next; - - /// - /// Initializes a new instance of the class. - /// - /// Next request delegate. - public AcceptLanguageMiddleware(RequestDelegate next) - { - _next = next; - } - - /// - /// Invoke request. - /// - /// Request context. - /// The server configuration manager. - /// Task. - public async Task Invoke(HttpContext context, IServerConfigurationManager configurationManager) - { - var chain = ResolveLanguages(context.Request, configurationManager); - if (chain is not null) - { - LocalizationManager.RequestCultureFallback = chain; - CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(chain[0]); - } - - context.Response.OnStarting( - static state => - { - var (ctx, languages) = ((HttpContext, IReadOnlyList?))state; - if (languages is not null) - { - ctx.Response.Headers.ContentLanguage = string.Join(", ", languages); - } - else - { - var culture = CultureInfo.CurrentUICulture.Name; - if (!string.IsNullOrEmpty(culture)) - { - ctx.Response.Headers.ContentLanguage = culture; - } - } - - return Task.CompletedTask; - }, - (context, chain)); - - try - { - await _next(context).ConfigureAwait(false); - } - finally - { - LocalizationManager.RequestCultureFallback = null; - } - } - - private static IReadOnlyList? ResolveLanguages(HttpRequest request, IServerConfigurationManager configurationManager) - { - var acceptLanguageHeader = request.GetTypedHeaders().AcceptLanguage; - if (acceptLanguageHeader is null || acceptLanguageHeader.Count == 0) - { - return null; - } - - var languages = acceptLanguageHeader - .OrderByDescending(h => h.Quality ?? 1) - .Select(h => h.Value.ToString()); - - var chain = new List(); - var seen = new HashSet(StringComparer.OrdinalIgnoreCase); - - foreach (var lang in languages) - { - TryAddCulture(lang, chain, seen); - } - - // Append server default culture if not already present - var serverCulture = configurationManager.Configuration.UICulture; - if (!string.IsNullOrEmpty(serverCulture)) - { - TryAddCulture(serverCulture, chain, seen); - } - - // Ensure en-US is always the final fallback - TryAddCulture("en-US", chain, seen); - - return chain; - } - - private static void TryAddCulture(string lang, List chain, HashSet seen) - { - // Direct match - if (LocalizationManager.HasTranslation(lang) && seen.Add(lang)) - { - chain.Add(lang); - return; - } - - // BCP-47 to Jellyfin underscore mapping (e.g. es-419 -> es_419) - if (LocalizationManager.Bcp47ToJellyfinMap.TryGetValue(lang, out var mapped) && seen.Add(mapped)) - { - chain.Add(mapped); - return; - } - - // Parent culture fallback (e.g. de-DE -> de) - var dashIndex = lang.IndexOf('-', StringComparison.Ordinal); - if (dashIndex > 0) - { - var parent = lang[..dashIndex]; - if (LocalizationManager.HasTranslation(parent) && seen.Add(parent)) - { - chain.Add(parent); - } - } - } -} diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index ea677cf6d9..f1c8b532aa 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -7,6 +7,7 @@ using System.Net.Http.Headers; using System.Net.Mime; using System.Text; using Emby.Server.Implementations.EntryPoints; +using Emby.Server.Implementations.Localization; using Jellyfin.Api.Middleware; using Jellyfin.Database.Implementations; using Jellyfin.LiveTv.Extensions; @@ -17,13 +18,13 @@ using Jellyfin.Networking.HappyEyeballs; using Jellyfin.Server.Extensions; using Jellyfin.Server.HealthChecks; using Jellyfin.Server.Implementations.Extensions; -using Jellyfin.Server.Middleware; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Extensions; using MediaBrowser.XbmcMetadata; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Localization; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -137,6 +138,15 @@ namespace Jellyfin.Server CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(serverUICulture); + services.Configure(options => + { + var supportedUICultures = LocalizationManager.GetSupportedUICultures(); + options.SupportedCultures = supportedUICultures; + options.SupportedUICultures = supportedUICultures; + options.DefaultRequestCulture = new RequestCulture(serverUICulture); + options.ApplyCurrentCultureToResponseHeaders = true; + }); + services.AddHostedService(); services.AddHostedService(); services.AddHostedService(); @@ -178,7 +188,7 @@ namespace Jellyfin.Server mainApp.UseCors(); - mainApp.UseMiddleware(); + mainApp.UseRequestLocalization(); if (config.RequireHttps && _serverApplicationHost.ListenWithHttps) { diff --git a/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs index 1c4d0d4a8a..3b8fe5ca60 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/Localization/LocalizationManagerTests.cs @@ -367,72 +367,7 @@ namespace Jellyfin.Server.Implementations.Tests.Localization } [Fact] - public void GetLocalizedString_FallbackChain_UsesFirstAvailableCulture() - { - var localizationManager = Setup(new ServerConfiguration - { - UICulture = "en-US" - }); - - // Set fallback chain: de -> fr -> en-US - // "Artists" exists in de as "Interpreten", should use de (first in chain) - LocalizationManager.RequestCultureFallback = new[] { "de", "fr", "en-US" }; - try - { - var translated = localizationManager.GetLocalizedString("Artists"); - Assert.Equal("Interpreten", translated); - } - finally - { - LocalizationManager.RequestCultureFallback = null; - } - } - - [Fact] - public void GetLocalizedString_FallbackChain_SkipsMissingAndUsesNext() - { - var localizationManager = Setup(new ServerConfiguration - { - UICulture = "en-US" - }); - - // "zz" has no translation file so the key won't be found there, - // should fall through to de which has "Artists" as "Interpreten" - LocalizationManager.RequestCultureFallback = new[] { "zz", "de", "en-US" }; - try - { - var translated = localizationManager.GetLocalizedString("Artists"); - Assert.Equal("Interpreten", translated); - } - finally - { - LocalizationManager.RequestCultureFallback = null; - } - } - - [Fact] - public void GetLocalizedString_FallbackChain_ReturnsKeyWhenNoTranslation() - { - var localizationManager = Setup(new ServerConfiguration - { - UICulture = "en-US" - }); - - var key = "CompletelyNonExistentKey"; - LocalizationManager.RequestCultureFallback = new[] { "de", "en-US" }; - try - { - var translated = localizationManager.GetLocalizedString(key); - Assert.Equal(key, translated); - } - finally - { - LocalizationManager.RequestCultureFallback = null; - } - } - - [Fact] - public void GetLocalizedString_NoFallbackChain_UsesCurrentUICulture() + public void GetLocalizedString_UsesCurrentUICulture() { var localizationManager = Setup(new ServerConfiguration { @@ -443,8 +378,6 @@ namespace Jellyfin.Server.Implementations.Tests.Localization try { CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("de"); - LocalizationManager.RequestCultureFallback = null; - var translated = localizationManager.GetLocalizedString("Artists"); Assert.Equal("Interpreten", translated); } @@ -454,16 +387,15 @@ namespace Jellyfin.Server.Implementations.Tests.Localization } } - [Theory] - [InlineData("de", true)] - [InlineData("en-US", true)] - [InlineData("fr", true)] - [InlineData("es_419", true)] - [InlineData("nonexistent", false)] - [InlineData("zz-ZZ", false)] - public void HasTranslation_ReturnsExpected(string culture, bool expected) + [Fact] + public void GetSupportedUICultures_IncludesCommonCultures() { - Assert.Equal(expected, LocalizationManager.HasTranslation(culture)); + var supported = LocalizationManager.GetSupportedUICultures(); + Assert.Contains(supported, c => c.Name.Equals("de", StringComparison.OrdinalIgnoreCase)); + Assert.Contains(supported, c => c.Name.Equals("en-US", StringComparison.OrdinalIgnoreCase)); + Assert.Contains(supported, c => c.Name.Equals("fr", StringComparison.OrdinalIgnoreCase)); + // Underscore variants get normalized to BCP-47 hyphen form for CultureInfo compatibility. + Assert.Contains(supported, c => c.Name.Equals("es-419", StringComparison.OrdinalIgnoreCase)); } private LocalizationManager Setup(ServerConfiguration config) -- cgit v1.2.3 From b8c0017b7466e5b50a8a32476469b8b6d2215b8c Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Wed, 13 May 2026 21:23:53 +0200 Subject: Build BCP47 map reflexively --- .../Localization/LocalizationManager.cs | 48 ++++++++++------------ 1 file changed, 21 insertions(+), 27 deletions(-) (limited to 'Emby.Server.Implementations/Localization') diff --git a/Emby.Server.Implementations/Localization/LocalizationManager.cs b/Emby.Server.Implementations/Localization/LocalizationManager.cs index 5c2376ea00..94aa933c92 100644 --- a/Emby.Server.Implementations/Localization/LocalizationManager.cs +++ b/Emby.Server.Implementations/Localization/LocalizationManager.cs @@ -31,15 +31,6 @@ namespace Emby.Server.Implementations.Localization private static readonly Assembly _assembly = typeof(LocalizationManager).Assembly; private static readonly string[] _unratedValues = ["n/a", "unrated", "not rated", "nr"]; - // Maps BCP-47 hyphenated culture codes (set by ASP.NET Core's RequestLocalizationMiddleware - // and used as CurrentUICulture.Name) to Jellyfin's underscore-based resource file codes. - private static readonly FrozenDictionary _bcp47ToJellyfinMap = new Dictionary(StringComparer.OrdinalIgnoreCase) - { - ["es-419"] = "es_419", - ["es-DO"] = "es_DO", - ["ur-PK"] = "ur_PK" - }.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase); - private readonly IServerConfigurationManager _configurationManager; private readonly ILogger _logger; @@ -52,7 +43,13 @@ namespace Emby.Server.Implementations.Localization private readonly ConcurrentDictionary _cultureCache = new(StringComparer.OrdinalIgnoreCase); private List _cultures = []; - private static readonly IReadOnlyList _localizationOptions = BuildLocalizationOptions(); + private static readonly (IReadOnlyList Options, FrozenDictionary Bcp47ToJellyfinMap) _localizationData = BuildLocalizationData(); + private static readonly IReadOnlyList _localizationOptions = _localizationData.Options; + + // Maps BCP-47 hyphenated culture codes (set by ASP.NET Core's RequestLocalizationMiddleware + // and used as CurrentUICulture.Name) to Jellyfin's underscore-based resource file codes. + // Built reflexively from the resource file scan so both directions stay in sync. + private static readonly FrozenDictionary _bcp47ToJellyfinMap = _localizationData.Bcp47ToJellyfinMap; private FrozenDictionary _iso6392BtoT = null!; @@ -575,9 +572,10 @@ namespace Emby.Server.Implementations.Localization return _localizationOptions; } - private static IReadOnlyList BuildLocalizationOptions() + private static (IReadOnlyList Options, FrozenDictionary Bcp47ToJellyfinMap) BuildLocalizationData() { var options = new List(); + var bcp47Map = new Dictionary(StringComparer.OrdinalIgnoreCase); var prefix = CoreResourcePrefix; foreach (var resource in _assembly.GetManifestResourceNames()) @@ -591,6 +589,12 @@ namespace Emby.Server.Implementations.Localization // Extract culture code from resource name: "...Core.de.json" -> "de", "...Core.pt-BR.json" -> "pt-BR" var code = resource[prefix.Length..^5]; + // Record the BCP-47 → Jellyfin mapping for any resource file using underscores. + if (code.Contains('_', StringComparison.Ordinal)) + { + bcp47Map[code.Replace('_', '-')] = code; + } + // Skip the base language file — en-US is added explicitly below if (code.Equals(DefaultCulture, StringComparison.OrdinalIgnoreCase)) { @@ -605,29 +609,19 @@ namespace Emby.Server.Implementations.Localization options.Add(new LocalizationOption("English", DefaultCulture)); options.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase)); - return options; + return (options, bcp47Map.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase)); } private static string GetDisplayName(string cultureCode) { - // Handle Jellyfin-specific codes that aren't valid CultureInfo names - if (_bcp47ToJellyfinMap.Values.Contains(cultureCode)) - { - // Convert underscore to hyphen for CultureInfo lookup - var normalized = cultureCode.Replace('_', '-'); - try - { - return CultureInfo.GetCultureInfo(normalized).NativeName; - } - catch (CultureNotFoundException) - { - return cultureCode; - } - } + // Resource files use underscores for codes that .NET's CultureInfo doesn't accept directly (e.g. es_419). + var lookup = cultureCode.Contains('_', StringComparison.Ordinal) + ? cultureCode.Replace('_', '-') + : cultureCode; try { - return CultureInfo.GetCultureInfo(cultureCode).NativeName; + return CultureInfo.GetCultureInfo(lookup).NativeName; } catch (CultureNotFoundException) { -- cgit v1.2.3 From 7a5181c3fd3aea8a9913fe07086970c39c9bc1c4 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Thu, 14 May 2026 07:46:43 +0200 Subject: Address review comments --- .../Localization/Core/en-US.json | 4 +- .../Localization/LocalizationManager.cs | 48 ++++++++++++---------- .../Channels/RefreshChannelsScheduledTask.cs | 4 +- 3 files changed, 30 insertions(+), 26 deletions(-) (limited to 'Emby.Server.Implementations/Localization') diff --git a/Emby.Server.Implementations/Localization/Core/en-US.json b/Emby.Server.Implementations/Localization/Core/en-US.json index ff674bd0d0..856941c61a 100644 --- a/Emby.Server.Implementations/Localization/Core/en-US.json +++ b/Emby.Server.Implementations/Localization/Core/en-US.json @@ -93,8 +93,8 @@ "TaskUpdatePluginsDescription": "Downloads and installs updates for plugins that are configured to update automatically.", "TaskCleanTranscode": "Clean Transcode Directory", "TaskCleanTranscodeDescription": "Deletes transcode files more than one day old.", - "TasksRefreshChannels": "Refresh Channels", - "TasksRefreshChannelsDescription": "Refreshes internet channel information.", + "TaskRefreshChannels": "Refresh Channels", + "TaskRefreshChannelsDescription": "Refreshes internet channel information.", "TaskDownloadMissingLyrics": "Download missing lyrics", "TaskDownloadMissingLyricsDescription": "Downloads lyrics for songs", "TaskDownloadMissingSubtitles": "Download missing subtitles", diff --git a/Emby.Server.Implementations/Localization/LocalizationManager.cs b/Emby.Server.Implementations/Localization/LocalizationManager.cs index 94aa933c92..0b0b300d30 100644 --- a/Emby.Server.Implementations/Localization/LocalizationManager.cs +++ b/Emby.Server.Implementations/Localization/LocalizationManager.cs @@ -77,22 +77,36 @@ namespace Emby.Server.Implementations.Localization var cultures = new List(); foreach (var option in _localizationOptions) { - // Resource files use underscores for some variants (e.g. es_419); - // CultureInfo only accepts hyphenated BCP-47 codes. - var code = option.Value.Replace('_', '-'); - try - { - cultures.Add(CultureInfo.GetCultureInfo(code)); - } - catch (CultureNotFoundException) + // Skip novelty codes (e.g. "pr" Pirate, "jbo" Lojban) that .NET cannot resolve. + if (TryGetCultureInfo(option.Value, out var cultureInfo)) { - // Skip novelty codes (e.g. "pr" Pirate, "jbo" Lojban) that .NET cannot resolve. + cultures.Add(cultureInfo); } } return cultures; } + /// + /// Resolves a Jellyfin resource culture code (which may use underscores, e.g. es_419) + /// to a . Returns for codes .NET cannot resolve. + /// + private static bool TryGetCultureInfo(string cultureCode, [NotNullWhen(true)] out CultureInfo? cultureInfo) + { + try + { + // Resource files use underscores for some variants (e.g. es_419); + // CultureInfo only accepts hyphenated BCP-47 codes. + cultureInfo = CultureInfo.GetCultureInfo(cultureCode.Replace('_', '-')); + return true; + } + catch (CultureNotFoundException) + { + cultureInfo = null; + return false; + } + } + private static void OnConfigurationUpdated(object? sender, EventArgs e) { if (sender is IServerConfigurationManager configManager) @@ -614,20 +628,10 @@ namespace Emby.Server.Implementations.Localization private static string GetDisplayName(string cultureCode) { - // Resource files use underscores for codes that .NET's CultureInfo doesn't accept directly (e.g. es_419). - var lookup = cultureCode.Contains('_', StringComparison.Ordinal) - ? cultureCode.Replace('_', '-') + // Custom/novelty codes like "pr" (Pirate) — fall back to code itself + return TryGetCultureInfo(cultureCode, out var cultureInfo) + ? cultureInfo.NativeName : cultureCode; - - try - { - return CultureInfo.GetCultureInfo(lookup).NativeName; - } - catch (CultureNotFoundException) - { - // Custom/novelty codes like "pr" (Pirate) — fall back to code itself - return cultureCode; - } } /// diff --git a/src/Jellyfin.LiveTv/Channels/RefreshChannelsScheduledTask.cs b/src/Jellyfin.LiveTv/Channels/RefreshChannelsScheduledTask.cs index 71e46764ad..bb4238a2ac 100644 --- a/src/Jellyfin.LiveTv/Channels/RefreshChannelsScheduledTask.cs +++ b/src/Jellyfin.LiveTv/Channels/RefreshChannelsScheduledTask.cs @@ -40,10 +40,10 @@ namespace Jellyfin.LiveTv.Channels } /// - public string Name => _localization.GetLocalizedString("TasksRefreshChannels"); + public string Name => _localization.GetLocalizedString("TaskRefreshChannels"); /// - public string Description => _localization.GetLocalizedString("TasksRefreshChannelsDescription"); + public string Description => _localization.GetLocalizedString("TaskRefreshChannelsDescription"); /// public string Category => _localization.GetLocalizedString("TasksChannelsCategory"); -- cgit v1.2.3