Log warnings when cultures specified by Request Culture Providers are unsupported

Fixes #372
This commit is contained in:
Pranav K 2017-08-28 14:31:56 -07:00
Родитель 7234a384f4
Коммит 41ae20525f
5 изменённых файлов: 98 добавлений и 41 удалений

Просмотреть файл

@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
</ItemGroup>
</Project>

Просмотреть файл

@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
namespace LocalizationSample
{
@ -154,6 +155,7 @@ $@"<!doctype html>
.Build();
var host = new WebHostBuilder()
.ConfigureLogging(factory => factory.AddConsole())
.UseKestrel()
.UseConfiguration(config)
.UseIISIntegration()

Просмотреть файл

@ -15,6 +15,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Options" />
</ItemGroup>

Просмотреть файл

@ -0,0 +1,38 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.Localization
{
internal static class RequestCultureProviderLoggerExtensions
{
private static readonly Action<ILogger, string, IList<StringSegment>, Exception> _unsupportedCulture;
private static readonly Action<ILogger, string, IList<StringSegment>, Exception> _unsupportedUICulture;
static RequestCultureProviderLoggerExtensions()
{
_unsupportedCulture = LoggerMessage.Define<string, IList<StringSegment>>(
LogLevel.Warning,
1,
"{requestCultureProvider} returned the following unsupported cultures '{cultures}'.");
_unsupportedUICulture = LoggerMessage.Define<string, IList<StringSegment>>(
LogLevel.Warning,
2,
"{requestCultureProvider} returned the following unsupported cultures '{cultures}'.");
}
public static void UnsupportedCultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> cultures)
{
_unsupportedCulture(logger, requestCultureProvider, cultures, null);
}
public static void UnsupportedUICultures(this ILogger logger, string requestCultureProvider, IList<StringSegment> uiCultures)
{
_unsupportedUICulture(logger, requestCultureProvider, uiCultures, null);
}
}
}

Просмотреть файл

@ -5,10 +5,11 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
@ -24,6 +25,7 @@ namespace Microsoft.AspNetCore.Localization
private readonly RequestDelegate _next;
private readonly RequestLocalizationOptions _options;
private ILogger _logger;
/// <summary>
/// Creates a new <see cref="RequestLocalizationMiddleware"/>.
@ -33,17 +35,12 @@ namespace Microsoft.AspNetCore.Localization
/// <see cref="RequestLocalizationMiddleware"/>.</param>
public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
_next = next;
_next = next ?? throw new ArgumentNullException(nameof(next));
_options = options.Value;
}
@ -68,52 +65,65 @@ namespace Microsoft.AspNetCore.Localization
foreach (var provider in _options.RequestCultureProviders)
{
var providerResultCulture = await provider.DetermineProviderCultureResult(context);
if (providerResultCulture != null)
if (providerResultCulture == null)
{
var cultures = providerResultCulture.Cultures;
var uiCultures = providerResultCulture.UICultures;
continue;
}
var cultures = providerResultCulture.Cultures;
var uiCultures = providerResultCulture.UICultures;
CultureInfo cultureInfo = null;
CultureInfo uiCultureInfo = null;
if (_options.SupportedCultures != null)
CultureInfo cultureInfo = null;
CultureInfo uiCultureInfo = null;
if (_options.SupportedCultures != null)
{
cultureInfo = GetCultureInfo(
cultures,
_options.SupportedCultures,
_options.FallBackToParentCultures);
if (cultureInfo == null)
{
cultureInfo = GetCultureInfo(
cultures,
_options.SupportedCultures,
_options.FallBackToParentCultures);
EnsureLogger(context);
_logger?.UnsupportedCultures(provider.GetType().Name, cultures);
}
}
if (_options.SupportedUICultures != null)
if (_options.SupportedUICultures != null)
{
uiCultureInfo = GetCultureInfo(
uiCultures,
_options.SupportedUICultures,
_options.FallBackToParentUICultures);
if (uiCultureInfo == null)
{
uiCultureInfo = GetCultureInfo(
uiCultures,
_options.SupportedUICultures,
_options.FallBackToParentUICultures);
EnsureLogger(context);
_logger?.UnsupportedCultures(provider.GetType().Name, uiCultures);
}
}
if (cultureInfo == null && uiCultureInfo == null)
{
continue;
}
if (cultureInfo == null && uiCultureInfo == null)
{
continue;
}
if (cultureInfo == null && uiCultureInfo != null)
{
cultureInfo = _options.DefaultRequestCulture.Culture;
}
if (cultureInfo == null && uiCultureInfo != null)
{
cultureInfo = _options.DefaultRequestCulture.Culture;
}
if (cultureInfo != null && uiCultureInfo == null)
{
uiCultureInfo = _options.DefaultRequestCulture.UICulture;
}
if (cultureInfo != null && uiCultureInfo == null)
{
uiCultureInfo = _options.DefaultRequestCulture.UICulture;
}
var result = new RequestCulture(cultureInfo, uiCultureInfo);
var result = new RequestCulture(cultureInfo, uiCultureInfo);
if (result != null)
{
requestCulture = result;
winningProvider = provider;
break;
}
if (result != null)
{
requestCulture = result;
winningProvider = provider;
break;
}
}
}
@ -125,6 +135,11 @@ namespace Microsoft.AspNetCore.Localization
await _next(context);
}
private void EnsureLogger(HttpContext context)
{
_logger = _logger ?? context.RequestServices.GetService<ILogger<RequestLocalizationMiddleware>>();
}
private static void SetCurrentThreadCulture(RequestCulture requestCulture)
{
CultureInfo.CurrentCulture = requestCulture.Culture;