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

Commit 0ac2a3f

Browse files
Use existing CultureInfo
1 parent b2ef91d commit 0ac2a3f

5 files changed

Lines changed: 79 additions & 3 deletions

File tree

src/Microsoft.AspNetCore.Localization/RequestLocalizationMiddleware.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Globalization;
7+
using System.Linq;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using Microsoft.AspNetCore.Builder;
1011
using Microsoft.AspNetCore.Http;
11-
using Microsoft.Extensions.Globalization;
1212
using Microsoft.Extensions.Options;
1313

1414
namespace Microsoft.AspNetCore.Localization
@@ -157,13 +157,32 @@ private static CultureInfo GetCultureInfo(
157157
return null;
158158
}
159159

160+
private static CultureInfo GetCultureInfo(string name, IList<CultureInfo> supportedCultures)
161+
{
162+
// Allow only known culture names as this API is called with input from users (HTTP requests) and
163+
// creating CultureInfo objects is expensive and we don't want it to throw either.
164+
if (name == null || supportedCultures == null)
165+
{
166+
return null;
167+
}
168+
var culture = supportedCultures.FirstOrDefault(
169+
supportedCulture => string.Equals(supportedCulture.Name, name, StringComparison.OrdinalIgnoreCase));
170+
171+
if (culture == null)
172+
{
173+
return null;
174+
}
175+
176+
return CultureInfo.ReadOnly(culture);
177+
}
178+
160179
private static CultureInfo GetCultureInfo(
161180
string cultureName,
162181
IList<CultureInfo> supportedCultures,
163182
bool fallbackToParentCultures,
164183
int currentDepth)
165184
{
166-
var culture = CultureInfoCache.GetCultureInfo(cultureName, supportedCultures);
185+
var culture = GetCultureInfo(cultureName, supportedCultures);
167186

168187
if (culture == null && fallbackToParentCultures && currentDepth < MaxCultureFallbackDepth)
169188
{

src/Microsoft.Extensions.Globalization.CultureInfoCache/CultureInfoCache.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Microsoft.Extensions.Globalization
1212
/// <summary>
1313
/// Provides read-only cached instances of <see cref="CultureInfo"/>.
1414
/// </summary>
15+
[Obsolete("This type is obsolete and will be removed in a future version.")]
1516
public static class CultureInfoCache
1617
{
1718
private static readonly ConcurrentDictionary<string, CacheEntry> _cache = new ConcurrentDictionary<string, CacheEntry>();
@@ -23,7 +24,7 @@ public static class CultureInfoCache
2324
/// <param name="name">The culture name.</param>
2425
/// <param name="supportedCultures">The cultures supported by the application.</param>
2526
/// <returns>
26-
/// A read-only cached <see cref="CultureInfo"/> or <c>null</c> a match wasn't found in
27+
/// A read-only cached <see cref="CultureInfo"/> or <c>null</c> if a match wasn't found in
2728
/// <paramref name="supportedCultures"/>.
2829
/// </returns>
2930
public static CultureInfo GetCultureInfo(string name, IList<CultureInfo> supportedCultures)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.Collections.Generic;
5+
using System.Globalization;
6+
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Localization;
9+
using Microsoft.Extensions.DependencyInjection;
10+
11+
namespace LocalizationWebsite
12+
{
13+
public class StartupCustomCulturePreserved
14+
{
15+
public void ConfigureServices(IServiceCollection services)
16+
{
17+
services.AddLocalization();
18+
}
19+
20+
public void Configure(
21+
IApplicationBuilder app)
22+
{
23+
app.UseRequestLocalization(new RequestLocalizationOptions
24+
{
25+
DefaultRequestCulture = new RequestCulture("en-US"),
26+
SupportedCultures = new List<CultureInfo>()
27+
{
28+
new CultureInfo("en-US") { NumberFormat= { CurrencySymbol = "kr" } }
29+
},
30+
SupportedUICultures = new List<CultureInfo>()
31+
{
32+
new CultureInfo("en-US") { NumberFormat= { CurrencySymbol = "kr" } }
33+
}
34+
});
35+
36+
app.Run(async (context) =>
37+
{
38+
await context.Response.WriteAsync(10.ToString("C"));
39+
});
40+
}
41+
}
42+
}

test/LocalizationWebsite/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dependencies": {
88
"Microsoft.AspNetCore.Localization": "1.1.0-*",
99
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0-*",
10+
"Microsoft.AspNetCore.Testing": "1.1.0-*",
1011
"Microsoft.Extensions.Configuration.CommandLine": "1.1.0-*",
1112
"Microsoft.Extensions.Localization": "1.1.0-*",
1213
"Microsoft.Extensions.Logging.Console": "1.1.0-*",

test/Microsoft.AspNetCore.Localization.FunctionalTests/LocalizationTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ public class LocalizationTest
1313
{
1414
private static readonly string _applicationPath = Path.Combine("test", "LocalizationWebsite");
1515

16+
[Fact]
17+
public Task Localization_CustomCulture()
18+
{
19+
var testRunner = new TestRunner(_applicationPath);
20+
return testRunner.RunTestAndVerifyResponse(
21+
RuntimeFlavor.CoreClr,
22+
RuntimeArchitecture.x64,
23+
"http://localhost:5070",
24+
"CustomCulturePreserved",
25+
"en-US",
26+
"kr10.00");
27+
}
28+
1629
[ConditionalTheory]
1730
[OSSkipCondition(OperatingSystems.Linux)]
1831
[OSSkipCondition(OperatingSystems.MacOSX)]

0 commit comments

Comments
 (0)