jellyfin-server/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs

366 lines
13 KiB
C#
Raw Normal View History

2014-03-31 21:04:22 +00:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Localization;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
2014-03-31 01:00:47 +00:00
using MediaBrowser.Model.Serialization;
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;
2014-03-31 01:00:47 +00:00
using System.Reflection;
namespace MediaBrowser.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");
2013-06-11 02:34:55 +00:00
private readonly ConcurrentDictionary<string, Dictionary<string, ParentalRating>> _allParentalRatings =
new ConcurrentDictionary<string, Dictionary<string, ParentalRating>>(StringComparer.OrdinalIgnoreCase);
private readonly IFileSystem _fileSystem;
2014-03-31 01:00:47 +00:00
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
/// Initializes a new instance of the <see cref="LocalizationManager"/> class.
/// </summary>
/// <param name="configurationManager">The configuration manager.</param>
2014-03-31 01:00:47 +00:00
public LocalizationManager(IServerConfigurationManager configurationManager, IFileSystem fileSystem, IJsonSerializer jsonSerializer)
{
_configurationManager = configurationManager;
_fileSystem = fileSystem;
2014-03-31 01:00:47 +00:00
_jsonSerializer = jsonSerializer;
2013-06-18 20:54:32 +00:00
ExtractAll();
}
private void ExtractAll()
{
var type = GetType();
var resourcePath = type.Namespace + ".Ratings.";
var localizationPath = LocalizationPath;
Directory.CreateDirectory(localizationPath);
2013-06-18 20:54:32 +00:00
2013-06-21 13:42:27 +00:00
var existingFiles = Directory.EnumerateFiles(localizationPath, "ratings-*.txt", SearchOption.TopDirectoryOnly)
.Select(Path.GetFileName)
.ToList();
2013-06-18 20:54:32 +00:00
// Extract from the assembly
foreach (var resource in type.Assembly
.GetManifestResourceNames()
.Where(i => i.StartsWith(resourcePath)))
{
var filename = "ratings-" + resource.Substring(resourcePath.Length);
if (!existingFiles.Contains(filename))
{
using (var stream = type.Assembly.GetManifestResourceStream(resource))
{
using (var fs = _fileSystem.GetFileStream(Path.Combine(localizationPath, filename), FileMode.Create, FileAccess.Write, FileShare.Read))
2013-06-18 20:54:32 +00:00
{
stream.CopyTo(fs);
}
}
}
}
foreach (var file in Directory.EnumerateFiles(localizationPath, "ratings-*.txt", SearchOption.TopDirectoryOnly))
{
LoadRatings(file);
}
}
/// <summary>
/// Gets the localization path.
/// </summary>
/// <value>The localization path.</value>
public string LocalizationPath
{
get
{
return Path.Combine(_configurationManager.ApplicationPaths.ProgramDataPath, "localization");
}
}
/// <summary>
/// Gets the cultures.
/// </summary>
/// <returns>IEnumerable{CultureDto}.</returns>
public IEnumerable<CultureDto> GetCultures()
{
2014-05-07 02:28:19 +00:00
var type = GetType();
var path = type.Namespace + ".cultures.json";
using (var stream = type.Assembly.GetManifestResourceStream(path))
{
return _jsonSerializer.DeserializeFromStream<List<CultureDto>>(stream);
}
}
/// <summary>
/// Gets the countries.
/// </summary>
/// <returns>IEnumerable{CountryInfo}.</returns>
public IEnumerable<CountryInfo> GetCountries()
{
2014-05-07 02:28:19 +00:00
var type = GetType();
var path = type.Namespace + ".countries.json";
using (var stream = type.Assembly.GetManifestResourceStream(path))
{
return _jsonSerializer.DeserializeFromStream<List<CountryInfo>>(stream);
}
}
/// <summary>
/// Gets the parental ratings.
/// </summary>
/// <returns>IEnumerable{ParentalRating}.</returns>
public IEnumerable<ParentalRating> GetParentalRatings()
{
2013-06-11 02:34:55 +00:00
return GetParentalRatingsDictionary().Values.ToList();
}
/// <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)
{
Dictionary<string, ParentalRating> value;
2013-06-18 20:54:32 +00:00
_allParentalRatings.TryGetValue(countryCode, out 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
{
2013-06-18 20:54:32 +00:00
var dict = File.ReadAllLines(file).Select(i =>
{
if (!string.IsNullOrWhiteSpace(i))
{
var parts = i.Split(',');
if (parts.Length == 2)
{
int value;
if (int.TryParse(parts[1], NumberStyles.Integer, UsCulture, out value))
{
return new ParentalRating { Name = parts[0], Value = value };
}
}
}
return null;
})
.Where(i => i != null)
.ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
2013-06-18 20:54:32 +00:00
var countryCode = Path.GetFileNameWithoutExtension(file).Split('-').Last();
2013-06-18 20:54:32 +00:00
_allParentalRatings.TryAdd(countryCode, dict);
}
/// <summary>
/// Gets the rating level.
/// </summary>
public int? GetRatingLevel(string rating)
{
if (string.IsNullOrEmpty(rating))
{
throw new ArgumentNullException("rating");
}
2013-06-11 02:34:55 +00:00
var ratingsDictionary = GetParentalRatingsDictionary();
2013-06-11 02:34:55 +00:00
ParentalRating value;
2013-06-11 02:34:55 +00:00
if (!ratingsDictionary.TryGetValue(rating, out value))
{
2013-06-18 20:54:32 +00:00
// 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;
}
}
2013-06-11 02:34:55 +00:00
}
2013-06-11 02:34:55 +00:00
return value == null ? (int?)null : value.Value;
}
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)
{
var dictionary = GetLocalizationDictionary(culture);
string value;
if (dictionary.TryGetValue(phrase, out value))
{
return value;
}
return phrase;
}
private readonly ConcurrentDictionary<string, Dictionary<string, string>> _dictionaries =
new ConcurrentDictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, string> GetLocalizationDictionary(string culture)
{
const string prefix = "Server";
var key = prefix + culture;
return _dictionaries.GetOrAdd(key, k => GetDictionary(prefix, culture, "server.json"));
}
public Dictionary<string, string> GetJavaScriptLocalizationDictionary(string culture)
{
const string prefix = "JavaScript";
var key = prefix + culture;
return _dictionaries.GetOrAdd(key, k => GetDictionary(prefix, culture, "javascript.json"));
}
private Dictionary<string, string> GetDictionary(string prefix, string culture, string baseFilename)
{
var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var assembly = GetType().Assembly;
var namespaceName = GetType().Namespace + "." + prefix;
CopyInto(dictionary, namespaceName + "." + baseFilename, assembly);
CopyInto(dictionary, namespaceName + "." + GetResourceFilename(culture), assembly);
return dictionary;
}
private void CopyInto(IDictionary<string, string> dictionary, string resourcePath, Assembly assembly)
{
using (var stream = assembly.GetManifestResourceStream(resourcePath))
{
if (stream != null)
{
var dict = _jsonSerializer.DeserializeFromStream<Dictionary<string, string>>(stream);
foreach (var key in dict.Keys)
{
dictionary[key] = dict[key];
}
}
}
}
private string GetResourceFilename(string culture)
{
var parts = culture.Split('-');
if (parts.Length == 2)
{
culture = parts[0].ToLower() + "_" + parts[1].ToUpper();
}
else
{
culture = culture.ToLower();
}
return culture + ".json";
}
public IEnumerable<LocalizatonOption> GetLocalizationOptions()
{
return new List<LocalizatonOption>
{
2014-04-05 15:02:50 +00:00
new LocalizatonOption{ Name="Arabic", Value="ar"},
2014-04-06 17:53:23 +00:00
new LocalizatonOption{ Name="English (United Kingdom)", Value="en-GB"},
2014-03-31 02:33:10 +00:00
new LocalizatonOption{ Name="English (United States)", Value="en-us"},
2014-04-09 02:12:17 +00:00
new LocalizatonOption{ Name="Catalan", Value="ca"},
2014-03-31 21:04:22 +00:00
new LocalizatonOption{ Name="Chinese Traditional", Value="zh-TW"},
2014-04-09 02:12:17 +00:00
new LocalizatonOption{ Name="Czech", Value="cs"},
2014-05-09 19:43:06 +00:00
new LocalizatonOption{ Name="Danish", Value="da"},
2014-03-31 21:04:22 +00:00
new LocalizatonOption{ Name="Dutch", Value="nl"},
new LocalizatonOption{ Name="French", Value="fr"},
2014-03-31 02:33:10 +00:00
new LocalizatonOption{ Name="German", Value="de"},
2014-04-05 15:02:50 +00:00
new LocalizatonOption{ Name="Greek", Value="el"},
2014-04-01 22:23:07 +00:00
new LocalizatonOption{ Name="Hebrew", Value="he"},
new LocalizatonOption{ Name="Italian", Value="it"},
2014-04-18 17:16:25 +00:00
new LocalizatonOption{ Name="Kazakh", Value="kk"},
new LocalizatonOption{ Name="Norwegian Bokmål", Value="nb"},
2014-03-31 21:04:22 +00:00
new LocalizatonOption{ Name="Portuguese (Brazil)", Value="pt-BR"},
2014-03-31 02:33:10 +00:00
new LocalizatonOption{ Name="Portuguese (Portugal)", Value="pt-PT"},
2014-03-31 21:04:22 +00:00
new LocalizatonOption{ Name="Russian", Value="ru"},
2014-04-02 21:55:19 +00:00
new LocalizatonOption{ Name="Spanish", Value="es"},
2014-04-08 04:17:18 +00:00
new LocalizatonOption{ Name="Spanish (Mexico)", Value="es-MX"},
new LocalizatonOption{ Name="Swedish", Value="sv"}
2014-03-31 02:33:10 +00:00
}.OrderBy(i => i.Name);
2014-03-31 01:00:47 +00:00
}
public string LocalizeDocument(string document, string culture, Func<string, string> tokenBuilder)
2014-03-31 01:00:47 +00:00
{
foreach (var pair in GetLocalizationDictionary(culture).ToList())
{
var token = tokenBuilder(pair.Key);
document = document.Replace(token, pair.Value, StringComparison.Ordinal);
}
return document;
}
}
}