Added a sample & fixed some issues it found
This commit is contained in:
parent
b11f7d51c3
commit
3dc0d40f7d
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"projects": [ "src" ]
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>55d9501f-15b9-4339-a0ab-6082850e5fce</ProjectGuid>
|
||||
<RootNamespace>LocalizationSample</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<DevelopmentServerPort>31454</DevelopmentServerPort>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
|
|
@ -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<Startup> 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<IRequestCultureFeature>().RequestCulture;
|
||||
|
||||
await context.Response.WriteAsync(
|
||||
$@"<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Request Localization</title>
|
||||
<style>
|
||||
body {{ font-family: 'Segoe UI', Helvetica, Sans-Serif }}
|
||||
h1, h2, h3, h4, th {{ font-family: 'Segoe UI Light', Helvetica, Sans-Serif }}
|
||||
th {{ text-align: left }}
|
||||
</style>
|
||||
</head>
|
||||
<body>");
|
||||
await context.Response.WriteAsync($"<h1>{SR["Request Localization Sample"]}</h1>");
|
||||
await context.Response.WriteAsync("<form id=\"theForm\" method=\"get\">");
|
||||
await context.Response.WriteAsync($"<label for=\"culture\">{SR["Culture"]}: </label>");
|
||||
await context.Response.WriteAsync("<select id=\"culture\" name=\"culture\">");
|
||||
await WriteCultureSelectOptions(context);
|
||||
await context.Response.WriteAsync("</select><br />");
|
||||
await context.Response.WriteAsync($"<label for=\"uiCulture\">{SR["UI Culture"]}: </label>");
|
||||
await context.Response.WriteAsync("<select id=\"uiCulture\" name=\"ui-culture\">");
|
||||
await WriteCultureSelectOptions(context);
|
||||
await context.Response.WriteAsync("</select><br />");
|
||||
await context.Response.WriteAsync("<input type=\"submit\" value=\"go\" /> ");
|
||||
await context.Response.WriteAsync($"<a href=\"/\">{SR["reset"]}</a>");
|
||||
await context.Response.WriteAsync("</form>");
|
||||
await context.Response.WriteAsync("<br />");
|
||||
await context.Response.WriteAsync("<table><tbody>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current request culture:"]}</th><td>{requestCulture.Culture.DisplayName} ({requestCulture.Culture})</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current request UI culture:"]}</th><td>{requestCulture.UICulture.DisplayName} ({requestCulture.UICulture})</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current thread culture:"]}</th><td>{CultureInfo.CurrentCulture.DisplayName} ({CultureInfo.CurrentCulture})</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current thread UI culture:"]}</th><td>{CultureInfo.CurrentUICulture.DisplayName} ({CultureInfo.CurrentUICulture})</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current date (invariant full):"]}</th><td>{DateTime.Now.ToString("F", CultureInfo.InvariantCulture)}</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current date (invariant):"]}</th><td>{DateTime.Now.ToString(CultureInfo.InvariantCulture)}</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current date (request full):"]}</th><td>{DateTime.Now.ToString("F")}</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current date (request):"]}</th><td>{DateTime.Now.ToString()}</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current time (invariant):"]}</th><td>{DateTime.Now.ToString("T", CultureInfo.InvariantCulture)}</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Current time (request):"]}</th><td>{DateTime.Now.ToString("T")}</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Big number (invariant):"]}</th><td>{(Math.Pow(2, 42) + 0.42).ToString("N", CultureInfo.InvariantCulture)}</td></tr>");
|
||||
await context.Response.WriteAsync($"<tr><th>{SR["Big number (request):"]}</th><td>{(Math.Pow(2, 42) + 0.42).ToString("N")}</td></tr>");
|
||||
await context.Response.WriteAsync("</tbody></table>");
|
||||
await context.Response.WriteAsync(
|
||||
@"</body>
|
||||
</html>");
|
||||
});
|
||||
}
|
||||
|
||||
private static async System.Threading.Tasks.Task WriteCultureSelectOptions(HttpContext context)
|
||||
{
|
||||
await context.Response.WriteAsync($" <option value=\"\">-- select --</option>");
|
||||
await context.Response.WriteAsync($" <option value=\"{new CultureInfo("en-US").Name}\">{new CultureInfo("en-US").DisplayName}</option>");
|
||||
await context.Response.WriteAsync($" <option value=\"{new CultureInfo("en-AU").Name}\">{new CultureInfo("en-AU").DisplayName}</option>");
|
||||
await context.Response.WriteAsync($" <option value=\"{new CultureInfo("en-GB").Name}\">{new CultureInfo("en-GB").DisplayName}</option>");
|
||||
await context.Response.WriteAsync($" <option value=\"{new CultureInfo("fr-FR").Name}\">{new CultureInfo("fr-FR").DisplayName}</option>");
|
||||
await context.Response.WriteAsync($" <option value=\"{new CultureInfo("es-ES").Name}\">{new CultureInfo("es-ES").DisplayName}</option>");
|
||||
await context.Response.WriteAsync($" <option value=\"{new CultureInfo("ja-JP").Name}\">{new CultureInfo("ja-JP").DisplayName}</option>");
|
||||
await context.Response.WriteAsync($" <option value=\"{new CultureInfo("zh").Name}\">{new CultureInfo("zh").DisplayName}</option>");
|
||||
await context.Response.WriteAsync($" <option value=\"{new CultureInfo("zh-CN").Name}\">{new CultureInfo("zh-CN").DisplayName}</option>");
|
||||
#if DNX451
|
||||
await context.Response.WriteAsync($" <option value=\"{new CultureInfo("zh-CHT").Name}\">{new CultureInfo("zh-CHT").DisplayName}</option>");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
|
|
@ -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<IRequestCultureFeature>(new RequestCultureFeature(requestCulture, winningStrategy));
|
||||
|
||||
SetCurrentThreadCulture(requestCulture);
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Localization
|
|||
/// </summary>
|
||||
public RequestLocalizationMiddlewareOptions()
|
||||
{
|
||||
DefaultRequestCulture = new RequestCulture(CultureInfo.DefaultThreadCurrentCulture, CultureInfo.DefaultThreadCurrentUICulture);
|
||||
DefaultRequestCulture = new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
|
||||
|
||||
RequestCultureStrategies = new List<IRequestCultureStrategy>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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-*"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue