| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Frozen; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Reflection; |
| | | 10 | | using System.Text.Json; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using Jellyfin.Extensions; |
| | | 13 | | using Jellyfin.Extensions.Json; |
| | | 14 | | using MediaBrowser.Controller.Configuration; |
| | | 15 | | using MediaBrowser.Model.Entities; |
| | | 16 | | using MediaBrowser.Model.Globalization; |
| | | 17 | | using Microsoft.Extensions.Logging; |
| | | 18 | | |
| | | 19 | | namespace Emby.Server.Implementations.Localization |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Class LocalizationManager. |
| | | 23 | | /// </summary> |
| | | 24 | | public class LocalizationManager : ILocalizationManager |
| | | 25 | | { |
| | | 26 | | private const string DefaultCulture = "en-US"; |
| | | 27 | | private const string RatingsPath = "Emby.Server.Implementations.Localization.Ratings."; |
| | | 28 | | private const string CulturesPath = "Emby.Server.Implementations.Localization.iso6392.txt"; |
| | | 29 | | private const string CountriesPath = "Emby.Server.Implementations.Localization.countries.json"; |
| | | 30 | | private const string CoreResourcePrefix = "Emby.Server.Implementations.Localization.Core."; |
| | 2 | 31 | | private static readonly Assembly _assembly = typeof(LocalizationManager).Assembly; |
| | 2 | 32 | | private static readonly string[] _unratedValues = ["n/a", "unrated", "not rated", "nr"]; |
| | | 33 | | |
| | | 34 | | private readonly IServerConfigurationManager _configurationManager; |
| | | 35 | | private readonly ILogger<LocalizationManager> _logger; |
| | | 36 | | |
| | 72 | 37 | | private readonly Dictionary<string, Dictionary<string, ParentalRatingScore?>> _allParentalRatings = new(StringCo |
| | | 38 | | |
| | 72 | 39 | | private readonly ConcurrentDictionary<string, Dictionary<string, string>> _cultureOnlyDictionaries = new(StringC |
| | | 40 | | |
| | 72 | 41 | | private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; |
| | | 42 | | |
| | 72 | 43 | | private readonly ConcurrentDictionary<string, CultureDto?> _cultureCache = new(StringComparer.OrdinalIgnoreCase) |
| | 72 | 44 | | private List<CultureDto> _cultures = []; |
| | | 45 | | |
| | 2 | 46 | | private static readonly (IReadOnlyList<LocalizationOption> Options, FrozenDictionary<string, string> Bcp47ToJell |
| | 2 | 47 | | private static readonly IReadOnlyList<LocalizationOption> _localizationOptions = _localizationData.Options; |
| | | 48 | | |
| | | 49 | | // Maps BCP-47 hyphenated culture codes (set by ASP.NET Core's RequestLocalizationMiddleware |
| | | 50 | | // and used as CurrentUICulture.Name) to Jellyfin's underscore-based resource file codes. |
| | | 51 | | // Built reflexively from the resource file scan so both directions stay in sync. |
| | 2 | 52 | | private static readonly FrozenDictionary<string, string> _bcp47ToJellyfinMap = _localizationData.Bcp47ToJellyfin |
| | | 53 | | |
| | | 54 | | private FrozenDictionary<string, string> _iso6392BtoT = null!; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Initializes a new instance of the <see cref="LocalizationManager" /> class. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="configurationManager">The configuration manager.</param> |
| | | 60 | | /// <param name="logger">The logger.</param> |
| | | 61 | | public LocalizationManager( |
| | | 62 | | IServerConfigurationManager configurationManager, |
| | | 63 | | ILogger<LocalizationManager> logger) |
| | | 64 | | { |
| | 72 | 65 | | _configurationManager = configurationManager; |
| | 72 | 66 | | _logger = logger; |
| | | 67 | | |
| | 72 | 68 | | _configurationManager.ConfigurationUpdated += OnConfigurationUpdated; |
| | 72 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the supported UI cultures. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <returns>A list of <see cref="CultureInfo"/> objects covering every embedded translation.</returns> |
| | | 75 | | public static IList<CultureInfo> GetSupportedUICultures() |
| | | 76 | | { |
| | 22 | 77 | | var cultures = new List<CultureInfo>(); |
| | 4620 | 78 | | foreach (var option in _localizationOptions) |
| | | 79 | | { |
| | | 80 | | // Skip novelty codes (e.g. "pr" Pirate, "jbo" Lojban) that .NET cannot resolve. |
| | 2288 | 81 | | if (TryGetCultureInfo(option.Value, out var cultureInfo)) |
| | | 82 | | { |
| | 2288 | 83 | | cultures.Add(cultureInfo); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | 22 | 87 | | return cultures; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Resolves a Jellyfin resource culture code (which may use underscores, e.g. <c>es_419</c>) |
| | | 92 | | /// to a <see cref="CultureInfo"/>. Returns <see langword="false"/> for codes .NET cannot resolve. |
| | | 93 | | /// </summary> |
| | | 94 | | private static bool TryGetCultureInfo(string cultureCode, [NotNullWhen(true)] out CultureInfo? cultureInfo) |
| | | 95 | | { |
| | | 96 | | try |
| | | 97 | | { |
| | | 98 | | // Resource files use underscores for some variants (e.g. es_419); |
| | | 99 | | // CultureInfo only accepts hyphenated BCP-47 codes. |
| | 2494 | 100 | | cultureInfo = CultureInfo.GetCultureInfo(cultureCode.Replace('_', '-')); |
| | 2494 | 101 | | return true; |
| | | 102 | | } |
| | 0 | 103 | | catch (CultureNotFoundException) |
| | | 104 | | { |
| | 0 | 105 | | cultureInfo = null; |
| | 0 | 106 | | return false; |
| | | 107 | | } |
| | 2494 | 108 | | } |
| | | 109 | | |
| | | 110 | | private static void OnConfigurationUpdated(object? sender, EventArgs e) |
| | | 111 | | { |
| | 101 | 112 | | if (sender is IServerConfigurationManager configManager) |
| | | 113 | | { |
| | 101 | 114 | | var uiCulture = configManager.Configuration.UICulture; |
| | 101 | 115 | | if (!string.IsNullOrEmpty(uiCulture)) |
| | | 116 | | { |
| | 101 | 117 | | CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(uiCulture); |
| | | 118 | | } |
| | | 119 | | } |
| | 101 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Loads all resources into memory. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <returns><see cref="Task" />.</returns> |
| | | 126 | | public async Task LoadAll() |
| | | 127 | | { |
| | | 128 | | // Extract from the assembly |
| | 19152 | 129 | | foreach (var resource in _assembly.GetManifestResourceNames()) |
| | | 130 | | { |
| | 9513 | 131 | | if (!resource.StartsWith(RatingsPath, StringComparison.Ordinal)) |
| | | 132 | | { |
| | | 133 | | continue; |
| | | 134 | | } |
| | | 135 | | |
| | 2835 | 136 | | using var stream = _assembly.GetManifestResourceStream(resource); |
| | 2835 | 137 | | if (stream is not null) |
| | | 138 | | { |
| | 2835 | 139 | | var ratingSystem = await JsonSerializer.DeserializeAsync<ParentalRatingSystem>(stream, _jsonOptions) |
| | 2835 | 140 | | ?? throw new InvalidOperationException($"Invalid resource path: '{CountriesPath}'"); |
| | | 141 | | |
| | 2835 | 142 | | var dict = new Dictionary<string, ParentalRatingScore?>(); |
| | 2835 | 143 | | if (ratingSystem.Ratings is not null) |
| | | 144 | | { |
| | 44100 | 145 | | foreach (var ratingEntry in ratingSystem.Ratings) |
| | | 146 | | { |
| | 99036 | 147 | | foreach (var ratingString in ratingEntry.RatingStrings) |
| | | 148 | | { |
| | 30303 | 149 | | dict[ratingString] = ratingEntry.RatingScore; |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | |
| | 2835 | 153 | | _allParentalRatings[ratingSystem.CountryCode] = dict; |
| | | 154 | | } |
| | | 155 | | } |
| | 2835 | 156 | | } |
| | | 157 | | |
| | 63 | 158 | | await LoadCultures().ConfigureAwait(false); |
| | 63 | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Gets the cultures. |
| | | 163 | | /// </summary> |
| | | 164 | | /// <returns><see cref="IEnumerable{CultureDto}" />.</returns> |
| | | 165 | | public IEnumerable<CultureDto> GetCultures() |
| | 1 | 166 | | => _cultures; |
| | | 167 | | |
| | | 168 | | private async Task LoadCultures() |
| | | 169 | | { |
| | 63 | 170 | | List<CultureDto> list = []; |
| | 63 | 171 | | Dictionary<string, string> iso6392BtoTdict = new Dictionary<string, string>(); |
| | | 172 | | |
| | 63 | 173 | | using var stream = _assembly.GetManifestResourceStream(CulturesPath); |
| | 63 | 174 | | if (stream is null) |
| | | 175 | | { |
| | 0 | 176 | | throw new InvalidOperationException($"Invalid resource path: '{CulturesPath}'"); |
| | | 177 | | } |
| | | 178 | | else |
| | | 179 | | { |
| | 63 | 180 | | using var reader = new StreamReader(stream); |
| | 62622 | 181 | | await foreach (var line in reader.ReadAllLinesAsync().ConfigureAwait(false)) |
| | | 182 | | { |
| | 31248 | 183 | | if (string.IsNullOrWhiteSpace(line)) |
| | | 184 | | { |
| | | 185 | | continue; |
| | | 186 | | } |
| | | 187 | | |
| | 31248 | 188 | | var parts = line.Split('|'); |
| | 31248 | 189 | | if (parts.Length != 5) |
| | | 190 | | { |
| | 0 | 191 | | throw new InvalidDataException($"Invalid culture data found at: '{line}'"); |
| | | 192 | | } |
| | | 193 | | |
| | 31248 | 194 | | string name = parts[3]; |
| | 31248 | 195 | | string displayname = parts[3]; |
| | 31248 | 196 | | if (string.IsNullOrWhiteSpace(displayname)) |
| | | 197 | | { |
| | | 198 | | continue; |
| | | 199 | | } |
| | | 200 | | |
| | 31248 | 201 | | string twoCharName = parts[2]; |
| | 31248 | 202 | | if (string.IsNullOrWhiteSpace(twoCharName)) |
| | | 203 | | { |
| | 19026 | 204 | | twoCharName = string.Empty; |
| | | 205 | | } |
| | 12222 | 206 | | else if (twoCharName.Contains('-', StringComparison.OrdinalIgnoreCase)) |
| | | 207 | | { |
| | 567 | 208 | | name = twoCharName; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | string[] threeLetterNames; |
| | 31248 | 212 | | if (string.IsNullOrWhiteSpace(parts[1])) |
| | | 213 | | { |
| | 29610 | 214 | | threeLetterNames = [parts[0]]; |
| | | 215 | | } |
| | | 216 | | else |
| | | 217 | | { |
| | 1638 | 218 | | threeLetterNames = [parts[0], parts[1]]; |
| | | 219 | | |
| | | 220 | | // In cases where there are two TLN the first one is ISO 639-2/T and the second one is ISO 639-2 |
| | | 221 | | // We need ISO 639-2/T for the .NET cultures so we cultivate a dictionary for the translation B- |
| | 1638 | 222 | | iso6392BtoTdict.TryAdd(parts[1], parts[0]); |
| | | 223 | | } |
| | | 224 | | |
| | 31248 | 225 | | list.Add(new CultureDto(name, displayname, twoCharName, threeLetterNames)); |
| | | 226 | | } |
| | | 227 | | |
| | 63 | 228 | | _cultureCache.Clear(); |
| | 63 | 229 | | _cultures = list; |
| | 63 | 230 | | _iso6392BtoT = iso6392BtoTdict.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase); |
| | 63 | 231 | | } |
| | 63 | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <inheritdoc /> |
| | | 235 | | public CultureDto? FindLanguageInfo(string language) |
| | | 236 | | { |
| | 9 | 237 | | if (string.IsNullOrEmpty(language)) |
| | | 238 | | { |
| | 0 | 239 | | return null; |
| | | 240 | | } |
| | | 241 | | |
| | 9 | 242 | | return _cultureCache.GetOrAdd( |
| | 9 | 243 | | language, |
| | 9 | 244 | | static (lang, cultures) => |
| | 9 | 245 | | { |
| | 9 | 246 | | // TODO language should ideally be a ReadOnlySpan but moq cannot mock ref structs |
| | 9 | 247 | | for (var i = 0; i < cultures.Count; i++) |
| | 9 | 248 | | { |
| | 9 | 249 | | var culture = cultures[i]; |
| | 9 | 250 | | if (lang.Equals(culture.DisplayName, StringComparison.OrdinalIgnoreCase) |
| | 9 | 251 | | || lang.Equals(culture.Name, StringComparison.OrdinalIgnoreCase) |
| | 9 | 252 | | || culture.ThreeLetterISOLanguageNames.Contains(lang, StringComparison.OrdinalIgnoreCase) |
| | 9 | 253 | | || lang.Equals(culture.TwoLetterISOLanguageName, StringComparison.OrdinalIgnoreCase)) |
| | 9 | 254 | | { |
| | 9 | 255 | | return culture; |
| | 9 | 256 | | } |
| | 9 | 257 | | } |
| | 9 | 258 | | |
| | 9 | 259 | | return null; |
| | 9 | 260 | | }, |
| | 9 | 261 | | _cultures); |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | /// <inheritdoc /> |
| | | 265 | | public IReadOnlyList<CountryInfo> GetCountries() |
| | | 266 | | { |
| | 1 | 267 | | using var stream = _assembly.GetManifestResourceStream(CountriesPath) ?? throw new InvalidOperationException |
| | | 268 | | |
| | 1 | 269 | | return JsonSerializer.Deserialize<IReadOnlyList<CountryInfo>>(stream, _jsonOptions) ?? []; |
| | 1 | 270 | | } |
| | | 271 | | |
| | | 272 | | /// <inheritdoc /> |
| | | 273 | | public IReadOnlyList<ParentalRating> GetParentalRatings() |
| | | 274 | | { |
| | | 275 | | // Use server default language for ratings |
| | | 276 | | // Fall back to empty list if there are no parental ratings for that language |
| | 2 | 277 | | var ratings = GetParentalRatingsDictionary()?.Select(x => new ParentalRating(x.Key, x.Value)).ToList() ?? [] |
| | | 278 | | |
| | | 279 | | // Add common ratings to ensure them being available for selection |
| | | 280 | | // Based on the US rating system due to it being the main source of rating in the metadata providers |
| | | 281 | | // Unrated |
| | 2 | 282 | | if (!ratings.Any(x => x is null)) |
| | | 283 | | { |
| | 2 | 284 | | ratings.Add(new("Unrated", null)); |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | // Minimum rating possible |
| | 2 | 288 | | if (ratings.All(x => x.RatingScore?.Score != 0)) |
| | | 289 | | { |
| | 0 | 290 | | ratings.Add(new("Approved", new(0, null))); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | // Matches PG (this has different age restrictions depending on country) |
| | 2 | 294 | | if (ratings.All(x => x.RatingScore?.Score != 10)) |
| | | 295 | | { |
| | 1 | 296 | | ratings.Add(new("10", new(10, null))); |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | // Matches PG-13 |
| | 2 | 300 | | if (ratings.All(x => x.RatingScore?.Score != 13)) |
| | | 301 | | { |
| | 1 | 302 | | ratings.Add(new("13", new(13, null))); |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | // Matches TV-14 |
| | 2 | 306 | | if (ratings.All(x => x.RatingScore?.Score != 14)) |
| | | 307 | | { |
| | 1 | 308 | | ratings.Add(new("14", new(14, null))); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | // Catchall if max rating of country is less than 21 |
| | | 312 | | // Using 21 instead of 18 to be sure to allow access to all rated content except adult and banned |
| | 2 | 313 | | if (!ratings.Any(x => x.RatingScore?.Score >= 21)) |
| | | 314 | | { |
| | 2 | 315 | | ratings.Add(new ParentalRating("21", new(21, null))); |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | // A lot of countries don't explicitly have a separate rating for adult content |
| | 2 | 319 | | if (ratings.All(x => x.RatingScore?.Score != 1000)) |
| | | 320 | | { |
| | 2 | 321 | | ratings.Add(new ParentalRating("XXX", new(1000, null))); |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | // A lot of countries don't explicitly have a separate rating for banned content |
| | 2 | 325 | | if (ratings.All(x => x.RatingScore?.Score != 1001)) |
| | | 326 | | { |
| | 2 | 327 | | ratings.Add(new ParentalRating("Banned", new(1001, null))); |
| | | 328 | | } |
| | | 329 | | |
| | 2 | 330 | | return [.. ratings.OrderBy(r => r.RatingScore?.Score).ThenBy(r => r.RatingScore?.SubScore)]; |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | /// <summary> |
| | | 334 | | /// Gets the parental ratings dictionary. |
| | | 335 | | /// </summary> |
| | | 336 | | /// <param name="countryCode">The optional two letter ISO language string.</param> |
| | | 337 | | /// <returns><see cref="Dictionary{String, ParentalRatingScore}" />.</returns> |
| | | 338 | | private Dictionary<string, ParentalRatingScore?>? GetParentalRatingsDictionary(string? countryCode = null) |
| | | 339 | | { |
| | | 340 | | // Fallback to server default if no country code is specified. |
| | 26 | 341 | | if (string.IsNullOrEmpty(countryCode)) |
| | | 342 | | { |
| | 26 | 343 | | countryCode = _configurationManager.Configuration.MetadataCountryCode; |
| | | 344 | | } |
| | | 345 | | |
| | 26 | 346 | | if (_allParentalRatings.TryGetValue(countryCode, out var countryValue)) |
| | | 347 | | { |
| | 25 | 348 | | return countryValue; |
| | | 349 | | } |
| | | 350 | | |
| | 1 | 351 | | return null; |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | /// <inheritdoc /> |
| | | 355 | | public ParentalRatingScore? GetRatingScore(string rating, string? countryCode = null) |
| | | 356 | | { |
| | 34 | 357 | | ArgumentException.ThrowIfNullOrEmpty(rating); |
| | | 358 | | |
| | | 359 | | // Handle unrated content |
| | 34 | 360 | | if (_unratedValues.Contains(rating.AsSpan(), StringComparison.OrdinalIgnoreCase)) |
| | | 361 | | { |
| | 4 | 362 | | return null; |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | // Convert ints directly |
| | | 366 | | // This may override some of the locale specific age ratings (but those always map to the same age) |
| | 30 | 367 | | if (int.TryParse(rating, out var ratingAge)) |
| | | 368 | | { |
| | 6 | 369 | | return new(ratingAge, null); |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | // Fairly common for some users to have "Rated R" in their rating field |
| | 24 | 373 | | rating = rating.Replace("Rated :", string.Empty, StringComparison.OrdinalIgnoreCase) |
| | 24 | 374 | | .Replace("Rated:", string.Empty, StringComparison.OrdinalIgnoreCase) |
| | 24 | 375 | | .Replace("Rated ", string.Empty, StringComparison.OrdinalIgnoreCase) |
| | 24 | 376 | | .Trim(); |
| | | 377 | | |
| | | 378 | | // Use rating system matching the language |
| | 24 | 379 | | if (!string.IsNullOrEmpty(countryCode)) |
| | | 380 | | { |
| | 0 | 381 | | var ratingsDictionary = GetParentalRatingsDictionary(countryCode); |
| | 0 | 382 | | if (ratingsDictionary is not null && ratingsDictionary.TryGetValue(rating, out ParentalRatingScore? valu |
| | | 383 | | { |
| | 0 | 384 | | return value; |
| | | 385 | | } |
| | | 386 | | |
| | 0 | 387 | | if (ratingsDictionary is not null && rating.Length > countryCode.Length |
| | 0 | 388 | | && rating.StartsWith(countryCode, StringComparison.OrdinalIgnoreCase) |
| | 0 | 389 | | && (rating[countryCode.Length] == '-' || rating[countryCode.Length] == ':') |
| | 0 | 390 | | && ratingsDictionary.TryGetValue(rating[(countryCode.Length + 1)..].Trim(), out var normalizedValue) |
| | | 391 | | { |
| | 0 | 392 | | return normalizedValue; |
| | | 393 | | } |
| | | 394 | | } |
| | | 395 | | else |
| | | 396 | | { |
| | | 397 | | // Fall back to server default language for ratings check |
| | 24 | 398 | | var ratingsDictionary = GetParentalRatingsDictionary(); |
| | 24 | 399 | | if (ratingsDictionary is not null && ratingsDictionary.TryGetValue(rating, out ParentalRatingScore? valu |
| | | 400 | | { |
| | 8 | 401 | | return value; |
| | | 402 | | } |
| | | 403 | | } |
| | | 404 | | |
| | | 405 | | // If we don't find anything, check all ratings systems, starting with US |
| | 16 | 406 | | if (_allParentalRatings.TryGetValue("us", out var usRatings) && usRatings.TryGetValue(rating, out var usValu |
| | | 407 | | { |
| | 3 | 408 | | return usValue; |
| | | 409 | | } |
| | | 410 | | |
| | 1016 | 411 | | foreach (var dictionary in _allParentalRatings.Values) |
| | | 412 | | { |
| | 496 | 413 | | if (dictionary.TryGetValue(rating, out var value)) |
| | | 414 | | { |
| | 2 | 415 | | return value; |
| | | 416 | | } |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | // Try splitting by country prefix separator to handle "US:PG-13", "Germany: FSK-18", "DE-FSK-18" |
| | 11 | 420 | | if (TryGetRatingScoreBySeparator(rating, ':', out var result) |
| | 11 | 421 | | || TryGetRatingScoreBySeparator(rating, '-', out result)) |
| | | 422 | | { |
| | 9 | 423 | | return result; |
| | | 424 | | } |
| | | 425 | | |
| | 2 | 426 | | return null; |
| | 2 | 427 | | } |
| | | 428 | | |
| | | 429 | | private bool TryGetRatingScoreBySeparator(string rating, char separator, out ParentalRatingScore? result) |
| | | 430 | | { |
| | 15 | 431 | | result = null; |
| | | 432 | | |
| | 15 | 433 | | if (rating.IndexOf(separator, StringComparison.Ordinal) < 0) |
| | | 434 | | { |
| | 4 | 435 | | return false; |
| | | 436 | | } |
| | | 437 | | |
| | 11 | 438 | | var ratingSpan = rating.AsSpan(); |
| | 11 | 439 | | var countryPart = ratingSpan.LeftPart(separator).Trim().ToString(); |
| | 11 | 440 | | var ratingPart = ratingSpan.RightPart(separator).Trim().ToString(); |
| | 11 | 441 | | if (ratingPart.Length == 0) |
| | | 442 | | { |
| | 2 | 443 | | return false; |
| | | 444 | | } |
| | | 445 | | |
| | 9 | 446 | | string? resolvedCountryCode = null; |
| | | 447 | | |
| | 9 | 448 | | if (_allParentalRatings.ContainsKey(countryPart)) |
| | | 449 | | { |
| | 8 | 450 | | resolvedCountryCode = countryPart; |
| | | 451 | | } |
| | | 452 | | else |
| | | 453 | | { |
| | 1 | 454 | | var culture = FindLanguageInfo(countryPart); |
| | 1 | 455 | | if (culture is not null) |
| | | 456 | | { |
| | 0 | 457 | | resolvedCountryCode = culture.TwoLetterISOLanguageName; |
| | | 458 | | } |
| | | 459 | | } |
| | | 460 | | |
| | 9 | 461 | | if (resolvedCountryCode is not null |
| | 9 | 462 | | && _allParentalRatings.TryGetValue(resolvedCountryCode, out var countryRatings)) |
| | | 463 | | { |
| | 8 | 464 | | if (countryRatings.TryGetValue(ratingPart, out result)) |
| | | 465 | | { |
| | 4 | 466 | | return true; |
| | | 467 | | } |
| | | 468 | | |
| | 4 | 469 | | _logger.LogWarning( |
| | 4 | 470 | | "Rating '{Rating}' not found in the '{CountryCode}' rating system, treating as unrated", |
| | 4 | 471 | | rating, |
| | 4 | 472 | | resolvedCountryCode); |
| | | 473 | | |
| | 4 | 474 | | return true; |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | // Country not identified or no rating data available, try recursive lookup |
| | 1 | 478 | | result = GetRatingScore(ratingPart, resolvedCountryCode); |
| | | 479 | | |
| | 1 | 480 | | return true; |
| | | 481 | | } |
| | | 482 | | |
| | | 483 | | /// <inheritdoc /> |
| | | 484 | | public string GetLocalizedString(string phrase) |
| | | 485 | | { |
| | 445 | 486 | | return GetLocalizedString(phrase, CultureInfo.CurrentUICulture.Name); |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | /// <inheritdoc /> |
| | | 490 | | public string GetServerLocalizedString(string phrase) |
| | | 491 | | { |
| | 65 | 492 | | return GetLocalizedString(phrase, _configurationManager.Configuration.UICulture); |
| | | 493 | | } |
| | | 494 | | |
| | | 495 | | /// <inheritdoc /> |
| | | 496 | | public string GetLocalizedString(string phrase, string culture) |
| | | 497 | | { |
| | 513 | 498 | | if (string.IsNullOrEmpty(culture)) |
| | | 499 | | { |
| | 0 | 500 | | culture = _configurationManager.Configuration.UICulture; |
| | | 501 | | } |
| | | 502 | | |
| | 513 | 503 | | if (string.IsNullOrEmpty(culture)) |
| | | 504 | | { |
| | 0 | 505 | | culture = DefaultCulture; |
| | | 506 | | } |
| | | 507 | | |
| | | 508 | | // Normalize BCP-47 hyphenated codes to Jellyfin's underscore-based codes |
| | 513 | 509 | | if (_bcp47ToJellyfinMap.TryGetValue(culture, out var mapped)) |
| | | 510 | | { |
| | 1 | 511 | | culture = mapped; |
| | | 512 | | } |
| | | 513 | | |
| | 513 | 514 | | var dictionary = GetLocalizationDictionary(culture); |
| | | 515 | | |
| | 513 | 516 | | if (dictionary.TryGetValue(phrase, out var value)) |
| | | 517 | | { |
| | 504 | 518 | | return value; |
| | | 519 | | } |
| | | 520 | | |
| | 9 | 521 | | if (!string.Equals(culture, DefaultCulture, StringComparison.OrdinalIgnoreCase)) |
| | | 522 | | { |
| | 8 | 523 | | var fallback = GetLocalizationDictionary(DefaultCulture); |
| | 8 | 524 | | if (fallback.TryGetValue(phrase, out var fallbackValue)) |
| | | 525 | | { |
| | 8 | 526 | | return fallbackValue; |
| | | 527 | | } |
| | | 528 | | } |
| | | 529 | | |
| | 1 | 530 | | return phrase; |
| | | 531 | | } |
| | | 532 | | |
| | | 533 | | private Dictionary<string, string> GetLocalizationDictionary(string culture) |
| | | 534 | | { |
| | 521 | 535 | | ArgumentException.ThrowIfNullOrEmpty(culture); |
| | | 536 | | |
| | 521 | 537 | | return _cultureOnlyDictionaries.GetOrAdd( |
| | 521 | 538 | | culture, |
| | 521 | 539 | | static (key, localizationManager) => |
| | 521 | 540 | | { |
| | 521 | 541 | | var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 521 | 542 | | var namespaceName = localizationManager.GetType().Namespace + ".Core"; |
| | 521 | 543 | | localizationManager.CopyInto(dictionary, namespaceName + "." + GetResourceFilename(key)).GetAwaiter( |
| | 521 | 544 | | |
| | 521 | 545 | | return dictionary; |
| | 521 | 546 | | }, |
| | 521 | 547 | | this); |
| | | 548 | | } |
| | | 549 | | |
| | | 550 | | private async Task CopyInto(IDictionary<string, string> dictionary, string resourcePath) |
| | | 551 | | { |
| | 31 | 552 | | using var stream = _assembly.GetManifestResourceStream(resourcePath); |
| | | 553 | | // If a Culture doesn't have a translation the stream will be null and it defaults to en-us further up the c |
| | 31 | 554 | | if (stream is null) |
| | | 555 | | { |
| | 2 | 556 | | _logger.LogError("Missing translation/culture resource: {ResourcePath}", resourcePath); |
| | 2 | 557 | | return; |
| | | 558 | | } |
| | | 559 | | |
| | 29 | 560 | | var dict = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(stream, _jsonOptions).Configure |
| | 6434 | 561 | | foreach (var key in dict.Keys) |
| | | 562 | | { |
| | 3188 | 563 | | dictionary[key] = dict[key]; |
| | | 564 | | } |
| | 31 | 565 | | } |
| | | 566 | | |
| | | 567 | | private static string GetResourceFilename(string culture) |
| | | 568 | | { |
| | 31 | 569 | | var parts = culture.Split('-'); |
| | | 570 | | |
| | 31 | 571 | | if (parts.Length == 2) |
| | | 572 | | { |
| | 25 | 573 | | culture = parts[0].ToLowerInvariant() + "-" + parts[1].ToUpperInvariant(); |
| | | 574 | | } |
| | | 575 | | else |
| | | 576 | | { |
| | 6 | 577 | | culture = culture.ToLowerInvariant(); |
| | | 578 | | } |
| | | 579 | | |
| | 31 | 580 | | return culture + ".json"; |
| | | 581 | | } |
| | | 582 | | |
| | | 583 | | /// <inheritdoc /> |
| | | 584 | | public IEnumerable<LocalizationOption> GetLocalizationOptions() |
| | | 585 | | { |
| | 0 | 586 | | return _localizationOptions; |
| | | 587 | | } |
| | | 588 | | |
| | | 589 | | private static (IReadOnlyList<LocalizationOption> Options, FrozenDictionary<string, string> Bcp47ToJellyfinMap) |
| | | 590 | | { |
| | 2 | 591 | | var options = new List<LocalizationOption>(); |
| | 2 | 592 | | var bcp47Map = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| | 2 | 593 | | var prefix = CoreResourcePrefix; |
| | | 594 | | |
| | 608 | 595 | | foreach (var resource in _assembly.GetManifestResourceNames()) |
| | | 596 | | { |
| | 302 | 597 | | if (!resource.StartsWith(prefix, StringComparison.Ordinal) |
| | 302 | 598 | | || !resource.EndsWith(".json", StringComparison.Ordinal)) |
| | | 599 | | { |
| | | 600 | | continue; |
| | | 601 | | } |
| | | 602 | | |
| | | 603 | | // Extract culture code from resource name: "...Core.de.json" -> "de", "...Core.pt-BR.json" -> "pt-BR" |
| | 208 | 604 | | var code = resource[prefix.Length..^5]; |
| | | 605 | | |
| | | 606 | | // Record the BCP-47 → Jellyfin mapping for any resource file using underscores. |
| | 208 | 607 | | if (code.Contains('_', StringComparison.Ordinal)) |
| | | 608 | | { |
| | 10 | 609 | | bcp47Map[code.Replace('_', '-')] = code; |
| | | 610 | | } |
| | | 611 | | |
| | | 612 | | // Skip the base language file — en-US is added explicitly below |
| | 208 | 613 | | if (code.Equals(DefaultCulture, StringComparison.OrdinalIgnoreCase)) |
| | | 614 | | { |
| | | 615 | | continue; |
| | | 616 | | } |
| | | 617 | | |
| | 206 | 618 | | var displayName = GetDisplayName(code); |
| | 206 | 619 | | options.Add(new LocalizationOption(displayName, code)); |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | // Ensure en-US is always present |
| | 2 | 623 | | options.Add(new LocalizationOption("English", DefaultCulture)); |
| | | 624 | | |
| | 2 | 625 | | options.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase)); |
| | 2 | 626 | | return (options, bcp47Map.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase)); |
| | | 627 | | } |
| | | 628 | | |
| | | 629 | | private static string GetDisplayName(string cultureCode) |
| | | 630 | | { |
| | | 631 | | // Custom/novelty codes like "pr" (Pirate) — fall back to code itself |
| | 206 | 632 | | return TryGetCultureInfo(cultureCode, out var cultureInfo) |
| | 206 | 633 | | ? cultureInfo.NativeName |
| | 206 | 634 | | : cultureCode; |
| | | 635 | | } |
| | | 636 | | |
| | | 637 | | /// <inheritdoc /> |
| | | 638 | | public bool TryGetISO6392TFromB(string isoB, [NotNullWhen(true)] out string? isoT) |
| | | 639 | | { |
| | | 640 | | // Unlikely case the dictionary is not (yet) initialized properly |
| | 3 | 641 | | if (_iso6392BtoT is null) |
| | | 642 | | { |
| | 0 | 643 | | isoT = null; |
| | 0 | 644 | | return false; |
| | | 645 | | } |
| | | 646 | | |
| | 3 | 647 | | var result = _iso6392BtoT.TryGetValue(isoB, out isoT) && !string.IsNullOrEmpty(isoT); |
| | | 648 | | |
| | | 649 | | // Ensure the ISO code being null if the result is false |
| | 3 | 650 | | if (!result) |
| | | 651 | | { |
| | 1 | 652 | | isoT = null; |
| | | 653 | | } |
| | | 654 | | |
| | 3 | 655 | | return result; |
| | | 656 | | } |
| | | 657 | | } |
| | | 658 | | } |