#nullable disable
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using Jellyfin.Extensions;
using Jellyfin.Extensions.Json;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.Localization
{
///
/// Class LocalizationManager.
///
public class LocalizationManager : ILocalizationManager
{
private const string DefaultCulture = "en-US";
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 _logger;
private readonly Dictionary> _allParentalRatings =
new Dictionary>(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary> _dictionaries =
new ConcurrentDictionary>(StringComparer.OrdinalIgnoreCase);
private List _cultures;
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
///
/// Initializes a new instance of the class.
///
/// The configuration manager.
/// The logger.
public LocalizationManager(
IServerConfigurationManager configurationManager,
ILogger logger)
{
_configurationManager = configurationManager;
_logger = logger;
}
///
/// Loads all resources into memory.
///
/// .
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(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);
}
///
/// Gets the cultures.
///
/// .
public IEnumerable GetCultures()
=> _cultures;
private async Task LoadCultures()
{
List list = new List();
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;
}
///
public CultureDto FindLanguageInfo(string language)
{
// TODO language should ideally be a ReadOnlySpan but moq cannot mock ref structs
for (var i = 0; i < _cultures.Count; i++)
{
var culture = _cultures[i];
if (language.Equals(culture.DisplayName, StringComparison.OrdinalIgnoreCase)
|| language.Equals(culture.Name, StringComparison.OrdinalIgnoreCase)
|| culture.ThreeLetterISOLanguageNames.Contains(language, StringComparison.OrdinalIgnoreCase)
|| language.Equals(culture.TwoLetterISOLanguageName, StringComparison.OrdinalIgnoreCase))
{
return culture;
}
}
return default;
}
///
public IEnumerable GetCountries()
{
using StreamReader reader = new StreamReader(_assembly.GetManifestResourceStream("Emby.Server.Implementations.Localization.countries.json"));
return JsonSerializer.Deserialize>(reader.ReadToEnd(), _jsonOptions);
}
///
public IEnumerable GetParentalRatings()
=> GetParentalRatingsDictionary().Values;
///
/// Gets the parental ratings dictionary.
///
/// .
private Dictionary GetParentalRatingsDictionary()
{
var countryCode = _configurationManager.Configuration.MetadataCountryCode;
if (string.IsNullOrEmpty(countryCode))
{
countryCode = "us";
}
return GetRatings(countryCode) ?? GetRatings("us");
}
///
/// Gets the ratings.
///
/// The country code.
/// The ratings.
private Dictionary GetRatings(string countryCode)
{
_allParentalRatings.TryGetValue(countryCode, out var value);
return value;
}
///
public int? GetRatingLevel(string rating)
{
if (string.IsNullOrEmpty(rating))
{
throw new ArgumentNullException(nameof(rating));
}
if (_unratedValues.Contains(rating.AsSpan(), StringComparison.OrdinalIgnoreCase))
{
return null;
}
// Fairly common for some users to have "Rated R" in their rating field
rating = rating.Replace("Rated ", string.Empty, StringComparison.OrdinalIgnoreCase);
var ratingsDictionary = GetParentalRatingsDictionary();
if (ratingsDictionary.TryGetValue(rating, out ParentalRating value))
{
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))
{
return value.Value;
}
}
// Try splitting by : to handle "Germany: FSK 18"
var index = rating.IndexOf(':', StringComparison.Ordinal);
if (index != -1)
{
var trimmedRating = rating.AsSpan(index).TrimStart(':').Trim();
if (!trimmedRating.IsEmpty)
{
return GetRatingLevel(trimmedRating.ToString());
}
}
// TODO: Further improve by normalizing out all spaces and dashes
return null;
}
///
public bool HasUnicodeCategory(string value, UnicodeCategory category)
{
foreach (var chr in value)
{
if (char.GetUnicodeCategory(chr) == category)
{
return true;
}
}
return false;
}
///
public string GetLocalizedString(string phrase)
{
return GetLocalizedString(phrase, _configurationManager.Configuration.UICulture);
}
///
public string GetLocalizedString(string phrase, string culture)
{
if (string.IsNullOrEmpty(culture))
{
culture = _configurationManager.Configuration.UICulture;
}
if (string.IsNullOrEmpty(culture))
{
culture = DefaultCulture;
}
var dictionary = GetLocalizationDictionary(culture);
if (dictionary.TryGetValue(phrase, out var value))
{
return value;
}
return phrase;
}
private Dictionary GetLocalizationDictionary(string culture)
{
if (string.IsNullOrEmpty(culture))
{
throw new ArgumentNullException(nameof(culture));
}
const string Prefix = "Core";
return _dictionaries.GetOrAdd(
culture,
(key, localizationManager) => localizationManager.GetDictionary(Prefix, key, DefaultCulture + ".json").GetAwaiter().GetResult(),
this);
}
private async Task> GetDictionary(string prefix, string culture, string baseFilename)
{
if (string.IsNullOrEmpty(culture))
{
throw new ArgumentNullException(nameof(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);
return dictionary;
}
private async Task CopyInto(IDictionary dictionary, string resourcePath)
{
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)
{
var dict = await JsonSerializer.DeserializeAsync>(stream, _jsonOptions).ConfigureAwait(false);
foreach (var key in dict.Keys)
{
dictionary[key] = dict[key];
}
}
else
{
_logger.LogError("Missing translation/culture resource: {ResourcePath}", resourcePath);
}
}
}
private static string GetResourceFilename(string culture)
{
var parts = culture.Split('-');
if (parts.Length == 2)
{
culture = parts[0].ToLowerInvariant() + "-" + parts[1].ToUpperInvariant();
}
else
{
culture = culture.ToLowerInvariant();
}
return culture + ".json";
}
///
public IEnumerable 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");
}
}
}