< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Localization.LocalizationManager
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Localization/LocalizationManager.cs
Line coverage
96%
Covered lines: 91
Uncovered lines: 3
Coverable lines: 94
Total lines: 509
Line coverage: 96.8%
Branch coverage
89%
Covered branches: 68
Total branches: 76
Branch coverage: 89.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
GetCultures()100%11100%
FindLanguageInfo(...)100%1010100%
GetCountries()50%44100%
GetParentalRatings()85%202094.44%
GetParentalRatingsDictionary(...)100%44100%
GetRatingScore(...)96.66%3030100%
GetLocalizedString(...)100%11100%
GetLocalizedString(...)66.66%7675%
GetLocalizationDictionary(...)100%11100%
GetResourceFilename(...)100%22100%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Localization/LocalizationManager.cs

#LineLine coverage
 1using System;
 2using System.Collections.Concurrent;
 3using System.Collections.Generic;
 4using System.IO;
 5using System.Linq;
 6using System.Reflection;
 7using System.Text.Json;
 8using System.Threading.Tasks;
 9using Jellyfin.Extensions;
 10using Jellyfin.Extensions.Json;
 11using MediaBrowser.Controller.Configuration;
 12using MediaBrowser.Model.Entities;
 13using MediaBrowser.Model.Globalization;
 14using Microsoft.Extensions.Logging;
 15
 16namespace Emby.Server.Implementations.Localization
 17{
 18    /// <summary>
 19    /// Class LocalizationManager.
 20    /// </summary>
 21    public class LocalizationManager : ILocalizationManager
 22    {
 23        private const string DefaultCulture = "en-US";
 24        private const string RatingsPath = "Emby.Server.Implementations.Localization.Ratings.";
 25        private const string CulturesPath = "Emby.Server.Implementations.Localization.iso6392.txt";
 26        private const string CountriesPath = "Emby.Server.Implementations.Localization.countries.json";
 227        private static readonly Assembly _assembly = typeof(LocalizationManager).Assembly;
 228        private static readonly string[] _unratedValues = ["n/a", "unrated", "not rated", "nr"];
 29
 30        private readonly IServerConfigurationManager _configurationManager;
 31        private readonly ILogger<LocalizationManager> _logger;
 32
 5233        private readonly Dictionary<string, Dictionary<string, ParentalRatingScore?>> _allParentalRatings = new(StringCo
 34
 5235        private readonly ConcurrentDictionary<string, Dictionary<string, string>> _dictionaries = new(StringComparer.Ord
 36
 5237        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
 38
 5239        private List<CultureDto> _cultures = [];
 40
 41        /// <summary>
 42        /// Initializes a new instance of the <see cref="LocalizationManager" /> class.
 43        /// </summary>
 44        /// <param name="configurationManager">The configuration manager.</param>
 45        /// <param name="logger">The logger.</param>
 46        public LocalizationManager(
 47            IServerConfigurationManager configurationManager,
 48            ILogger<LocalizationManager> logger)
 49        {
 5250            _configurationManager = configurationManager;
 5251            _logger = logger;
 5252        }
 53
 54        /// <summary>
 55        /// Loads all resources into memory.
 56        /// </summary>
 57        /// <returns><see cref="Task" />.</returns>
 58        public async Task LoadAll()
 59        {
 60            // Extract from the assembly
 61            foreach (var resource in _assembly.GetManifestResourceNames())
 62            {
 63                if (!resource.StartsWith(RatingsPath, StringComparison.Ordinal))
 64                {
 65                    continue;
 66                }
 67
 68                using var stream = _assembly.GetManifestResourceStream(resource);
 69                if (stream is not null)
 70                {
 71                    var ratingSystem = await JsonSerializer.DeserializeAsync<ParentalRatingSystem>(stream, _jsonOptions)
 72                                ?? throw new InvalidOperationException($"Invalid resource path: '{CountriesPath}'");
 73
 74                    var dict = new Dictionary<string, ParentalRatingScore?>();
 75                    if (ratingSystem.Ratings is not null)
 76                    {
 77                        foreach (var ratingEntry in ratingSystem.Ratings)
 78                        {
 79                            foreach (var ratingString in ratingEntry.RatingStrings)
 80                            {
 81                                dict[ratingString] = ratingEntry.RatingScore;
 82                            }
 83                        }
 84
 85                        _allParentalRatings[ratingSystem.CountryCode] = dict;
 86                    }
 87                }
 88            }
 89
 90            await LoadCultures().ConfigureAwait(false);
 91        }
 92
 93        /// <summary>
 94        /// Gets the cultures.
 95        /// </summary>
 96        /// <returns><see cref="IEnumerable{CultureDto}" />.</returns>
 97        public IEnumerable<CultureDto> GetCultures()
 198            => _cultures;
 99
 100        private async Task LoadCultures()
 101        {
 102            List<CultureDto> list = [];
 103
 104            using var stream = _assembly.GetManifestResourceStream(CulturesPath);
 105            if (stream is null)
 106            {
 107                throw new InvalidOperationException($"Invalid resource path: '{CulturesPath}'");
 108            }
 109            else
 110            {
 111                using var reader = new StreamReader(stream);
 112                await foreach (var line in reader.ReadAllLinesAsync().ConfigureAwait(false))
 113                {
 114                    if (string.IsNullOrWhiteSpace(line))
 115                    {
 116                        continue;
 117                    }
 118
 119                    var parts = line.Split('|');
 120                    if (parts.Length != 5)
 121                    {
 122                        throw new InvalidDataException($"Invalid culture data found at: '{line}'");
 123                    }
 124
 125                    string name = parts[3];
 126                    if (string.IsNullOrWhiteSpace(name))
 127                    {
 128                        continue;
 129                    }
 130
 131                    string twoCharName = parts[2];
 132                    if (string.IsNullOrWhiteSpace(twoCharName))
 133                    {
 134                        continue;
 135                    }
 136
 137                    string[] threeLetterNames;
 138                    if (string.IsNullOrWhiteSpace(parts[1]))
 139                    {
 140                        threeLetterNames = [parts[0]];
 141                    }
 142                    else
 143                    {
 144                        threeLetterNames = [parts[0], parts[1]];
 145                    }
 146
 147                    list.Add(new CultureDto(name, name, twoCharName, threeLetterNames));
 148                }
 149
 150                _cultures = list;
 151            }
 152        }
 153
 154        /// <inheritdoc />
 155        public CultureDto? FindLanguageInfo(string language)
 156        {
 157            // TODO language should ideally be a ReadOnlySpan but moq cannot mock ref structs
 702158            for (var i = 0; i < _cultures.Count; i++)
 159            {
 350160                var culture = _cultures[i];
 350161                if (language.Equals(culture.DisplayName, StringComparison.OrdinalIgnoreCase)
 350162                    || language.Equals(culture.Name, StringComparison.OrdinalIgnoreCase)
 350163                    || culture.ThreeLetterISOLanguageNames.Contains(language, StringComparison.OrdinalIgnoreCase)
 350164                    || language.Equals(culture.TwoLetterISOLanguageName, StringComparison.OrdinalIgnoreCase))
 165                {
 5166                    return culture;
 167                }
 168            }
 169
 1170            return default;
 171        }
 172
 173        /// <inheritdoc />
 174        public IReadOnlyList<CountryInfo> GetCountries()
 175        {
 1176            using var stream = _assembly.GetManifestResourceStream(CountriesPath) ?? throw new InvalidOperationException
 177
 1178            return JsonSerializer.Deserialize<IReadOnlyList<CountryInfo>>(stream, _jsonOptions) ?? [];
 1179        }
 180
 181        /// <inheritdoc />
 182        public IReadOnlyList<ParentalRating> GetParentalRatings()
 183        {
 184            // Use server default language for ratings
 185            // Fall back to empty list if there are no parental ratings for that language
 2186            var ratings = GetParentalRatingsDictionary()?.Select(x => new ParentalRating(x.Key, x.Value)).ToList() ?? []
 187
 188            // Add common ratings to ensure them being available for selection
 189            // Based on the US rating system due to it being the main source of rating in the metadata providers
 190            // Unrated
 2191            if (!ratings.Any(x => x is null))
 192            {
 2193                ratings.Add(new("Unrated", null));
 194            }
 195
 196            // Minimum rating possible
 2197            if (ratings.All(x => x.RatingScore?.Score != 0))
 198            {
 0199                ratings.Add(new("Approved", new(0, null)));
 200            }
 201
 202            // Matches PG (this has different age restrictions depending on country)
 2203            if (ratings.All(x => x.RatingScore?.Score != 10))
 204            {
 1205                ratings.Add(new("10", new(10, null)));
 206            }
 207
 208            // Matches PG-13
 2209            if (ratings.All(x => x.RatingScore?.Score != 13))
 210            {
 1211                ratings.Add(new("13", new(13, null)));
 212            }
 213
 214            // Matches TV-14
 2215            if (ratings.All(x => x.RatingScore?.Score != 14))
 216            {
 1217                ratings.Add(new("14", new(14, null)));
 218            }
 219
 220            // Catchall if max rating of country is less than 21
 221            // Using 21 instead of 18 to be sure to allow access to all rated content except adult and banned
 2222            if (!ratings.Any(x => x.RatingScore?.Score >= 21))
 223            {
 2224                ratings.Add(new ParentalRating("21", new(21, null)));
 225            }
 226
 227            // A lot of countries don't explicitly have a separate rating for adult content
 2228            if (ratings.All(x => x.RatingScore?.Score != 1000))
 229            {
 2230                ratings.Add(new ParentalRating("XXX",  new(1000, null)));
 231            }
 232
 233            // A lot of countries don't explicitly have a separate rating for banned content
 2234            if (ratings.All(x => x.RatingScore?.Score != 1001))
 235            {
 2236                ratings.Add(new ParentalRating("Banned",  new(1001, null)));
 237            }
 238
 2239            return [.. ratings.OrderBy(r => r.RatingScore?.Score).ThenBy(r => r.RatingScore?.SubScore)];
 240        }
 241
 242        /// <summary>
 243        /// Gets the parental ratings dictionary.
 244        /// </summary>
 245        /// <param name="countryCode">The optional two letter ISO language string.</param>
 246        /// <returns><see cref="Dictionary{String, ParentalRatingScore}" />.</returns>
 247        private Dictionary<string, ParentalRatingScore?>? GetParentalRatingsDictionary(string? countryCode = null)
 248        {
 249            // Fallback to server default if no country code is specified.
 18250            if (string.IsNullOrEmpty(countryCode))
 251            {
 16252                countryCode = _configurationManager.Configuration.MetadataCountryCode;
 253            }
 254
 18255            if (_allParentalRatings.TryGetValue(countryCode, out var countryValue))
 256            {
 17257                return countryValue;
 258            }
 259
 1260            return null;
 261        }
 262
 263        /// <inheritdoc />
 264        public ParentalRatingScore? GetRatingScore(string rating, string? countryCode = null)
 265        {
 25266            ArgumentException.ThrowIfNullOrEmpty(rating);
 267
 268            // Handle unrated content
 25269            if (_unratedValues.Contains(rating.AsSpan(), StringComparison.OrdinalIgnoreCase))
 270            {
 4271                return null;
 272            }
 273
 274            // Convert ints directly
 275            // This may override some of the locale specific age ratings (but those always map to the same age)
 21276            if (int.TryParse(rating, out var ratingAge))
 277            {
 6278                return new(ratingAge, null);
 279            }
 280
 281            // Fairly common for some users to have "Rated R" in their rating field
 15282            rating = rating.Replace("Rated :", string.Empty, StringComparison.OrdinalIgnoreCase)
 15283                            .Replace("Rated:", string.Empty, StringComparison.OrdinalIgnoreCase)
 15284                            .Replace("Rated ", string.Empty, StringComparison.OrdinalIgnoreCase)
 15285                            .Trim();
 286
 287            // Use rating system matching the language
 15288            if (!string.IsNullOrEmpty(countryCode))
 289            {
 1290                var ratingsDictionary = GetParentalRatingsDictionary(countryCode);
 1291                if (ratingsDictionary is not null && ratingsDictionary.TryGetValue(rating, out ParentalRatingScore? valu
 292                {
 1293                    return value;
 294                }
 295            }
 296            else
 297            {
 298                // Fall back to server default language for ratings check
 299                // If it has no ratings, use the US ratings
 14300                var ratingsDictionary = GetParentalRatingsDictionary() ?? GetParentalRatingsDictionary("us");
 14301                if (ratingsDictionary is not null && ratingsDictionary.TryGetValue(rating, out ParentalRatingScore? valu
 302                {
 8303                    return value;
 304                }
 305            }
 306
 307            // If we don't find anything, check all ratings systems
 228308            foreach (var dictionary in _allParentalRatings.Values)
 309            {
 109310                if (dictionary.TryGetValue(rating, out var value))
 311                {
 2312                    return value;
 313                }
 314            }
 315
 316            // Try splitting by : to handle "Germany: FSK-18"
 4317            if (rating.Contains(':', StringComparison.OrdinalIgnoreCase))
 318            {
 2319                var ratingLevelRightPart = rating.AsSpan().RightPart(':');
 2320                if (ratingLevelRightPart.Length != 0)
 321                {
 1322                    return GetRatingScore(ratingLevelRightPart.ToString());
 323                }
 324            }
 325
 326            // Handle prefix country code to handle "DE-18"
 3327            if (rating.Contains('-', StringComparison.OrdinalIgnoreCase))
 328            {
 2329                var ratingSpan = rating.AsSpan();
 330
 331                // Extract culture from country prefix
 2332                var culture = FindLanguageInfo(ratingSpan.LeftPart('-').ToString());
 333
 2334                var ratingLevelRightPart = ratingSpan.RightPart('-');
 2335                if (ratingLevelRightPart.Length != 0)
 336                {
 337                    // Check rating system of culture
 1338                    return GetRatingScore(ratingLevelRightPart.ToString(), culture?.TwoLetterISOLanguageName);
 339                }
 340            }
 341
 2342            return null;
 2343        }
 344
 345        /// <inheritdoc />
 346        public string GetLocalizedString(string phrase)
 347        {
 555348            return GetLocalizedString(phrase, _configurationManager.Configuration.UICulture);
 349        }
 350
 351        /// <inheritdoc />
 352        public string GetLocalizedString(string phrase, string culture)
 353        {
 555354            if (string.IsNullOrEmpty(culture))
 355            {
 0356                culture = _configurationManager.Configuration.UICulture;
 357            }
 358
 555359            if (string.IsNullOrEmpty(culture))
 360            {
 0361                culture = DefaultCulture;
 362            }
 363
 555364            var dictionary = GetLocalizationDictionary(culture);
 365
 555366            if (dictionary.TryGetValue(phrase, out var value))
 367            {
 533368                return value;
 369            }
 370
 22371            return phrase;
 372        }
 373
 374        private Dictionary<string, string> GetLocalizationDictionary(string culture)
 375        {
 555376            ArgumentException.ThrowIfNullOrEmpty(culture);
 377
 378            const string Prefix = "Core";
 379
 555380            return _dictionaries.GetOrAdd(
 555381                culture,
 555382                static (key, localizationManager) => localizationManager.GetDictionary(Prefix, key, DefaultCulture + ".j
 555383                this);
 384        }
 385
 386        private async Task<Dictionary<string, string>> GetDictionary(string prefix, string culture, string baseFilename)
 387        {
 388            ArgumentException.ThrowIfNullOrEmpty(culture);
 389
 390            var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
 391
 392            var namespaceName = GetType().Namespace + "." + prefix;
 393
 394            await CopyInto(dictionary, namespaceName + "." + baseFilename).ConfigureAwait(false);
 395            await CopyInto(dictionary, namespaceName + "." + GetResourceFilename(culture)).ConfigureAwait(false);
 396
 397            return dictionary;
 398        }
 399
 400        private async Task CopyInto(IDictionary<string, string> dictionary, string resourcePath)
 401        {
 402            using var stream = _assembly.GetManifestResourceStream(resourcePath);
 403            // If a Culture doesn't have a translation the stream will be null and it defaults to en-us further up the c
 404            if (stream is null)
 405            {
 406                _logger.LogError("Missing translation/culture resource: {ResourcePath}", resourcePath);
 407                return;
 408            }
 409
 410            var dict = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(stream, _jsonOptions).Configure
 411            foreach (var key in dict.Keys)
 412            {
 413                dictionary[key] = dict[key];
 414            }
 415        }
 416
 417        private static string GetResourceFilename(string culture)
 418        {
 25419            var parts = culture.Split('-');
 420
 25421            if (parts.Length == 2)
 422            {
 24423                culture = parts[0].ToLowerInvariant() + "-" + parts[1].ToUpperInvariant();
 424            }
 425            else
 426            {
 1427                culture = culture.ToLowerInvariant();
 428            }
 429
 25430            return culture + ".json";
 431        }
 432
 433        /// <inheritdoc />
 434        public IEnumerable<LocalizationOption> GetLocalizationOptions()
 435        {
 436            yield return new LocalizationOption("Afrikaans", "af");
 437            yield return new LocalizationOption("العربية", "ar");
 438            yield return new LocalizationOption("Беларуская", "be");
 439            yield return new LocalizationOption("Български", "bg-BG");
 440            yield return new LocalizationOption("বাংলা (বাংলাদেশ)", "bn");
 441            yield return new LocalizationOption("Català", "ca");
 442            yield return new LocalizationOption("Čeština", "cs");
 443            yield return new LocalizationOption("Cymraeg", "cy");
 444            yield return new LocalizationOption("Dansk", "da");
 445            yield return new LocalizationOption("Deutsch", "de");
 446            yield return new LocalizationOption("English (United Kingdom)", "en-GB");
 447            yield return new LocalizationOption("English", "en-US");
 448            yield return new LocalizationOption("Ελληνικά", "el");
 449            yield return new LocalizationOption("Esperanto", "eo");
 450            yield return new LocalizationOption("Español", "es");
 451            yield return new LocalizationOption("Español americano", "es_419");
 452            yield return new LocalizationOption("Español (Argentina)", "es-AR");
 453            yield return new LocalizationOption("Español (Dominicana)", "es_DO");
 454            yield return new LocalizationOption("Español (México)", "es-MX");
 455            yield return new LocalizationOption("Eesti", "et");
 456            yield return new LocalizationOption("Basque", "eu");
 457            yield return new LocalizationOption("فارسی", "fa");
 458            yield return new LocalizationOption("Suomi", "fi");
 459            yield return new LocalizationOption("Filipino", "fil");
 460            yield return new LocalizationOption("Français", "fr");
 461            yield return new LocalizationOption("Français (Canada)", "fr-CA");
 462            yield return new LocalizationOption("Galego", "gl");
 463            yield return new LocalizationOption("Schwiizerdütsch", "gsw");
 464            yield return new LocalizationOption("עִבְרִית", "he");
 465            yield return new LocalizationOption("हिन्दी", "hi");
 466            yield return new LocalizationOption("Hrvatski", "hr");
 467            yield return new LocalizationOption("Magyar", "hu");
 468            yield return new LocalizationOption("Bahasa Indonesia", "id");
 469            yield return new LocalizationOption("Íslenska", "is");
 470            yield return new LocalizationOption("Italiano", "it");
 471            yield return new LocalizationOption("日本語", "ja");
 472            yield return new LocalizationOption("Qazaqşa", "kk");
 473            yield return new LocalizationOption("한국어", "ko");
 474            yield return new LocalizationOption("Lietuvių", "lt");
 475            yield return new LocalizationOption("Latviešu", "lv");
 476            yield return new LocalizationOption("Македонски", "mk");
 477            yield return new LocalizationOption("മലയാളം", "ml");
 478            yield return new LocalizationOption("मराठी", "mr");
 479            yield return new LocalizationOption("Bahasa Melayu", "ms");
 480            yield return new LocalizationOption("Norsk bokmål", "nb");
 481            yield return new LocalizationOption("नेपाली", "ne");
 482            yield return new LocalizationOption("Nederlands", "nl");
 483            yield return new LocalizationOption("Norsk nynorsk", "nn");
 484            yield return new LocalizationOption("ਪੰਜਾਬੀ", "pa");
 485            yield return new LocalizationOption("Polski", "pl");
 486            yield return new LocalizationOption("Pirate", "pr");
 487            yield return new LocalizationOption("Português", "pt");
 488            yield return new LocalizationOption("Português (Brasil)", "pt-BR");
 489            yield return new LocalizationOption("Português (Portugal)", "pt-PT");
 490            yield return new LocalizationOption("Românește", "ro");
 491            yield return new LocalizationOption("Русский", "ru");
 492            yield return new LocalizationOption("Slovenčina", "sk");
 493            yield return new LocalizationOption("Slovenščina", "sl-SI");
 494            yield return new LocalizationOption("Shqip", "sq");
 495            yield return new LocalizationOption("Српски", "sr");
 496            yield return new LocalizationOption("Svenska", "sv");
 497            yield return new LocalizationOption("தமிழ்", "ta");
 498            yield return new LocalizationOption("తెలుగు", "te");
 499            yield return new LocalizationOption("ภาษาไทย", "th");
 500            yield return new LocalizationOption("Türkçe", "tr");
 501            yield return new LocalizationOption("Українська", "uk");
 502            yield return new LocalizationOption("اُردُو", "ur_PK");
 503            yield return new LocalizationOption("Tiếng Việt", "vi");
 504            yield return new LocalizationOption("汉语 (简体字)", "zh-CN");
 505            yield return new LocalizationOption("漢語 (繁體字)", "zh-TW");
 506            yield return new LocalizationOption("廣東話 (香港)", "zh-HK");
 507        }
 508    }
 509}