diff --git a/Localization.sln b/Localization.sln index 0df01140f0..ddfd06056c 100644 --- a/Localization.sln +++ b/Localization.sln @@ -11,6 +11,15 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Localizati EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Localization.Abstractions", "src\Microsoft.Framework.Localization.Abstractions\Microsoft.Framework.Localization.Abstractions.xproj", "{A1FCF259-70F6-4605-AA2D-E4B356BE771A}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{79878809-8D1C-4BD4-BA99-F1F13FF96FD8}" +EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "LocalizationSample", "samples\LocalizationSample\LocalizationSample.xproj", "{55D9501F-15B9-4339-A0AB-6082850E5FCE}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5E7E7712-E3E3-4AD5-9B89-7FC140359FF1}" + ProjectSection(SolutionItems) = preProject + global.json = global.json + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -29,6 +38,10 @@ Global {A1FCF259-70F6-4605-AA2D-E4B356BE771A}.Debug|Any CPU.Build.0 = Debug|Any CPU {A1FCF259-70F6-4605-AA2D-E4B356BE771A}.Release|Any CPU.ActiveCfg = Release|Any CPU {A1FCF259-70F6-4605-AA2D-E4B356BE771A}.Release|Any CPU.Build.0 = Release|Any CPU + {55D9501F-15B9-4339-A0AB-6082850E5FCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {55D9501F-15B9-4339-A0AB-6082850E5FCE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {55D9501F-15B9-4339-A0AB-6082850E5FCE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {55D9501F-15B9-4339-A0AB-6082850E5FCE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -37,5 +50,6 @@ Global {29743CFF-120E-40EB-9B89-5818425946FA} = {FB313677-BAB3-4E49-8CDB-4FA4A9564767} {23E3BC23-3464-4D9B-BF78-02CB2182BEF0} = {FB313677-BAB3-4E49-8CDB-4FA4A9564767} {A1FCF259-70F6-4605-AA2D-E4B356BE771A} = {FB313677-BAB3-4E49-8CDB-4FA4A9564767} + {55D9501F-15B9-4339-A0AB-6082850E5FCE} = {79878809-8D1C-4BD4-BA99-F1F13FF96FD8} EndGlobalSection EndGlobal diff --git a/global.json b/global.json new file mode 100644 index 0000000000..0ddf69c7e5 --- /dev/null +++ b/global.json @@ -0,0 +1,3 @@ +{ + "projects": [ "src" ] +} \ No newline at end of file diff --git a/samples/LocalizationSample/LocalizationSample.xproj b/samples/LocalizationSample/LocalizationSample.xproj new file mode 100644 index 0000000000..e81f2086c0 --- /dev/null +++ b/samples/LocalizationSample/LocalizationSample.xproj @@ -0,0 +1,19 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 55d9501f-15b9-4339-a0ab-6082850e5fce + LocalizationSample + ..\..\artifacts\obj\$(MSBuildProjectName) + ..\..\artifacts\bin\$(MSBuildProjectName)\ + + + 2.0 + 31454 + + + \ No newline at end of file diff --git a/samples/LocalizationSample/Startup.cs b/samples/LocalizationSample/Startup.cs new file mode 100644 index 0000000000..75d1cd2004 --- /dev/null +++ b/samples/LocalizationSample/Startup.cs @@ -0,0 +1,95 @@ +// 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.Globalization; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Localization; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.Localization; + +namespace LocalizationSample +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddLocalization(); + } + + public void Configure(IApplicationBuilder app, IStringLocalizer SR) + { + var options = new RequestLocalizationMiddlewareOptions(); + app.UseRequestLocalization(options); + + app.Run(async (context) => + { + context.Response.StatusCode = 200; + context.Response.ContentType = "text/html; charset=utf-8"; + + var requestCulture = context.GetFeature().RequestCulture; + + await context.Response.WriteAsync( +$@" + + + Request Localization + + +"); + await context.Response.WriteAsync($"

{SR["Request Localization Sample"]}

"); + await context.Response.WriteAsync("
"); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync("
"); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync("
"); + await context.Response.WriteAsync(" "); + await context.Response.WriteAsync($"{SR["reset"]}"); + await context.Response.WriteAsync("
"); + await context.Response.WriteAsync("
"); + await context.Response.WriteAsync(""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync($""); + await context.Response.WriteAsync("
{SR["Current request culture:"]}{requestCulture.Culture.DisplayName} ({requestCulture.Culture})
{SR["Current request UI culture:"]}{requestCulture.UICulture.DisplayName} ({requestCulture.UICulture})
{SR["Current thread culture:"]}{CultureInfo.CurrentCulture.DisplayName} ({CultureInfo.CurrentCulture})
{SR["Current thread UI culture:"]}{CultureInfo.CurrentUICulture.DisplayName} ({CultureInfo.CurrentUICulture})
{SR["Current date (invariant full):"]}{DateTime.Now.ToString("F", CultureInfo.InvariantCulture)}
{SR["Current date (invariant):"]}{DateTime.Now.ToString(CultureInfo.InvariantCulture)}
{SR["Current date (request full):"]}{DateTime.Now.ToString("F")}
{SR["Current date (request):"]}{DateTime.Now.ToString()}
{SR["Current time (invariant):"]}{DateTime.Now.ToString("T", CultureInfo.InvariantCulture)}
{SR["Current time (request):"]}{DateTime.Now.ToString("T")}
{SR["Big number (invariant):"]}{(Math.Pow(2, 42) + 0.42).ToString("N", CultureInfo.InvariantCulture)}
{SR["Big number (request):"]}{(Math.Pow(2, 42) + 0.42).ToString("N")}
"); + await context.Response.WriteAsync( +@" +"); + }); + } + + private static async System.Threading.Tasks.Task WriteCultureSelectOptions(HttpContext context) + { + await context.Response.WriteAsync($" "); + await context.Response.WriteAsync($" "); + await context.Response.WriteAsync($" "); + await context.Response.WriteAsync($" "); + await context.Response.WriteAsync($" "); + await context.Response.WriteAsync($" "); + await context.Response.WriteAsync($" "); + await context.Response.WriteAsync($" "); + await context.Response.WriteAsync($" "); +#if DNX451 + await context.Response.WriteAsync($" "); +#endif + } + } +} diff --git a/samples/LocalizationSample/project.json b/samples/LocalizationSample/project.json new file mode 100644 index 0000000000..9ab24a6df7 --- /dev/null +++ b/samples/LocalizationSample/project.json @@ -0,0 +1,38 @@ +{ + "webroot": "wwwroot", + "version": "1.0.0-*", + + "dependencies": { + "Microsoft.AspNet.Server.IIS": "1.0.0-*", + "Microsoft.AspNet.Server.WebListener": "1.0.0-*", + "Microsoft.AspNet.Localization": "1.0.0-*", + "Microsoft.Framework.Localization": "1.0.0-*" + }, + + "commands": { + "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000" + }, + + "frameworks": { + "dnx451": { }, + "dnxcore50": { + "dependencies": { + "System.Globalization": "4.0.10-*", + "System.Globalization.Extensions": "4.0.0-*" + } + } + }, + + "publishExclude": [ + "node_modules", + "bower_components", + "**.xproj", + "**.user", + "**.vspscc" + ], + "exclude": [ + "wwwroot", + "node_modules", + "bower_components" + ] +} diff --git a/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs b/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs index 2064690662..43ac3496dc 100644 --- a/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs +++ b/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs @@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Localization public async Task Invoke([NotNull] HttpContext context) { var requestCulture = _options.DefaultRequestCulture ?? - new RequestCulture(CultureInfo.DefaultThreadCurrentCulture, CultureInfo.DefaultThreadCurrentUICulture); + new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture); IRequestCultureStrategy winningStrategy = null; @@ -56,6 +56,20 @@ namespace Microsoft.AspNet.Localization } } + if (_options.SupportedCultures != null) + { + // Ensure that selected cultures are in the supported list and if not, set them to the default + if (!_options.SupportedCultures.Contains(requestCulture.Culture)) + { + requestCulture = new RequestCulture(_options.DefaultRequestCulture.Culture, requestCulture.UICulture); + } + + if (!_options.SupportedUICultures.Contains(requestCulture.UICulture)) + { + requestCulture = new RequestCulture(requestCulture.Culture, _options.DefaultRequestCulture.UICulture); + } + } + context.SetFeature(new RequestCultureFeature(requestCulture, winningStrategy)); SetCurrentThreadCulture(requestCulture); diff --git a/src/Microsoft.AspNet.Localization/RequestLocalizationMiddlewareOptions.cs b/src/Microsoft.AspNet.Localization/RequestLocalizationMiddlewareOptions.cs index cfa8e6e196..32827ebb1a 100644 --- a/src/Microsoft.AspNet.Localization/RequestLocalizationMiddlewareOptions.cs +++ b/src/Microsoft.AspNet.Localization/RequestLocalizationMiddlewareOptions.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Localization /// public RequestLocalizationMiddlewareOptions() { - DefaultRequestCulture = new RequestCulture(CultureInfo.DefaultThreadCurrentCulture, CultureInfo.DefaultThreadCurrentUICulture); + DefaultRequestCulture = new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture); RequestCultureStrategies = new List { diff --git a/src/Microsoft.Framework.Localization.Abstractions/project.json b/src/Microsoft.Framework.Localization.Abstractions/project.json index 63eb5f2524..b725e19028 100644 --- a/src/Microsoft.Framework.Localization.Abstractions/project.json +++ b/src/Microsoft.Framework.Localization.Abstractions/project.json @@ -10,11 +10,11 @@ "dnx451": { }, "dnxcore50": { "dependencies": { - "System.Collections": "4.0.10-beta-*", - "System.Globalization": "4.0.10-beta-*", - "System.Reflection": "4.0.10-beta-*", - "System.Runtime": "4.0.20-beta-22904", - "Microsoft.CSharp": "4.0.0-beta-*" + "System.Collections": "4.0.10-*", + "System.Globalization": "4.0.10-*", + "System.Reflection": "4.0.10-*", + "System.Runtime": "4.0.20-*", + "Microsoft.CSharp": "4.0.0-*" } } }