Merge pull request #732 from Bond-009/locale

Reworked LocalizationManager to load data async
This commit is contained in:
Joshua M. Boniface 2019-02-03 22:08:20 -05:00 committed by GitHub
commit 56dcc45dc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 202 additions and 267 deletions

View File

@ -110,7 +110,6 @@ using MediaBrowser.XbmcMetadata.Providers;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using ServiceStack; using ServiceStack;
using ServiceStack.Text.Jsv; using ServiceStack.Text.Jsv;
using StringExtensions = MediaBrowser.Controller.Extensions.StringExtensions;
using X509Certificate = System.Security.Cryptography.X509Certificates.X509Certificate; using X509Certificate = System.Security.Cryptography.X509Certificates.X509Certificate;
namespace Emby.Server.Implementations namespace Emby.Server.Implementations
@ -302,7 +301,7 @@ namespace Emby.Server.Implementations
private ILiveTvManager LiveTvManager { get; set; } private ILiveTvManager LiveTvManager { get; set; }
public ILocalizationManager LocalizationManager { get; set; } public LocalizationManager LocalizationManager { get; set; }
private IEncodingManager EncodingManager { get; set; } private IEncodingManager EncodingManager { get; set; }
private IChannelManager ChannelManager { get; set; } private IChannelManager ChannelManager { get; set; }
@ -703,7 +702,7 @@ namespace Emby.Server.Implementations
} }
} }
public void Init() public async Task Init()
{ {
HttpPort = ServerConfigurationManager.Configuration.HttpServerPortNumber; HttpPort = ServerConfigurationManager.Configuration.HttpServerPortNumber;
HttpsPort = ServerConfigurationManager.Configuration.HttpsPortNumber; HttpsPort = ServerConfigurationManager.Configuration.HttpsPortNumber;
@ -733,7 +732,7 @@ namespace Emby.Server.Implementations
SetHttpLimit(); SetHttpLimit();
RegisterResources(); await RegisterResources();
FindParts(); FindParts();
} }
@ -748,7 +747,7 @@ namespace Emby.Server.Implementations
/// <summary> /// <summary>
/// Registers resources that classes will depend on /// Registers resources that classes will depend on
/// </summary> /// </summary>
protected void RegisterResources() protected async Task RegisterResources()
{ {
RegisterSingleInstance(ConfigurationManager); RegisterSingleInstance(ConfigurationManager);
RegisterSingleInstance<IApplicationHost>(this); RegisterSingleInstance<IApplicationHost>(this);
@ -809,9 +808,9 @@ namespace Emby.Server.Implementations
IAssemblyInfo assemblyInfo = new AssemblyInfo(); IAssemblyInfo assemblyInfo = new AssemblyInfo();
RegisterSingleInstance(assemblyInfo); RegisterSingleInstance(assemblyInfo);
LocalizationManager = new LocalizationManager(ServerConfigurationManager, FileSystemManager, JsonSerializer, LoggerFactory, assemblyInfo, new TextLocalizer()); LocalizationManager = new LocalizationManager(ServerConfigurationManager, FileSystemManager, JsonSerializer, LoggerFactory);
StringExtensions.LocalizationManager = LocalizationManager; await LocalizationManager.LoadAll();
RegisterSingleInstance(LocalizationManager); RegisterSingleInstance<ILocalizationManager>(LocalizationManager);
BlurayExaminer = new BdInfoExaminer(FileSystemManager); BlurayExaminer = new BdInfoExaminer(FileSystemManager);
RegisterSingleInstance(BlurayExaminer); RegisterSingleInstance(BlurayExaminer);

View File

@ -322,18 +322,18 @@ namespace Emby.Server.Implementations.Library
private string[] NormalizeLanguage(string language) private string[] NormalizeLanguage(string language)
{ {
if (language != null) if (language == null)
{ {
var culture = _localizationManager.FindLanguageInfo(language); return Array.Empty<string>();
if (culture != null)
{
return culture.ThreeLetterISOLanguageNames;
}
return new string[] { language };
} }
return Array.Empty<string>(); var culture = _localizationManager.FindLanguageInfo(language);
if (culture != null)
{
return culture.ThreeLetterISOLanguageNames;
}
return new string[] { language };
} }
private void SetDefaultSubtitleStreamIndex(MediaSourceInfo source, UserItemData userData, User user, bool allowRememberingSelection) private void SetDefaultSubtitleStreamIndex(MediaSourceInfo source, UserItemData userData, User user, bool allowRememberingSelection)

File diff suppressed because one or more lines are too long

View File

@ -1,63 +0,0 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Emby.Server.Implementations.Localization
{
public class TextLocalizer : ITextLocalizer
{
public string RemoveDiacritics(string text)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
var chars = Normalize(text, NormalizationForm.FormD)
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark);
return Normalize(string.Concat(chars), NormalizationForm.FormC);
}
private static string Normalize(string text, NormalizationForm form, bool stripStringOnFailure = true)
{
if (stripStringOnFailure)
{
try
{
return text.Normalize(form);
}
catch (ArgumentException)
{
// will throw if input contains invalid unicode chars
// https://mnaoumov.wordpress.com/2014/06/14/stripping-invalid-characters-from-utf-16-strings/
text = StripInvalidUnicodeCharacters(text);
return Normalize(text, form, false);
}
}
try
{
return text.Normalize(form);
}
catch (ArgumentException)
{
// if it still fails, return the original text
return text;
}
}
private static string StripInvalidUnicodeCharacters(string str)
{
var invalidCharactersRegex = new Regex("([\ud800-\udbff](?![\udc00-\udfff]))|((?<![\ud800-\udbff])[\udc00-\udfff])");
return invalidCharactersRegex.Replace(str, "");
}
public string NormalizeFormKD(string text)
{
return text.Normalize(NormalizationForm.FormKD);
}
}
}

View File

@ -114,7 +114,7 @@ namespace Jellyfin.Server
new NullImageEncoder(), new NullImageEncoder(),
new NetworkManager(_loggerFactory, environmentInfo))) new NetworkManager(_loggerFactory, environmentInfo)))
{ {
appHost.Init(); await appHost.Init();
appHost.ImageProcessor.ImageEncoder = GetImageEncoder(fileSystem, appPaths, appHost.LocalizationManager); appHost.ImageProcessor.ImageEncoder = GetImageEncoder(fileSystem, appPaths, appHost.LocalizationManager);

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Entities.Audio;
@ -60,15 +61,15 @@ namespace MediaBrowser.Api
_fileSystem = fileSystem; _fileSystem = fileSystem;
} }
public object Get(GetMetadataEditorInfo request) public async Task<object> Get(GetMetadataEditorInfo request)
{ {
var item = _libraryManager.GetItemById(request.ItemId); var item = _libraryManager.GetItemById(request.ItemId);
var info = new MetadataEditorInfo var info = new MetadataEditorInfo
{ {
ParentalRatingOptions = _localizationManager.GetParentalRatings(), ParentalRatingOptions = _localizationManager.GetParentalRatings().ToArray(),
ExternalIdInfos = _providerManager.GetExternalIdInfos(item).ToArray(), ExternalIdInfos = _providerManager.GetExternalIdInfos(item).ToArray(),
Countries = _localizationManager.GetCountries(), Countries = await _localizationManager.GetCountries(),
Cultures = _localizationManager.GetCultures() Cultures = _localizationManager.GetCultures()
}; };

View File

@ -89,7 +89,7 @@ namespace MediaBrowser.Common
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="manageLiftime">if set to <c>true</c> [manage liftime].</param> /// <param name="manageLiftime">if set to <c>true</c> [manage liftime].</param>
/// <returns>IEnumerable{``0}.</returns> /// <returns>IEnumerable{``0}.</returns>
IEnumerable<T> GetExports<T>(bool manageLiftime = true); IEnumerable<T> GetExports<T>(bool manageLifetime = true);
/// <summary> /// <summary>
/// Updates the application. /// Updates the application.
@ -131,7 +131,7 @@ namespace MediaBrowser.Common
/// <summary> /// <summary>
/// Inits this instance. /// Inits this instance.
/// </summary> /// </summary>
void Init(); Task Init();
/// <summary> /// <summary>
/// Creates the instance. /// Creates the instance.

View File

@ -1,4 +1,8 @@
using MediaBrowser.Model.Globalization; using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace MediaBrowser.Controller.Extensions namespace MediaBrowser.Controller.Extensions
{ {
@ -7,11 +11,45 @@ namespace MediaBrowser.Controller.Extensions
/// </summary> /// </summary>
public static class StringExtensions public static class StringExtensions
{ {
public static ILocalizationManager LocalizationManager { get; set; }
public static string RemoveDiacritics(this string text) public static string RemoveDiacritics(this string text)
{ {
return LocalizationManager.RemoveDiacritics(text); if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
var chars = Normalize(text, NormalizationForm.FormD)
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark);
return Normalize(string.Concat(chars), NormalizationForm.FormC);
}
private static string Normalize(string text, NormalizationForm form, bool stripStringOnFailure = true)
{
if (stripStringOnFailure)
{
try
{
return text.Normalize(form);
}
catch (ArgumentException)
{
// will throw if input contains invalid unicode chars
// https://mnaoumov.wordpress.com/2014/06/14/stripping-invalid-characters-from-utf-16-strings/
text = Regex.Replace(text, "([\ud800-\udbff](?![\udc00-\udfff]))|((?<![\ud800-\udbff])[\udc00-\udfff])", "");
return Normalize(text, form, false);
}
}
try
{
return text.Normalize(form);
}
catch (ArgumentException)
{
// if it still fails, return the original text
return text;
}
} }
} }
} }

View File

@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Threading.Tasks;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
namespace MediaBrowser.Model.Globalization namespace MediaBrowser.Model.Globalization
@ -17,12 +19,12 @@ namespace MediaBrowser.Model.Globalization
/// Gets the countries. /// Gets the countries.
/// </summary> /// </summary>
/// <returns>IEnumerable{CountryInfo}.</returns> /// <returns>IEnumerable{CountryInfo}.</returns>
CountryInfo[] GetCountries(); Task<CountryInfo[]> GetCountries();
/// <summary> /// <summary>
/// Gets the parental ratings. /// Gets the parental ratings.
/// </summary> /// </summary>
/// <returns>IEnumerable{ParentalRating}.</returns> /// <returns>IEnumerable{ParentalRating}.</returns>
ParentalRating[] GetParentalRatings(); IEnumerable<ParentalRating> GetParentalRatings();
/// <summary> /// <summary>
/// Gets the rating level. /// Gets the rating level.
/// </summary> /// </summary>
@ -51,8 +53,6 @@ namespace MediaBrowser.Model.Globalization
/// <returns>IEnumerable{LocalizatonOption}.</returns> /// <returns>IEnumerable{LocalizatonOption}.</returns>
LocalizationOption[] GetLocalizationOptions(); LocalizationOption[] GetLocalizationOptions();
string RemoveDiacritics(string text);
string NormalizeFormKD(string text); string NormalizeFormKD(string text);
bool HasUnicodeCategory(string value, UnicodeCategory category); bool HasUnicodeCategory(string value, UnicodeCategory category);

View File

@ -566,8 +566,7 @@ namespace MediaBrowser.Providers.Manager
var providersWithChanges = providers var providersWithChanges = providers
.Where(i => .Where(i =>
{ {
var hasFileChangeMonitor = i as IHasItemChangeMonitor; if (i is IHasItemChangeMonitor hasFileChangeMonitor)
if (hasFileChangeMonitor != null)
{ {
return HasChanged(item, hasFileChangeMonitor, options.DirectoryService); return HasChanged(item, hasFileChangeMonitor, options.DirectoryService);
} }

View File

@ -74,17 +74,12 @@ namespace MediaBrowser.Providers.MediaInfo
} }
} }
if (item.SupportsLocalMetadata) if (item.SupportsLocalMetadata && video != null && !video.IsPlaceHolder
&& !video.SubtitleFiles.SequenceEqual(
_subtitleResolver.GetExternalSubtitleFiles(video, directoryService, false), StringComparer.Ordinal))
{ {
if (video != null && !video.IsPlaceHolder) _logger.LogDebug("Refreshing {0} due to external subtitles change.", item.Path);
{ return true;
if (!video.SubtitleFiles
.SequenceEqual(_subtitleResolver.GetExternalSubtitleFiles(video, directoryService, false), StringComparer.Ordinal))
{
_logger.LogDebug("Refreshing {0} due to external subtitles change.", item.Path);
return true;
}
}
} }
return false; return false;