2013-06-10 17:46:11 +00:00
|
|
|
using System;
|
2013-06-11 02:34:55 +00:00
|
|
|
using System.Collections.Concurrent;
|
2013-06-06 14:33:11 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
2013-06-10 17:46:11 +00:00
|
|
|
using System.IO;
|
2013-06-06 14:33:11 +00:00
|
|
|
using System.Linq;
|
2019-01-13 19:22:00 +00:00
|
|
|
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;
|
2016-11-05 02:17:18 +00:00
|
|
|
using MediaBrowser.Model.Reflection;
|
2019-01-13 19:22:00 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2013-06-06 14:33:11 +00:00
|
|
|
|
2016-11-05 02:17:18 +00:00
|
|
|
namespace Emby.Server.Implementations.Localization
|
2013-06-06 14:33:11 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class LocalizationManager
|
|
|
|
/// </summary>
|
|
|
|
public class LocalizationManager : ILocalizationManager
|
|
|
|
{
|
2013-06-10 17:46:11 +00:00
|
|
|
/// <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
|
|
|
|
2013-10-31 14:03:23 +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;
|
2016-11-05 02:17:18 +00:00
|
|
|
private readonly IAssemblyInfo _assemblyInfo;
|
|
|
|
private readonly ITextLocalizer _textLocalizer;
|
2014-03-31 01:00:47 +00:00
|
|
|
|
2013-06-10 17:46:11 +00:00
|
|
|
/// <summary>
|
2014-06-05 02:32:40 +00:00
|
|
|
/// Initializes a new instance of the <see cref="LocalizationManager" /> class.
|
2013-06-10 17:46:11 +00:00
|
|
|
/// </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>
|
2019-01-17 22:55:05 +00:00
|
|
|
public LocalizationManager(
|
|
|
|
IServerConfigurationManager configurationManager,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IJsonSerializer jsonSerializer,
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
IAssemblyInfo assemblyInfo,
|
|
|
|
ITextLocalizer textLocalizer)
|
2013-06-10 17:46:11 +00:00
|
|
|
{
|
|
|
|
_configurationManager = configurationManager;
|
2013-10-31 14:03:23 +00:00
|
|
|
_fileSystem = fileSystem;
|
2014-03-31 01:00:47 +00:00
|
|
|
_jsonSerializer = jsonSerializer;
|
2019-01-17 22:55:05 +00:00
|
|
|
_logger = loggerFactory.CreateLogger(nameof(LocalizationManager));
|
2016-11-05 02:17:18 +00:00
|
|
|
_assemblyInfo = assemblyInfo;
|
|
|
|
_textLocalizer = textLocalizer;
|
2013-06-18 20:54:32 +00:00
|
|
|
|
|
|
|
ExtractAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ExtractAll()
|
|
|
|
{
|
|
|
|
var type = GetType();
|
|
|
|
var resourcePath = type.Namespace + ".Ratings.";
|
|
|
|
|
|
|
|
var localizationPath = LocalizationPath;
|
|
|
|
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(localizationPath);
|
2013-06-18 20:54:32 +00:00
|
|
|
|
2016-11-05 02:17:18 +00:00
|
|
|
var existingFiles = GetRatingsFiles(localizationPath)
|
2013-06-21 13:42:27 +00:00
|
|
|
.Select(Path.GetFileName)
|
|
|
|
.ToList();
|
|
|
|
|
2013-06-18 20:54:32 +00:00
|
|
|
// Extract from the assembly
|
2016-11-05 02:17:18 +00:00
|
|
|
foreach (var resource in _assemblyInfo
|
|
|
|
.GetManifestResourceNames(type)
|
2013-06-18 20:54:32 +00:00
|
|
|
.Where(i => i.StartsWith(resourcePath)))
|
|
|
|
{
|
|
|
|
var filename = "ratings-" + resource.Substring(resourcePath.Length);
|
|
|
|
|
|
|
|
if (!existingFiles.Contains(filename))
|
|
|
|
{
|
2016-11-05 02:17:18 +00:00
|
|
|
using (var stream = _assemblyInfo.GetManifestResourceStream(type, resource))
|
2013-06-18 20:54:32 +00:00
|
|
|
{
|
2016-06-30 23:17:49 +00:00
|
|
|
var target = Path.Combine(localizationPath, filename);
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogInformation("Extracting ratings to {0}", target);
|
2016-06-30 23:17:49 +00:00
|
|
|
|
2016-10-25 19:02:04 +00:00
|
|
|
using (var fs = _fileSystem.GetFileStream(target, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
|
2013-06-18 20:54:32 +00:00
|
|
|
{
|
|
|
|
stream.CopyTo(fs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-05 21:51:23 +00:00
|
|
|
|
2016-11-05 02:17:18 +00:00
|
|
|
foreach (var file in GetRatingsFiles(localizationPath))
|
2013-06-18 20:54:32 +00:00
|
|
|
{
|
|
|
|
LoadRatings(file);
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
LoadAdditionalRatings();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadAdditionalRatings()
|
|
|
|
{
|
|
|
|
LoadRatings("au", new[] {
|
|
|
|
|
|
|
|
new ParentalRating("AU-G", 1),
|
|
|
|
new ParentalRating("AU-PG", 5),
|
|
|
|
new ParentalRating("AU-M", 6),
|
|
|
|
new ParentalRating("AU-MA15+", 7),
|
|
|
|
new ParentalRating("AU-M15+", 8),
|
|
|
|
new ParentalRating("AU-R18+", 9),
|
|
|
|
new ParentalRating("AU-X18+", 10),
|
|
|
|
new ParentalRating("AU-RC", 11)
|
|
|
|
});
|
|
|
|
|
|
|
|
LoadRatings("be", new[] {
|
|
|
|
|
|
|
|
new ParentalRating("BE-AL", 1),
|
|
|
|
new ParentalRating("BE-MG6", 2),
|
|
|
|
new ParentalRating("BE-6", 3),
|
|
|
|
new ParentalRating("BE-9", 5),
|
|
|
|
new ParentalRating("BE-12", 6),
|
|
|
|
new ParentalRating("BE-16", 8)
|
|
|
|
});
|
|
|
|
|
|
|
|
LoadRatings("de", new[] {
|
|
|
|
|
|
|
|
new ParentalRating("DE-0", 1),
|
|
|
|
new ParentalRating("FSK-0", 1),
|
|
|
|
new ParentalRating("DE-6", 5),
|
|
|
|
new ParentalRating("FSK-6", 5),
|
|
|
|
new ParentalRating("DE-12", 7),
|
|
|
|
new ParentalRating("FSK-12", 7),
|
|
|
|
new ParentalRating("DE-16", 8),
|
|
|
|
new ParentalRating("FSK-16", 8),
|
|
|
|
new ParentalRating("DE-18", 9),
|
|
|
|
new ParentalRating("FSK-18", 9)
|
|
|
|
});
|
|
|
|
|
2019-01-13 19:22:00 +00:00
|
|
|
LoadRatings("ru", new[] {
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
new ParentalRating("RU-0+", 1),
|
|
|
|
new ParentalRating("RU-6+", 3),
|
|
|
|
new ParentalRating("RU-12+", 7),
|
|
|
|
new ParentalRating("RU-16+", 9),
|
|
|
|
new ParentalRating("RU-18+", 10)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadRatings(string country, ParentalRating[] ratings)
|
|
|
|
{
|
|
|
|
_allParentalRatings[country] = ratings.ToDictionary(i => i.Name);
|
2013-06-10 17:46:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 02:17:18 +00:00
|
|
|
private List<string> GetRatingsFiles(string directory)
|
|
|
|
{
|
|
|
|
return _fileSystem.GetFilePaths(directory, false)
|
|
|
|
.Where(i => string.Equals(Path.GetExtension(i), ".txt", StringComparison.OrdinalIgnoreCase))
|
|
|
|
.Where(i => Path.GetFileName(i).StartsWith("ratings-", StringComparison.OrdinalIgnoreCase))
|
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
|
2013-06-10 17:46:11 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the localization path.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The localization path.</value>
|
2019-01-06 20:50:43 +00:00
|
|
|
public string LocalizationPath => Path.Combine(_configurationManager.ApplicationPaths.ProgramDataPath, "localization");
|
2013-06-10 17:46:11 +00:00
|
|
|
|
2016-10-25 19:02:04 +00:00
|
|
|
public string RemoveDiacritics(string text)
|
|
|
|
{
|
2016-11-05 02:17:18 +00:00
|
|
|
return _textLocalizer.RemoveDiacritics(text);
|
2016-10-25 19:02:04 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 07:58:33 +00:00
|
|
|
public string NormalizeFormKD(string text)
|
|
|
|
{
|
2016-11-05 02:17:18 +00:00
|
|
|
return _textLocalizer.NormalizeFormKD(text);
|
2016-10-27 07:58:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 21:51:23 +00:00
|
|
|
private CultureDto[] _cultures;
|
|
|
|
|
2013-06-06 14:33:11 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the cultures.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>IEnumerable{CultureDto}.</returns>
|
2017-08-19 19:43:35 +00:00
|
|
|
public CultureDto[] GetCultures()
|
2013-06-06 14:33:11 +00:00
|
|
|
{
|
2017-11-05 21:51:23 +00:00
|
|
|
var result = _cultures;
|
|
|
|
if (result != null)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-05-07 02:28:19 +00:00
|
|
|
var type = GetType();
|
2014-06-18 15:12:20 +00:00
|
|
|
var path = type.Namespace + ".iso6392.txt";
|
|
|
|
|
|
|
|
var list = new List<CultureDto>();
|
2014-05-07 02:28:19 +00:00
|
|
|
|
2016-11-05 02:17:18 +00:00
|
|
|
using (var stream = _assemblyInfo.GetManifestResourceStream(type, path))
|
2014-05-07 02:28:19 +00:00
|
|
|
{
|
2014-06-18 15:12:20 +00:00
|
|
|
using (var reader = new StreamReader(stream))
|
|
|
|
{
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
{
|
|
|
|
var line = reader.ReadLine();
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(line))
|
|
|
|
{
|
|
|
|
var parts = line.Split('|');
|
|
|
|
|
|
|
|
if (parts.Length == 5)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var threeletterNames = new List<string> { parts[0] };
|
|
|
|
if (!string.IsNullOrWhiteSpace(parts[1]))
|
|
|
|
{
|
|
|
|
threeletterNames.Add(parts[1]);
|
|
|
|
}
|
|
|
|
|
2014-06-18 15:12:20 +00:00
|
|
|
list.Add(new CultureDto
|
|
|
|
{
|
|
|
|
DisplayName = parts[3],
|
|
|
|
Name = parts[3],
|
2018-09-12 17:26:21 +00:00
|
|
|
ThreeLetterISOLanguageNames = threeletterNames.ToArray(),
|
2014-06-18 15:12:20 +00:00
|
|
|
TwoLetterISOLanguageName = parts[2]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 02:28:19 +00:00
|
|
|
}
|
2014-06-18 15:12:20 +00:00
|
|
|
|
2017-11-05 21:51:23 +00:00
|
|
|
result = list.Where(i => !string.IsNullOrWhiteSpace(i.Name) &&
|
|
|
|
!string.IsNullOrWhiteSpace(i.DisplayName) &&
|
2018-09-12 17:26:21 +00:00
|
|
|
i.ThreeLetterISOLanguageNames.Length > 0 &&
|
2017-11-05 21:51:23 +00:00
|
|
|
!string.IsNullOrWhiteSpace(i.TwoLetterISOLanguageName)).ToArray();
|
|
|
|
|
|
|
|
_cultures = result;
|
|
|
|
|
|
|
|
return result;
|
2013-06-06 14:33:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public CultureDto FindLanguageInfo(string language)
|
|
|
|
{
|
|
|
|
return 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));
|
|
|
|
}
|
|
|
|
|
2013-06-06 14:33:11 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the countries.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>IEnumerable{CountryInfo}.</returns>
|
2017-08-19 19:43:35 +00:00
|
|
|
public CountryInfo[] GetCountries()
|
2013-06-06 14:33:11 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
// ToDo: DeserializeFromStream seems broken in this case
|
|
|
|
string jsonCountries = "[{\"Name\":\"AF\",\"DisplayName\":\"Afghanistan\",\"TwoLetterISORegionName\":\"AF\",\"ThreeLetterISORegionName\":\"AFG\"},{\"Name\":\"AL\",\"DisplayName\":\"Albania\",\"TwoLetterISORegionName\":\"AL\",\"ThreeLetterISORegionName\":\"ALB\"},{\"Name\":\"DZ\",\"DisplayName\":\"Algeria\",\"TwoLetterISORegionName\":\"DZ\",\"ThreeLetterISORegionName\":\"DZA\"},{\"Name\":\"AR\",\"DisplayName\":\"Argentina\",\"TwoLetterISORegionName\":\"AR\",\"ThreeLetterISORegionName\":\"ARG\"},{\"Name\":\"AM\",\"DisplayName\":\"Armenia\",\"TwoLetterISORegionName\":\"AM\",\"ThreeLetterISORegionName\":\"ARM\"},{\"Name\":\"AU\",\"DisplayName\":\"Australia\",\"TwoLetterISORegionName\":\"AU\",\"ThreeLetterISORegionName\":\"AUS\"},{\"Name\":\"AT\",\"DisplayName\":\"Austria\",\"TwoLetterISORegionName\":\"AT\",\"ThreeLetterISORegionName\":\"AUT\"},{\"Name\":\"AZ\",\"DisplayName\":\"Azerbaijan\",\"TwoLetterISORegionName\":\"AZ\",\"ThreeLetterISORegionName\":\"AZE\"},{\"Name\":\"BH\",\"DisplayName\":\"Bahrain\",\"TwoLetterISORegionName\":\"BH\",\"ThreeLetterISORegionName\":\"BHR\"},{\"Name\":\"BD\",\"DisplayName\":\"Bangladesh\",\"TwoLetterISORegionName\":\"BD\",\"ThreeLetterISORegionName\":\"BGD\"},{\"Name\":\"BY\",\"DisplayName\":\"Belarus\",\"TwoLetterISORegionName\":\"BY\",\"ThreeLetterISORegionName\":\"BLR\"},{\"Name\":\"BE\",\"DisplayName\":\"Belgium\",\"TwoLetterISORegionName\":\"BE\",\"ThreeLetterISORegionName\":\"BEL\"},{\"Name\":\"BZ\",\"DisplayName\":\"Belize\",\"TwoLetterISORegionName\":\"BZ\",\"ThreeLetterISORegionName\":\"BLZ\"},{\"Name\":\"VE\",\"DisplayName\":\"Bolivarian Republic of Venezuela\",\"TwoLetterISORegionName\":\"VE\",\"ThreeLetterISORegionName\":\"VEN\"},{\"Name\":\"BO\",\"DisplayName\":\"Bolivia\",\"TwoLetterISORegionName\":\"BO\",\"ThreeLetterISORegionName\":\"BOL\"},{\"Name\":\"BA\",\"DisplayName\":\"Bosnia and Herzegovina\",\"TwoLetterISORegionName\":\"BA\",\"ThreeLetterISORegionName\":\"BIH\"},{\"Name\":\"BW\",\"DisplayName\":\"Botswana\",\"TwoLetterISORegionName\":\"BW\",\"ThreeLetterISORegionName\":\"BWA\"},{\"Name\":\"BR\",\"DisplayName\":\"Brazil\",\"TwoLetterISORegionName\":\"BR\",\"ThreeLetterISORegionName\":\"BRA\"},{\"Name\":\"BN\",\"DisplayName\":\"Brunei Darussalam\",\"TwoLetterISORegionName\":\"BN\",\"ThreeLetterISORegionName\":\"BRN\"},{\"Name\":\"BG\",\"DisplayName\":\"Bulgaria\",\"TwoLetterISORegionName\":\"BG\",\"ThreeLetterISORegionName\":\"BGR\"},{\"Name\":\"KH\",\"DisplayName\":\"Cambodia\",\"TwoLetterISORegionName\":\"KH\",\"ThreeLetterISORegionName\":\"KHM\"},{\"Name\":\"CM\",\"DisplayName\":\"Cameroon\",\"TwoLetterISORegionName\":\"CM\",\"ThreeLetterISORegionName\":\"CMR\"},{\"Name\":\"CA\",\"DisplayName\":\"Canada\",\"TwoLetterISORegionName\":\"CA\",\"ThreeLetterISORegionName\":\"CAN\"},{\"Name\":\"029\",\"DisplayName\":\"Caribbean\",\"TwoLetterISORegionName\":\"029\",\"ThreeLetterISORegionName\":\"029\"},{\"Name\":\"CL\",\"DisplayName\":\"Chile\",\"TwoLetterISORegionName\":\"CL\",\"ThreeLetterISORegionName\":\"CHL\"},{\"Name\":\"CO\",\"DisplayName\":\"Colombia\",\"TwoLetterISORegionName\":\"CO\",\"ThreeLetterISORegionName\":\"COL\"},{\"Name\":\"CD\",\"DisplayName\":\"Congo [DRC]\",\"TwoLetterISORegionName\":\"CD\",\"ThreeLetterISORegionName\":\"COD\"},{\"Name\":\"CR\",\"DisplayName\":\"Costa Rica\",\"TwoLetterISORegionName\":\"CR\",\"ThreeLetterISORegionName\":\"CRI\"},{\"Name\":\"HR\",\"DisplayName\":\"Croatia\",\"TwoLetterISORegionName\":\"HR\",\"ThreeLetterISORegionName\":\"HRV\"},{\"Name\":\"CZ\",\"DisplayName\":\"Czech Republic\",\"TwoLetterISORegionName\":\"CZ\",\"ThreeLetterISORegionName\":\"CZE\"},{\"Name\":\"DK\",\"DisplayName\":\"Denmark\",\"TwoLetterISORegionName\":\"DK\",\"ThreeLetterISORegionName\":\"DNK\"},{\"Name\":\"DO\",\"DisplayName\":\"Dominican Republic\",\"TwoLetterISORegionName\":\"DO\",\"ThreeLetterISORegionName\":\"DOM\"},{\"Name\":\"EC\",\"DisplayName\":\"Ecuador\",\"TwoLetterISORegionName\":\"EC\",\"ThreeLetterISORegionName\":\"ECU\"},{\"Name\":\"EG\",\"DisplayName\":\"Egypt\",\"TwoLetterISORegionName\":\"EG\",\"ThreeLetterISORegionName\
|
2014-05-07 02:28:19 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return _jsonSerializer.DeserializeFromString<CountryInfo[]>(jsonCountries);
|
2013-06-06 14:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the parental ratings.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>IEnumerable{ParentalRating}.</returns>
|
2017-08-19 19:43:35 +00:00
|
|
|
public ParentalRating[] GetParentalRatings()
|
2013-06-06 14:33:11 +00:00
|
|
|
{
|
2017-08-19 19:43:35 +00:00
|
|
|
return GetParentalRatingsDictionary().Values.ToArray();
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
var ratings = GetRatings(countryCode);
|
|
|
|
|
|
|
|
if (ratings == null)
|
|
|
|
{
|
|
|
|
ratings = GetRatings("us");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ratings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the ratings.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="countryCode">The country code.</param>
|
|
|
|
private Dictionary<string, ParentalRating> GetRatings(string countryCode)
|
|
|
|
{
|
2019-01-13 20:46:33 +00:00
|
|
|
_allParentalRatings.TryGetValue(countryCode, out var value);
|
2013-06-11 02:34:55 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Loads the ratings.
|
|
|
|
/// </summary>
|
2013-06-18 20:54:32 +00:00
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
/// <returns>Dictionary{System.StringParentalRating}.</returns>
|
|
|
|
private void LoadRatings(string file)
|
2013-06-11 02:34:55 +00:00
|
|
|
{
|
2019-01-26 21:31:59 +00:00
|
|
|
var dict = File.ReadAllLines(file).Select(i =>
|
2013-06-10 17:46:11 +00:00
|
|
|
{
|
|
|
|
if (!string.IsNullOrWhiteSpace(i))
|
|
|
|
{
|
|
|
|
var parts = i.Split(',');
|
|
|
|
|
|
|
|
if (parts.Length == 2)
|
|
|
|
{
|
2019-01-13 20:46:33 +00:00
|
|
|
if (int.TryParse(parts[1], NumberStyles.Integer, UsCulture, out var value))
|
2013-06-10 17:46:11 +00:00
|
|
|
{
|
|
|
|
return new ParentalRating { Name = parts[0], Value = value };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
})
|
|
|
|
.Where(i => i != null)
|
2013-09-11 17:55:12 +00:00
|
|
|
.ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
|
2013-06-10 17:46:11 +00:00
|
|
|
|
2019-01-26 20:47:11 +00:00
|
|
|
var countryCode = Path.GetFileNameWithoutExtension(file)
|
2014-07-26 17:30:15 +00:00
|
|
|
.Split('-')
|
|
|
|
.Last();
|
2013-06-10 17:46:11 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
_allParentalRatings[countryCode] = dict;
|
2013-06-10 17:46:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 21:51:23 +00:00
|
|
|
private readonly string[] _unratedValues = { "n/a", "unrated", "not rated" };
|
2015-11-06 15:02:22 +00:00
|
|
|
|
2013-06-10 17:46:11 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the rating level.
|
|
|
|
/// </summary>
|
|
|
|
public int? GetRatingLevel(string rating)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(rating))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(rating));
|
2013-06-10 17:46:11 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2013-06-10 17:46:11 +00:00
|
|
|
|
2019-01-17 17:47:41 +00:00
|
|
|
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
|
|
|
}
|
2013-06-10 17:46:11 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// TODO: Further improve by normalizing out all spaces and dashes
|
|
|
|
return null;
|
2013-06-10 17:46:11 +00:00
|
|
|
}
|
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);
|
|
|
|
|
2019-01-13 20:46:33 +00:00
|
|
|
if (dictionary.TryGetValue(phrase, out var value))
|
2014-03-31 01:00:47 +00:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return phrase;
|
|
|
|
}
|
|
|
|
|
2017-10-21 16:39:52 +00:00
|
|
|
const string DefaultCulture = "en-US";
|
|
|
|
|
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
|
|
|
{
|
2019-01-06 20:50:43 +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;
|
|
|
|
|
2017-10-21 16:39:52 +00:00
|
|
|
return _dictionaries.GetOrAdd(key, k => GetDictionary(prefix, culture, DefaultCulture + ".json"));
|
2014-03-31 01:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Dictionary<string, string> GetDictionary(string prefix, string culture, string baseFilename)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (string.IsNullOrEmpty(culture))
|
2017-10-21 16:39:52 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +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;
|
|
|
|
|
2016-11-05 02:17:18 +00:00
|
|
|
CopyInto(dictionary, namespaceName + "." + baseFilename);
|
|
|
|
CopyInto(dictionary, namespaceName + "." + GetResourceFilename(culture));
|
2014-03-31 01:00:47 +00:00
|
|
|
|
|
|
|
return dictionary;
|
|
|
|
}
|
|
|
|
|
2016-11-05 02:17:18 +00:00
|
|
|
private void CopyInto(IDictionary<string, string> dictionary, string resourcePath)
|
2014-03-31 01:00:47 +00:00
|
|
|
{
|
2016-11-05 02:17:18 +00:00
|
|
|
using (var stream = _assemblyInfo.GetManifestResourceStream(GetType(), resourcePath))
|
2014-03-31 01:00:47 +00:00
|
|
|
{
|
|
|
|
if (stream != null)
|
|
|
|
{
|
|
|
|
var dict = _jsonSerializer.DeserializeFromStream<Dictionary<string, string>>(stream);
|
|
|
|
|
|
|
|
foreach (var key in dict.Keys)
|
|
|
|
{
|
|
|
|
dictionary[key] = dict[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +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("Belarusian (Belarus)", "be-BY"),
|
|
|
|
new LocalizationOption("Bulgarian (Bulgaria)", "bg-BG"),
|
|
|
|
new LocalizationOption("Catalan", "ca"),
|
|
|
|
new LocalizationOption("Chinese Simplified", "zh-CN"),
|
|
|
|
new LocalizationOption("Chinese Traditional", "zh-TW"),
|
|
|
|
new LocalizationOption("Chinese Traditional (Hong Kong)", "zh-HK"),
|
|
|
|
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("Finnish", "fi"),
|
|
|
|
new LocalizationOption("French", "fr"),
|
|
|
|
new LocalizationOption("French (Canada)", "fr-CA"),
|
|
|
|
new LocalizationOption("German", "de"),
|
|
|
|
new LocalizationOption("Greek", "el"),
|
|
|
|
new LocalizationOption("Hebrew", "he"),
|
|
|
|
new LocalizationOption("Hindi (India)", "hi-IN"),
|
|
|
|
new LocalizationOption("Hungarian", "hu"),
|
|
|
|
new LocalizationOption("Indonesian", "id"),
|
|
|
|
new LocalizationOption("Italian", "it"),
|
|
|
|
new LocalizationOption("Japanese", "ja"),
|
|
|
|
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("Romanian", "ro"),
|
|
|
|
new LocalizationOption("Russian", "ru"),
|
|
|
|
new LocalizationOption("Slovak", "sk"),
|
|
|
|
new LocalizationOption("Slovenian (Slovenia)", "sl-SI"),
|
|
|
|
new LocalizationOption("Spanish", "es"),
|
|
|
|
new LocalizationOption("Spanish (Latin America)", "es-419"),
|
|
|
|
new LocalizationOption("Spanish (Mexico)", "es-MX"),
|
|
|
|
new LocalizationOption("Swedish", "sv"),
|
|
|
|
new LocalizationOption("Swiss German", "gsw"),
|
|
|
|
new LocalizationOption("Turkish", "tr"),
|
|
|
|
new LocalizationOption("Ukrainian", "uk"),
|
|
|
|
new LocalizationOption("Vietnamese", "vi")
|
2017-08-19 19:43:35 +00:00
|
|
|
};
|
2013-06-06 14:33:11 +00:00
|
|
|
}
|
2016-11-05 02:17:18 +00:00
|
|
|
|
|
|
|
public interface ITextLocalizer
|
|
|
|
{
|
|
|
|
string RemoveDiacritics(string text);
|
|
|
|
|
|
|
|
string NormalizeFormKD(string text);
|
|
|
|
}
|
2013-06-06 14:33:11 +00:00
|
|
|
}
|