diff --git a/samples/LocalizationSample/LocalizationSample.csproj b/samples/LocalizationSample/LocalizationSample.csproj index 6d078d3..f3d0573 100644 --- a/samples/LocalizationSample/LocalizationSample.csproj +++ b/samples/LocalizationSample/LocalizationSample.csproj @@ -13,6 +13,7 @@ + diff --git a/samples/LocalizationSample/Startup.cs b/samples/LocalizationSample/Startup.cs index dccbf54..ef6e638 100644 --- a/samples/LocalizationSample/Startup.cs +++ b/samples/LocalizationSample/Startup.cs @@ -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 @@ $@" .Build(); var host = new WebHostBuilder() + .ConfigureLogging(factory => factory.AddConsole()) .UseKestrel() .UseConfiguration(config) .UseIISIntegration() diff --git a/src/Microsoft.AspNetCore.Localization/Microsoft.AspNetCore.Localization.csproj b/src/Microsoft.AspNetCore.Localization/Microsoft.AspNetCore.Localization.csproj index 1ddf696..a34013b 100644 --- a/src/Microsoft.AspNetCore.Localization/Microsoft.AspNetCore.Localization.csproj +++ b/src/Microsoft.AspNetCore.Localization/Microsoft.AspNetCore.Localization.csproj @@ -15,6 +15,7 @@ + diff --git a/src/Microsoft.AspNetCore.Localization/RequestCultureProviderLoggerExtensions.cs b/src/Microsoft.AspNetCore.Localization/RequestCultureProviderLoggerExtensions.cs new file mode 100644 index 0000000..d7d6821 --- /dev/null +++ b/src/Microsoft.AspNetCore.Localization/RequestCultureProviderLoggerExtensions.cs @@ -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, Exception> _unsupportedCulture; + private static readonly Action, Exception> _unsupportedUICulture; + + static RequestCultureProviderLoggerExtensions() + { + _unsupportedCulture = LoggerMessage.Define>( + LogLevel.Warning, + 1, + "{requestCultureProvider} returned the following unsupported cultures '{cultures}'."); + _unsupportedUICulture = LoggerMessage.Define>( + LogLevel.Warning, + 2, + "{requestCultureProvider} returned the following unsupported cultures '{cultures}'."); + } + + public static void UnsupportedCultures(this ILogger logger, string requestCultureProvider, IList cultures) + { + _unsupportedCulture(logger, requestCultureProvider, cultures, null); + } + + public static void UnsupportedUICultures(this ILogger logger, string requestCultureProvider, IList uiCultures) + { + _unsupportedUICulture(logger, requestCultureProvider, uiCultures, null); + } + } +} diff --git a/src/Microsoft.AspNetCore.Localization/RequestLocalizationMiddleware.cs b/src/Microsoft.AspNetCore.Localization/RequestLocalizationMiddleware.cs index 01cef8c..b186a76 100644 --- a/src/Microsoft.AspNetCore.Localization/RequestLocalizationMiddleware.cs +++ b/src/Microsoft.AspNetCore.Localization/RequestLocalizationMiddleware.cs @@ -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; /// /// Creates a new . @@ -33,17 +35,12 @@ namespace Microsoft.AspNetCore.Localization /// . public RequestLocalizationMiddleware(RequestDelegate next, IOptions 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>(); + } + private static void SetCurrentThreadCulture(RequestCulture requestCulture) { CultureInfo.CurrentCulture = requestCulture.Culture;