Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

Commit 861ae52

Browse files
committed
Added support for get culture information from request's route data information.
[Fixes #122] Get segment from mapped route for CustomRequestCultureProvider
1 parent 07e7741 commit 861ae52

12 files changed

Lines changed: 403 additions & 6 deletions

Localization.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ResourcesClassLibraryWithAt
3535
EndProject
3636
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ResourcesClassLibraryNoAttribute", "test\ResourcesClassLibraryNoAttribute\ResourcesClassLibraryNoAttribute.xproj", "{34740578-D5B5-4FB4-AFD4-5E87B5443E20}"
3737
EndProject
38+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Localization.Routing", "src\Microsoft.AspNetCore.Localization.Routing\Microsoft.AspNetCore.Localization.Routing.xproj", "{E1B8DA18-7885-40ED-94ED-881BB0804F23}"
39+
EndProject
40+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Localization.Routing.Tests", "test\Microsoft.AspNetCore.Localization.Routing.Tests\Microsoft.AspNetCore.Localization.Routing.Tests.xproj", "{375B000B-5DC0-4D16-AC0C-A5432D8E7581}"
41+
EndProject
3842
Global
3943
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4044
Debug|Any CPU = Debug|Any CPU
@@ -85,6 +89,14 @@ Global
8589
{34740578-D5B5-4FB4-AFD4-5E87B5443E20}.Debug|Any CPU.Build.0 = Debug|Any CPU
8690
{34740578-D5B5-4FB4-AFD4-5E87B5443E20}.Release|Any CPU.ActiveCfg = Release|Any CPU
8791
{34740578-D5B5-4FB4-AFD4-5E87B5443E20}.Release|Any CPU.Build.0 = Release|Any CPU
92+
{E1B8DA18-7885-40ED-94ED-881BB0804F23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
93+
{E1B8DA18-7885-40ED-94ED-881BB0804F23}.Debug|Any CPU.Build.0 = Debug|Any CPU
94+
{E1B8DA18-7885-40ED-94ED-881BB0804F23}.Release|Any CPU.ActiveCfg = Release|Any CPU
95+
{E1B8DA18-7885-40ED-94ED-881BB0804F23}.Release|Any CPU.Build.0 = Release|Any CPU
96+
{375B000B-5DC0-4D16-AC0C-A5432D8E7581}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
97+
{375B000B-5DC0-4D16-AC0C-A5432D8E7581}.Debug|Any CPU.Build.0 = Debug|Any CPU
98+
{375B000B-5DC0-4D16-AC0C-A5432D8E7581}.Release|Any CPU.ActiveCfg = Release|Any CPU
99+
{375B000B-5DC0-4D16-AC0C-A5432D8E7581}.Release|Any CPU.Build.0 = Release|Any CPU
88100
EndGlobalSection
89101
GlobalSection(SolutionProperties) = preSolution
90102
HideSolutionNode = FALSE
@@ -101,5 +113,7 @@ Global
101113
{B1B441BA-3AC8-49F8-850D-E5A178E77DE2} = {B723DB83-A670-4BCB-95FB-195361331AD2}
102114
{F27639B9-913E-43AF-9D64-BBD98D9A420A} = {B723DB83-A670-4BCB-95FB-195361331AD2}
103115
{34740578-D5B5-4FB4-AFD4-5E87B5443E20} = {B723DB83-A670-4BCB-95FB-195361331AD2}
116+
{E1B8DA18-7885-40ED-94ED-881BB0804F23} = {FB313677-BAB3-4E49-8CDB-4FA4A9564767}
117+
{375B000B-5DC0-4D16-AC0C-A5432D8E7581} = {B723DB83-A670-4BCB-95FB-195361331AD2}
104118
EndGlobalSection
105119
EndGlobal
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0.25420" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25420</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>e1b8da18-7885-40ed-94ed-881bb0804f23</ProjectGuid>
10+
<RootNamespace>Microsoft.AspNetCore.Localization.Routing</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
</PropertyGroup>
14+
15+
<PropertyGroup>
16+
<SchemaVersion>2.0</SchemaVersion>
17+
</PropertyGroup>
18+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
19+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Reflection;
5+
using System.Resources;
6+
7+
[assembly: AssemblyMetadata("Serviceable", "True")]
8+
[assembly: NeutralResourcesLanguage("en-us")]
9+
[assembly: AssemblyCompany("Microsoft Corporation.")]
10+
[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")]
11+
[assembly: AssemblyProduct("Microsoft ASP.NET Core")]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Routing;
8+
using Microsoft.Extensions.Internal;
9+
10+
namespace Microsoft.AspNetCore.Localization.Routing
11+
{
12+
/// <summary>
13+
/// Determines the culture information for a request via values in the route data.
14+
/// </summary>
15+
public class RouteDataRequestCultureProvider : RequestCultureProvider
16+
{
17+
/// <summary>
18+
/// The key that contains the culture name.
19+
/// Defaults to "culture".
20+
/// </summary>
21+
public string RouteDataStringKey { get; set; } = "culture";
22+
23+
/// <summary>
24+
/// The key that contains the UI culture name. If not specified or no value is found,
25+
/// <see cref="RouteDataStringKey"/> will be used.
26+
/// Defaults to "ui-culture".
27+
/// </summary>
28+
public string UIRouteDataStringKey { get; set; } = "ui-culture";
29+
30+
/// <inheritdoc />
31+
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
32+
{
33+
if (httpContext == null)
34+
{
35+
throw new ArgumentNullException(nameof(httpContext));
36+
}
37+
38+
string culture = null;
39+
string uiCulture = null;
40+
41+
if (!string.IsNullOrEmpty(RouteDataStringKey))
42+
{
43+
culture = httpContext.GetRouteValue(RouteDataStringKey)?.ToString();
44+
}
45+
46+
if (!string.IsNullOrEmpty(UIRouteDataStringKey))
47+
{
48+
uiCulture = httpContext.GetRouteValue(UIRouteDataStringKey)?.ToString();
49+
}
50+
51+
if (culture == null && uiCulture == null)
52+
{
53+
// No values specified for either so no match
54+
return TaskCache<ProviderCultureResult>.DefaultCompletedTask;
55+
}
56+
57+
if (culture != null && uiCulture == null)
58+
{
59+
// Value for culture but not for UI culture so default to culture value for both
60+
uiCulture = culture;
61+
}
62+
63+
if (culture == null && uiCulture != null)
64+
{
65+
// Value for UI culture but not for culture so default to UI culture value for both
66+
culture = uiCulture;
67+
}
68+
69+
var providerResultCulture = new ProviderCultureResult(culture, uiCulture);
70+
71+
return Task.FromResult(providerResultCulture);
72+
}
73+
}
74+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"version": "1.1.0-*",
3+
"description": "Provides a request culture provider which gets culture and ui-culture from request's route data.",
4+
"packOptions": {
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/aspnet/localization"
8+
},
9+
"tags": [
10+
"aspnetcore",
11+
"localization"
12+
]
13+
},
14+
"buildOptions": {
15+
"warningsAsErrors": true,
16+
"keyFile": "../../tools/Key.snk",
17+
"nowarn": [
18+
"CS1591"
19+
],
20+
"xmlDoc": true
21+
},
22+
"dependencies": {
23+
"Microsoft.AspNetCore.Localization": "1.1.0-*",
24+
"Microsoft.AspNetCore.Routing.Abstractions": "1.1.0-*",
25+
"Microsoft.Extensions.TaskCache.Sources": {
26+
"type": "build",
27+
"version": "1.1.0-*"
28+
}
29+
},
30+
"frameworks": {
31+
"net451": {},
32+
"netstandard1.3": {}
33+
}
34+
}

src/Microsoft.AspNetCore.Localization/AcceptLanguageHeaderRequestCultureProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Internal;
89
using Microsoft.Net.Http.Headers;
910

1011
namespace Microsoft.AspNetCore.Localization
@@ -33,7 +34,7 @@ public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpC
3334

3435
if (acceptLanguageHeader == null || acceptLanguageHeader.Count == 0)
3536
{
36-
return Task.FromResult((ProviderCultureResult)null);
37+
return TaskCache<ProviderCultureResult>.DefaultCompletedTask;
3738
}
3839

3940
var languages = acceptLanguageHeader.AsEnumerable();
@@ -53,7 +54,7 @@ public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpC
5354
return Task.FromResult(new ProviderCultureResult(orderedLanguages));
5455
}
5556

56-
return Task.FromResult((ProviderCultureResult)null);
57+
return TaskCache<ProviderCultureResult>.DefaultCompletedTask;
5758
}
5859
}
5960
}

src/Microsoft.AspNetCore.Localization/CookieRequestCultureProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Extensions.Internal;
78

89
namespace Microsoft.AspNetCore.Localization
910
{
@@ -39,7 +40,7 @@ public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpC
3940

4041
if (string.IsNullOrEmpty(cookie))
4142
{
42-
return Task.FromResult<ProviderCultureResult>(null);
43+
return TaskCache<ProviderCultureResult>.DefaultCompletedTask;
4344
}
4445

4546
var providerResultCulture = ParseCookieValue(cookie);

src/Microsoft.AspNetCore.Localization/QueryStringRequestCultureProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Extensions.Internal;
78

89
namespace Microsoft.AspNetCore.Localization
910
{
@@ -36,7 +37,7 @@ public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpC
3637
var request = httpContext.Request;
3738
if (!request.QueryString.HasValue)
3839
{
39-
return Task.FromResult((ProviderCultureResult)null);
40+
return TaskCache<ProviderCultureResult>.DefaultCompletedTask;
4041
}
4142

4243
string queryCulture = null;
@@ -55,7 +56,7 @@ public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpC
5556
if (queryCulture == null && queryUICulture == null)
5657
{
5758
// No values specified for either so no match
58-
return Task.FromResult((ProviderCultureResult)null);
59+
return TaskCache<ProviderCultureResult>.DefaultCompletedTask;
5960
}
6061

6162
if (queryCulture != null && queryUICulture == null)

src/Microsoft.AspNetCore.Localization/project.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
"Microsoft.AspNetCore.Http.Extensions": "1.1.0-*",
2424
"Microsoft.Extensions.Globalization.CultureInfoCache": "1.1.0-*",
2525
"Microsoft.Extensions.Localization.Abstractions": "1.1.0-*",
26-
"Microsoft.Extensions.Options": "1.1.0-*"
26+
"Microsoft.Extensions.Options": "1.1.0-*",
27+
"Microsoft.Extensions.TaskCache.Sources": {
28+
"type": "build",
29+
"version": "1.1.0-*"
30+
}
2731
},
2832
"frameworks": {
2933
"net451": {},
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0.25420" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25420</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>375b000b-5dc0-4d16-ac0c-a5432d8e7581</ProjectGuid>
10+
<RootNamespace>Microsoft.AspNetCore.Localization.Routing.Tests</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
</PropertyGroup>
14+
<PropertyGroup>
15+
<SchemaVersion>2.0</SchemaVersion>
16+
</PropertyGroup>
17+
<ItemGroup>
18+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
19+
</ItemGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>

0 commit comments

Comments
 (0)