Browse Source

Refactor LocalizationManager and remove dead method

David Ullmer 4 years ago
parent
commit
db2b53a4b5

+ 190 - 210
Emby.Server.Implementations/Localization/LocalizationManager.cs

@@ -25,18 +25,18 @@ namespace Emby.Server.Implementations.Localization
         private static readonly Assembly _assembly = typeof(LocalizationManager).Assembly;
         private static readonly string[] _unratedValues = { "n/a", "unrated", "not rated" };
 
-        private readonly IServerConfigurationManager _configurationManager;
-        private readonly ILogger<LocalizationManager> _logger;
-
         private readonly Dictionary<string, Dictionary<string, ParentalRating>> _allParentalRatings =
             new Dictionary<string, Dictionary<string, ParentalRating>>(StringComparer.OrdinalIgnoreCase);
 
+        private readonly IServerConfigurationManager _configurationManager;
+
         private readonly ConcurrentDictionary<string, Dictionary<string, string>> _dictionaries =
             new ConcurrentDictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
 
-        private List<CultureDto> _cultures;
-
         private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
+        private readonly ILogger<LocalizationManager> _logger;
+
+        private List<CultureDto> _cultures;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="LocalizationManager" /> class.
@@ -51,57 +51,6 @@ namespace Emby.Server.Implementations.Localization
             _logger = logger;
         }
 
-        /// <summary>
-        /// Loads all resources into memory.
-        /// </summary>
-        /// <returns><see cref="Task" />.</returns>
-        public async Task LoadAll()
-        {
-            const string RatingsResource = "Emby.Server.Implementations.Localization.Ratings.";
-
-            // Extract from the assembly
-            foreach (var resource in _assembly.GetManifestResourceNames())
-            {
-                if (!resource.StartsWith(RatingsResource, StringComparison.Ordinal))
-                {
-                    continue;
-                }
-
-                string countryCode = resource.Substring(RatingsResource.Length, 2);
-                var dict = new Dictionary<string, ParentalRating>(StringComparer.OrdinalIgnoreCase);
-
-                using (var str = _assembly.GetManifestResourceStream(resource))
-                using (var reader = new StreamReader(str))
-                {
-                    await foreach (var line in reader.ReadAllLinesAsync().ConfigureAwait(false))
-                    {
-                        if (string.IsNullOrWhiteSpace(line))
-                        {
-                            continue;
-                        }
-
-                        string[] parts = line.Split(',');
-                        if (parts.Length == 2
-                            && int.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
-                        {
-                            var name = parts[0];
-                            dict.Add(name, new ParentalRating(name, value));
-                        }
-#if DEBUG
-                        else
-                        {
-                            _logger.LogWarning("Malformed line in ratings file for country {CountryCode}", countryCode);
-                        }
-#endif
-                    }
-                }
-
-                _allParentalRatings[countryCode] = dict;
-            }
-
-            await LoadCultures().ConfigureAwait(false);
-        }
-
         /// <summary>
         /// Gets the cultures.
         /// </summary>
@@ -109,62 +58,6 @@ namespace Emby.Server.Implementations.Localization
         public IEnumerable<CultureDto> GetCultures()
             => _cultures;
 
-        private async Task LoadCultures()
-        {
-            List<CultureDto> list = new List<CultureDto>();
-
-            const string ResourcePath = "Emby.Server.Implementations.Localization.iso6392.txt";
-
-            using (var stream = _assembly.GetManifestResourceStream(ResourcePath))
-            using (var reader = new StreamReader(stream))
-            {
-                await foreach (var line in reader.ReadAllLinesAsync().ConfigureAwait(false))
-                {
-                    if (string.IsNullOrWhiteSpace(line))
-                    {
-                        continue;
-                    }
-
-                    var parts = line.Split('|');
-
-                    if (parts.Length == 5)
-                    {
-                        string name = parts[3];
-                        if (string.IsNullOrWhiteSpace(name))
-                        {
-                            continue;
-                        }
-
-                        string twoCharName = parts[2];
-                        if (string.IsNullOrWhiteSpace(twoCharName))
-                        {
-                            continue;
-                        }
-
-                        string[] threeletterNames;
-                        if (string.IsNullOrWhiteSpace(parts[1]))
-                        {
-                            threeletterNames = new[] { parts[0] };
-                        }
-                        else
-                        {
-                            threeletterNames = new[] { parts[0], parts[1] };
-                        }
-
-                        list.Add(new CultureDto
-                        {
-                            DisplayName = name,
-                            Name = name,
-                            ThreeLetterISOLanguageNames = threeletterNames,
-                            TwoLetterISOLanguageName = twoCharName
-                        });
-                    }
-                }
-            }
-
-            _cultures = list;
-        }
-
         /// <inheritdoc />
         public CultureDto FindLanguageInfo(string language)
             => GetCultures()
@@ -186,34 +79,6 @@ namespace Emby.Server.Implementations.Localization
         public IEnumerable<ParentalRating> GetParentalRatings()
             => GetParentalRatingsDictionary().Values;
 
-        /// <summary>
-        /// Gets the parental ratings dictionary.
-        /// </summary>
-        /// <returns><see cref="Dictionary{String, ParentalRating}" />.</returns>
-        private Dictionary<string, ParentalRating> GetParentalRatingsDictionary()
-        {
-            var countryCode = _configurationManager.Configuration.MetadataCountryCode;
-
-            if (string.IsNullOrEmpty(countryCode))
-            {
-                countryCode = "us";
-            }
-
-            return GetRatings(countryCode) ?? GetRatings("us");
-        }
-
-        /// <summary>
-        /// Gets the ratings.
-        /// </summary>
-        /// <param name="countryCode">The country code.</param>
-        /// <returns>The ratings.</returns>
-        private Dictionary<string, ParentalRating> GetRatings(string countryCode)
-        {
-            _allParentalRatings.TryGetValue(countryCode, out var value);
-
-            return value;
-        }
-
         /// <inheritdoc />
         public int? GetRatingLevel(string rating)
         {
@@ -250,7 +115,7 @@ namespace Emby.Server.Implementations.Localization
             var index = rating.IndexOf(':', StringComparison.Ordinal);
             if (index != -1)
             {
-                rating = rating.Substring(index).TrimStart(':').Trim();
+                rating = rating.Substring(index + 1).Trim();
 
                 if (!string.IsNullOrWhiteSpace(rating))
                 {
@@ -262,20 +127,6 @@ namespace Emby.Server.Implementations.Localization
             return null;
         }
 
-        /// <inheritdoc />
-        public bool HasUnicodeCategory(string value, UnicodeCategory category)
-        {
-            foreach (var chr in value)
-            {
-                if (char.GetUnicodeCategory(chr) == category)
-                {
-                    return true;
-                }
-            }
-
-            return false;
-        }
-
         /// <inheritdoc />
         public string GetLocalizedString(string phrase)
         {
@@ -305,6 +156,179 @@ namespace Emby.Server.Implementations.Localization
             return phrase;
         }
 
+        /// <inheritdoc />
+        public IEnumerable<LocalizationOption> GetLocalizationOptions()
+        {
+            yield return new LocalizationOption("Arabic", "ar");
+            yield return new LocalizationOption("Bulgarian (Bulgaria)", "bg-BG");
+            yield return new LocalizationOption("Catalan", "ca");
+            yield return new LocalizationOption("Chinese Simplified", "zh-CN");
+            yield return new LocalizationOption("Chinese Traditional", "zh-TW");
+            yield return new LocalizationOption("Croatian", "hr");
+            yield return new LocalizationOption("Czech", "cs");
+            yield return new LocalizationOption("Danish", "da");
+            yield return new LocalizationOption("Dutch", "nl");
+            yield return new LocalizationOption("English (United Kingdom)", "en-GB");
+            yield return new LocalizationOption("English (United States)", "en-US");
+            yield return new LocalizationOption("French", "fr");
+            yield return new LocalizationOption("French (Canada)", "fr-CA");
+            yield return new LocalizationOption("German", "de");
+            yield return new LocalizationOption("Greek", "el");
+            yield return new LocalizationOption("Hebrew", "he");
+            yield return new LocalizationOption("Hungarian", "hu");
+            yield return new LocalizationOption("Italian", "it");
+            yield return new LocalizationOption("Kazakh", "kk");
+            yield return new LocalizationOption("Korean", "ko");
+            yield return new LocalizationOption("Lithuanian", "lt-LT");
+            yield return new LocalizationOption("Malay", "ms");
+            yield return new LocalizationOption("Norwegian Bokmål", "nb");
+            yield return new LocalizationOption("Persian", "fa");
+            yield return new LocalizationOption("Polish", "pl");
+            yield return new LocalizationOption("Portuguese (Brazil)", "pt-BR");
+            yield return new LocalizationOption("Portuguese (Portugal)", "pt-PT");
+            yield return new LocalizationOption("Russian", "ru");
+            yield return new LocalizationOption("Slovak", "sk");
+            yield return new LocalizationOption("Slovenian (Slovenia)", "sl-SI");
+            yield return new LocalizationOption("Spanish", "es");
+            yield return new LocalizationOption("Spanish (Argentina)", "es-AR");
+            yield return new LocalizationOption("Spanish (Mexico)", "es-MX");
+            yield return new LocalizationOption("Swedish", "sv");
+            yield return new LocalizationOption("Swiss German", "gsw");
+            yield return new LocalizationOption("Turkish", "tr");
+            yield return new LocalizationOption("Tiếng Việt", "vi");
+        }
+
+        /// <summary>
+        /// Loads all resources into memory.
+        /// </summary>
+        /// <returns><see cref="Task" />.</returns>
+        public async Task LoadAll()
+        {
+            const string RatingsResource = "Emby.Server.Implementations.Localization.Ratings.";
+
+            // Extract from the assembly
+            foreach (var resource in _assembly.GetManifestResourceNames())
+            {
+                if (!resource.StartsWith(RatingsResource, StringComparison.Ordinal))
+                {
+                    continue;
+                }
+
+                string countryCode = resource.Substring(RatingsResource.Length, 2);
+                var dict = new Dictionary<string, ParentalRating>(StringComparer.OrdinalIgnoreCase);
+
+                await using var str = _assembly.GetManifestResourceStream(resource);
+                using var reader = new StreamReader(str);
+                await foreach (var line in reader.ReadAllLinesAsync().ConfigureAwait(false))
+                {
+                    if (string.IsNullOrWhiteSpace(line))
+                    {
+                        continue;
+                    }
+
+                    string[] parts = line.Split(',');
+                    if (parts.Length == 2
+                        && int.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
+                    {
+                        var name = parts[0];
+                        dict.Add(name, new ParentalRating(name, value));
+                    }
+#if DEBUG
+                    else
+                    {
+                        _logger.LogWarning("Malformed line in ratings file for country {CountryCode}", countryCode);
+                    }
+#endif
+                }
+
+                _allParentalRatings[countryCode] = dict;
+            }
+
+            await LoadCultures().ConfigureAwait(false);
+        }
+
+        private async Task LoadCultures()
+        {
+            List<CultureDto> list = new List<CultureDto>();
+
+            const string ResourcePath = "Emby.Server.Implementations.Localization.iso6392.txt";
+
+            await using var stream = _assembly.GetManifestResourceStream(ResourcePath);
+            using var reader = new StreamReader(stream);
+            await foreach (var line in reader.ReadAllLinesAsync().ConfigureAwait(false))
+            {
+                if (string.IsNullOrWhiteSpace(line))
+                {
+                    continue;
+                }
+
+                var parts = line.Split('|');
+
+                if (parts.Length == 5)
+                {
+                    string name = parts[3];
+                    if (string.IsNullOrWhiteSpace(name))
+                    {
+                        continue;
+                    }
+
+                    string twoCharName = parts[2];
+                    if (string.IsNullOrWhiteSpace(twoCharName))
+                    {
+                        continue;
+                    }
+
+                    string[] threeletterNames;
+                    if (string.IsNullOrWhiteSpace(parts[1]))
+                    {
+                        threeletterNames = new[] { parts[0] };
+                    }
+                    else
+                    {
+                        threeletterNames = new[] { parts[0], parts[1] };
+                    }
+
+                    list.Add(new CultureDto
+                    {
+                        DisplayName = name,
+                        Name = name,
+                        ThreeLetterISOLanguageNames = threeletterNames,
+                        TwoLetterISOLanguageName = twoCharName
+                    });
+                }
+            }
+
+            _cultures = list;
+        }
+
+        /// <summary>
+        /// Gets the parental ratings dictionary.
+        /// </summary>
+        /// <returns><see cref="Dictionary{String, ParentalRating}" />.</returns>
+        private Dictionary<string, ParentalRating> GetParentalRatingsDictionary()
+        {
+            var countryCode = _configurationManager.Configuration.MetadataCountryCode;
+
+            if (string.IsNullOrEmpty(countryCode))
+            {
+                countryCode = "us";
+            }
+
+            return GetRatings(countryCode) ?? GetRatings("us");
+        }
+
+        /// <summary>
+        /// Gets the ratings.
+        /// </summary>
+        /// <param name="countryCode">The country code.</param>
+        /// <returns>The ratings.</returns>
+        private Dictionary<string, ParentalRating> GetRatings(string countryCode)
+        {
+            _allParentalRatings.TryGetValue(countryCode, out var value);
+
+            return value;
+        }
+
         private Dictionary<string, string> GetLocalizationDictionary(string culture)
         {
             if (string.IsNullOrEmpty(culture))
@@ -316,7 +340,7 @@ namespace Emby.Server.Implementations.Localization
 
             return _dictionaries.GetOrAdd(
                 culture,
-                f => GetDictionary(Prefix, culture, DefaultCulture + ".json").GetAwaiter().GetResult());
+                _ => GetDictionary(Prefix, culture, DefaultCulture + ".json").GetAwaiter().GetResult());
         }
 
         private async Task<Dictionary<string, string>> GetDictionary(string prefix, string culture, string baseFilename)
@@ -338,23 +362,21 @@ namespace Emby.Server.Implementations.Localization
 
         private async Task CopyInto(IDictionary<string, string> dictionary, string resourcePath)
         {
-            using (var stream = _assembly.GetManifestResourceStream(resourcePath))
+            await using var stream = _assembly.GetManifestResourceStream(resourcePath);
+            // If a Culture doesn't have a translation the stream will be null and it defaults to en-us further up the chain
+            if (stream != null)
             {
-                // If a Culture doesn't have a translation the stream will be null and it defaults to en-us further up the chain
-                if (stream != null)
-                {
-                    var dict = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(stream, _jsonOptions).ConfigureAwait(false);
+                var dict = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(stream, _jsonOptions).ConfigureAwait(false);
 
-                    foreach (var key in dict.Keys)
-                    {
-                        dictionary[key] = dict[key];
-                    }
-                }
-                else
+                foreach (var key in dict.Keys)
                 {
-                    _logger.LogError("Missing translation/culture resource: {ResourcePath}", resourcePath);
+                    dictionary[key] = dict[key];
                 }
             }
+            else
+            {
+                _logger.LogError("Missing translation/culture resource: {ResourcePath}", resourcePath);
+            }
         }
 
         private static string GetResourceFilename(string culture)
@@ -372,47 +394,5 @@ namespace Emby.Server.Implementations.Localization
 
             return culture + ".json";
         }
-
-        /// <inheritdoc />
-        public IEnumerable<LocalizationOption> GetLocalizationOptions()
-        {
-            yield return new LocalizationOption("Arabic", "ar");
-            yield return new LocalizationOption("Bulgarian (Bulgaria)", "bg-BG");
-            yield return new LocalizationOption("Catalan", "ca");
-            yield return new LocalizationOption("Chinese Simplified", "zh-CN");
-            yield return new LocalizationOption("Chinese Traditional", "zh-TW");
-            yield return new LocalizationOption("Croatian", "hr");
-            yield return new LocalizationOption("Czech", "cs");
-            yield return new LocalizationOption("Danish", "da");
-            yield return new LocalizationOption("Dutch", "nl");
-            yield return new LocalizationOption("English (United Kingdom)", "en-GB");
-            yield return new LocalizationOption("English (United States)", "en-US");
-            yield return new LocalizationOption("French", "fr");
-            yield return new LocalizationOption("French (Canada)", "fr-CA");
-            yield return new LocalizationOption("German", "de");
-            yield return new LocalizationOption("Greek", "el");
-            yield return new LocalizationOption("Hebrew", "he");
-            yield return new LocalizationOption("Hungarian", "hu");
-            yield return new LocalizationOption("Italian", "it");
-            yield return new LocalizationOption("Kazakh", "kk");
-            yield return new LocalizationOption("Korean", "ko");
-            yield return new LocalizationOption("Lithuanian", "lt-LT");
-            yield return new LocalizationOption("Malay", "ms");
-            yield return new LocalizationOption("Norwegian Bokmål", "nb");
-            yield return new LocalizationOption("Persian", "fa");
-            yield return new LocalizationOption("Polish", "pl");
-            yield return new LocalizationOption("Portuguese (Brazil)", "pt-BR");
-            yield return new LocalizationOption("Portuguese (Portugal)", "pt-PT");
-            yield return new LocalizationOption("Russian", "ru");
-            yield return new LocalizationOption("Slovak", "sk");
-            yield return new LocalizationOption("Slovenian (Slovenia)", "sl-SI");
-            yield return new LocalizationOption("Spanish", "es");
-            yield return new LocalizationOption("Spanish (Argentina)", "es-AR");
-            yield return new LocalizationOption("Spanish (Mexico)", "es-MX");
-            yield return new LocalizationOption("Swedish", "sv");
-            yield return new LocalizationOption("Swiss German", "gsw");
-            yield return new LocalizationOption("Turkish", "tr");
-            yield return new LocalizationOption("Tiếng Việt", "vi");
-        }
     }
 }

+ 0 - 8
MediaBrowser.Model/Globalization/ILocalizationManager.cs

@@ -56,14 +56,6 @@ namespace MediaBrowser.Model.Globalization
         /// <returns><see cref="IEnumerable{LocalizatonOption}" />.</returns>
         IEnumerable<LocalizationOption> GetLocalizationOptions();
 
-        /// <summary>
-        /// Checks if the string contains a character with the specified unicode category.
-        /// </summary>
-        /// <param name="value">The string.</param>
-        /// <param name="category">The unicode category.</param>
-        /// <returns>Wether or not the string contains a character with the specified unicode category.</returns>
-        bool HasUnicodeCategory(string value, UnicodeCategory category);
-
         /// <summary>
         /// Returns the correct <see cref="CultureInfo" /> for the given language.
         /// </summary>