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

435 lines
16 KiB
C#
Raw Normal View History

using System;
2013-06-11 02:34:55 +00:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Globalization;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
2016-11-05 02:17:18 +00:00
namespace Emby.Server.Implementations.Localization
{
/// <summary>
/// Class LocalizationManager
/// </summary>
public class LocalizationManager : ILocalizationManager
{
/// <summary>
/// The _configuration manager
/// </summary>
private readonly IServerConfigurationManager _configurationManager;
/// <summary>
/// The us culture
/// </summary>
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
2018-09-12 17:26:21 +00:00
private readonly Dictionary<string, Dictionary<string, ParentalRating>> _allParentalRatings =
new Dictionary<string, Dictionary<string, ParentalRating>>(StringComparer.OrdinalIgnoreCase);
2013-06-11 02:34:55 +00:00
private readonly IFileSystem _fileSystem;
2014-03-31 01:00:47 +00:00
private readonly IJsonSerializer _jsonSerializer;
2016-06-30 23:17:49 +00:00
private readonly ILogger _logger;
private static readonly Assembly _assembly = typeof(LocalizationManager).Assembly;
2014-03-31 01:00:47 +00:00
/// <summary>
2014-06-05 02:32:40 +00:00
/// Initializes a new instance of the <see cref="LocalizationManager" /> class.
/// </summary>
/// <param name="configurationManager">The configuration manager.</param>
2014-06-05 02:32:40 +00:00
/// <param name="fileSystem">The file system.</param>
/// <param name="jsonSerializer">The json serializer.</param>
public LocalizationManager(
IServerConfigurationManager configurationManager,
IFileSystem fileSystem,
IJsonSerializer jsonSerializer,
ILoggerFactory loggerFactory)
{
_configurationManager = configurationManager;
_fileSystem = fileSystem;
2014-03-31 01:00:47 +00:00
_jsonSerializer = jsonSerializer;
_logger = loggerFactory.CreateLogger(nameof(LocalizationManager));
2013-06-18 20:54:32 +00:00
}
public async Task LoadAll()
2013-06-18 20:54:32 +00:00
{
2019-02-04 17:46:36 +00:00
const string ratingsResource = "Emby.Server.Implementations.Localization.Ratings.";
2013-06-18 20:54:32 +00:00
// Extract from the assembly
2019-02-04 17:46:36 +00:00
foreach (var resource in _assembly.GetManifestResourceNames())
2013-06-18 20:54:32 +00:00
{
2019-02-04 17:46:36 +00:00
if (!resource.StartsWith(ratingsResource))
{
continue;
}
2019-03-01 18:30:48 +00:00
string countryCode = resource.Substring(ratingsResource.Length, 2);
var dict = new Dictionary<string, ParentalRating>(StringComparer.OrdinalIgnoreCase);
2016-06-30 23:17:49 +00:00
2019-03-01 18:30:48 +00:00
using (var str = _assembly.GetManifestResourceStream(resource))
using (var reader = new StreamReader(str))
2019-02-04 17:46:36 +00:00
{
2019-03-01 18:30:48 +00:00
string line;
while ((line = await reader.ReadLineAsync()) != null)
2019-02-04 17:46:36 +00:00
{
2019-03-01 18:30:48 +00:00
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
string[] parts = line.Split(',');
if (parts.Length == 2
&& int.TryParse(parts[1], NumberStyles.Integer, UsCulture, out var value))
{
dict.Add(parts[0], (new ParentalRating { Name = parts[0], Value = value }));
}
#if DEBUG
else
{
_logger.LogWarning("Misformed line in ratings file for country {CountryCode}", countryCode);
}
#endif
2013-06-18 20:54:32 +00:00
}
}
2019-03-01 18:30:48 +00:00
_logger.LogWarning("{t}", countryCode);
2019-03-01 18:30:48 +00:00
_allParentalRatings[countryCode] = dict;
2013-06-18 20:54:32 +00:00
}
2018-09-12 17:26:21 +00:00
await LoadCultures();
2018-09-12 17:26:21 +00:00
}
2016-10-27 07:58:33 +00:00
public string NormalizeFormKD(string text)
=> text.Normalize(NormalizationForm.FormKD);
2016-10-27 07:58:33 +00:00
private CultureDto[] _cultures;
/// <summary>
/// Gets the cultures.
/// </summary>
/// <returns>IEnumerable{CultureDto}.</returns>
2017-08-19 19:43:35 +00:00
public CultureDto[] GetCultures()
=> _cultures;
private async Task LoadCultures()
{
List<CultureDto> list = new List<CultureDto>();
2014-06-18 15:12:20 +00:00
const string path = "Emby.Server.Implementations.Localization.iso6392.txt";
2014-05-07 02:28:19 +00:00
using (var stream = _assembly.GetManifestResourceStream(path))
using (var reader = new StreamReader(stream))
2014-05-07 02:28:19 +00:00
{
while (!reader.EndOfStream)
2014-06-18 15:12:20 +00:00
{
var line = await reader.ReadLineAsync();
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
var parts = line.Split('|');
if (parts.Length == 5)
2014-06-18 15:12:20 +00:00
{
string name = parts[3];
if (string.IsNullOrWhiteSpace(name))
{
continue;
}
2014-06-18 15:12:20 +00:00
string twoCharName = parts[2];
if (string.IsNullOrWhiteSpace(twoCharName))
{
continue;
}
2014-06-18 15:12:20 +00:00
string[] threeletterNames;
if (string.IsNullOrWhiteSpace(parts[1]))
2014-06-18 15:12:20 +00:00
{
threeletterNames = new [] { parts[0] };
2014-06-18 15:12:20 +00:00
}
else
2014-06-18 15:12:20 +00:00
{
threeletterNames = new [] { parts[0], parts[1] };
2014-06-18 15:12:20 +00:00
}
list.Add(new CultureDto
{
DisplayName = name,
Name = name,
ThreeLetterISOLanguageNames = threeletterNames,
TwoLetterISOLanguageName = twoCharName
});
2014-06-18 15:12:20 +00:00
}
}
2014-05-07 02:28:19 +00:00
}
2014-06-18 15:12:20 +00:00
_cultures = list.ToArray();
}
2018-09-12 17:26:21 +00:00
public CultureDto FindLanguageInfo(string language)
=> GetCultures()
.FirstOrDefault(i =>
string.Equals(i.DisplayName, language, StringComparison.OrdinalIgnoreCase)
|| string.Equals(i.Name, language, StringComparison.OrdinalIgnoreCase)
|| i.ThreeLetterISOLanguageNames.Contains(language, StringComparer.OrdinalIgnoreCase)
|| string.Equals(i.TwoLetterISOLanguageName, language, StringComparison.OrdinalIgnoreCase));
2018-09-12 17:26:21 +00:00
/// <summary>
/// Gets the countries.
/// </summary>
/// <returns>IEnumerable{CountryInfo}.</returns>
public Task<CountryInfo[]> GetCountries()
=> _jsonSerializer.DeserializeFromStreamAsync<CountryInfo[]>(
_assembly.GetManifestResourceStream("Emby.Server.Implementations.Localization.countries.json"));
/// <summary>
/// Gets the parental ratings.
/// </summary>
/// <returns>IEnumerable{ParentalRating}.</returns>
public IEnumerable<ParentalRating> GetParentalRatings()
=> GetParentalRatingsDictionary().Values;
2013-06-11 02:34:55 +00:00
/// <summary>
/// Gets the parental ratings dictionary.
/// </summary>
/// <returns>Dictionary{System.StringParentalRating}.</returns>
private Dictionary<string, ParentalRating> GetParentalRatingsDictionary()
{
var countryCode = _configurationManager.Configuration.MetadataCountryCode;
if (string.IsNullOrEmpty(countryCode))
{
countryCode = "us";
}
return GetRatings(countryCode) ?? GetRatings("us");
2013-06-11 02:34:55 +00:00
}
/// <summary>
/// Gets the ratings.
/// </summary>
/// <param name="countryCode">The country code.</param>
private Dictionary<string, ParentalRating> GetRatings(string countryCode)
{
_allParentalRatings.TryGetValue(countryCode, out var value);
2013-06-11 02:34:55 +00:00
return value;
}
private static readonly string[] _unratedValues = { "n/a", "unrated", "not rated" };
2015-11-06 15:02:22 +00:00
/// <summary>
/// Gets the rating level.
/// </summary>
public int? GetRatingLevel(string rating)
{
if (string.IsNullOrEmpty(rating))
{
throw new ArgumentNullException(nameof(rating));
}
2015-11-06 15:02:22 +00:00
if (_unratedValues.Contains(rating, StringComparer.OrdinalIgnoreCase))
{
return null;
}
2015-05-11 16:32:15 +00:00
// Fairly common for some users to have "Rated R" in their rating field
rating = rating.Replace("Rated ", string.Empty, StringComparison.OrdinalIgnoreCase);
2013-06-11 02:34:55 +00:00
var ratingsDictionary = GetParentalRatingsDictionary();
if (ratingsDictionary.TryGetValue(rating, out ParentalRating value))
2013-06-11 02:34:55 +00:00
{
2018-09-12 17:26:21 +00:00
return value.Value;
}
// If we don't find anything check all ratings systems
foreach (var dictionary in _allParentalRatings.Values)
{
if (dictionary.TryGetValue(rating, out value))
2013-06-18 20:54:32 +00:00
{
2018-09-12 17:26:21 +00:00
return value.Value;
}
}
// Try splitting by : to handle "Germany: FSK 18"
var index = rating.IndexOf(':');
if (index != -1)
{
rating = rating.Substring(index).TrimStart(':').Trim();
if (!string.IsNullOrWhiteSpace(rating))
{
return GetRatingLevel(rating);
2013-06-18 20:54:32 +00:00
}
2013-06-11 02:34:55 +00:00
}
2018-09-12 17:26:21 +00:00
// TODO: Further improve by normalizing out all spaces and dashes
return null;
}
2014-03-31 01:00:47 +00:00
2017-11-01 19:50:16 +00:00
public bool HasUnicodeCategory(string value, UnicodeCategory category)
{
foreach (var chr in value)
{
if (char.GetUnicodeCategory(chr) == category)
{
return true;
}
}
return false;
}
2014-03-31 01:00:47 +00:00
public string GetLocalizedString(string phrase)
{
return GetLocalizedString(phrase, _configurationManager.Configuration.UICulture);
}
public string GetLocalizedString(string phrase, string culture)
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(culture))
2017-10-20 16:16:56 +00:00
{
culture = _configurationManager.Configuration.UICulture;
}
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(culture))
2017-10-21 16:39:52 +00:00
{
culture = DefaultCulture;
}
2017-10-20 16:16:56 +00:00
2014-03-31 01:00:47 +00:00
var dictionary = GetLocalizationDictionary(culture);
if (dictionary.TryGetValue(phrase, out var value))
2014-03-31 01:00:47 +00:00
{
return value;
}
return phrase;
}
private const string DefaultCulture = "en-US";
2017-10-21 16:39:52 +00:00
2014-03-31 01:00:47 +00:00
private readonly ConcurrentDictionary<string, Dictionary<string, string>> _dictionaries =
new ConcurrentDictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, string> GetLocalizationDictionary(string culture)
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(culture))
2017-10-21 16:39:52 +00:00
{
throw new ArgumentNullException(nameof(culture));
2017-10-21 16:39:52 +00:00
}
2015-07-27 18:18:10 +00:00
const string prefix = "Core";
2014-03-31 01:00:47 +00:00
var key = prefix + culture;
return _dictionaries.GetOrAdd(key,
f => GetDictionary(prefix, culture, DefaultCulture + ".json").GetAwaiter().GetResult());
2014-03-31 01:00:47 +00:00
}
private async Task<Dictionary<string, string>> GetDictionary(string prefix, string culture, string baseFilename)
2014-03-31 01:00:47 +00:00
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(culture))
2017-10-21 16:39:52 +00:00
{
throw new ArgumentNullException(nameof(culture));
2017-10-21 16:39:52 +00:00
}
2014-03-31 01:00:47 +00:00
var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var namespaceName = GetType().Namespace + "." + prefix;
await CopyInto(dictionary, namespaceName + "." + baseFilename);
await CopyInto(dictionary, namespaceName + "." + GetResourceFilename(culture));
2014-03-31 01:00:47 +00:00
return dictionary;
}
private async Task CopyInto(IDictionary<string, string> dictionary, string resourcePath)
2014-03-31 01:00:47 +00:00
{
using (var stream = _assembly.GetManifestResourceStream(resourcePath))
2014-03-31 01:00:47 +00:00
{
// 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.DeserializeFromStreamAsync<Dictionary<string, string>>(stream);
foreach (var key in dict.Keys)
{
dictionary[key] = dict[key];
}
2014-03-31 01:00:47 +00:00
}
else
{
_logger.LogError("Missing translation/culture resource: {ResourcePath}", resourcePath);
}
2014-03-31 01:00:47 +00:00
}
}
private static string GetResourceFilename(string culture)
2014-03-31 01:00:47 +00:00
{
var parts = culture.Split('-');
if (parts.Length == 2)
{
2019-01-27 11:03:43 +00:00
culture = parts[0].ToLowerInvariant() + "-" + parts[1].ToUpperInvariant();
2014-03-31 01:00:47 +00:00
}
else
{
2019-01-27 11:03:43 +00:00
culture = culture.ToLowerInvariant();
2014-03-31 01:00:47 +00:00
}
return culture + ".json";
}
2019-01-25 21:27:33 +00:00
public LocalizationOption[] GetLocalizationOptions()
=> new LocalizationOption[]
2014-03-31 01:00:47 +00:00
{
2019-01-25 21:27:33 +00:00
new LocalizationOption("Arabic", "ar"),
new LocalizationOption("Bulgarian (Bulgaria)", "bg-BG"),
new LocalizationOption("Catalan", "ca"),
new LocalizationOption("Chinese Simplified", "zh-CN"),
new LocalizationOption("Chinese Traditional", "zh-TW"),
new LocalizationOption("Croatian", "hr"),
new LocalizationOption("Czech", "cs"),
new LocalizationOption("Danish", "da"),
new LocalizationOption("Dutch", "nl"),
new LocalizationOption("English (United Kingdom)", "en-GB"),
new LocalizationOption("English (United States)", "en-US"),
new LocalizationOption("French", "fr"),
new LocalizationOption("French (Canada)", "fr-CA"),
new LocalizationOption("German", "de"),
new LocalizationOption("Greek", "el"),
new LocalizationOption("Hebrew", "he"),
new LocalizationOption("Hungarian", "hu"),
new LocalizationOption("Italian", "it"),
new LocalizationOption("Kazakh", "kk"),
new LocalizationOption("Korean", "ko"),
new LocalizationOption("Lithuanian", "lt-LT"),
new LocalizationOption("Malay", "ms"),
new LocalizationOption("Norwegian Bokmål", "nb"),
new LocalizationOption("Persian", "fa"),
new LocalizationOption("Polish", "pl"),
new LocalizationOption("Portuguese (Brazil)", "pt-BR"),
new LocalizationOption("Portuguese (Portugal)", "pt-PT"),
new LocalizationOption("Russian", "ru"),
new LocalizationOption("Slovak", "sk"),
new LocalizationOption("Slovenian (Slovenia)", "sl-SI"),
new LocalizationOption("Spanish", "es"),
new LocalizationOption("Spanish (Argentina)", "es-AR"),
2019-01-25 21:27:33 +00:00
new LocalizationOption("Spanish (Mexico)", "es-MX"),
new LocalizationOption("Swedish", "sv"),
new LocalizationOption("Swiss German", "gsw"),
new LocalizationOption("Turkish", "tr")
2017-08-19 19:43:35 +00:00
};
}
}