-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Culture names are now limited to a known list:
- Added tool to generate a set of known culture names from the OS/Fx - CultureInfoCache is now limited to only caching/returning cultures from the known list - #6
- Loading branch information
1 parent
ca4b85e
commit 9834a27
Showing
7 changed files
with
606 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?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>bd22ae1c-6631-4da6-874d-0dc0f803ceab</ProjectGuid> | ||
<RootNamespace>CultureInfoGenerator</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// 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 System.IO; | ||
using Microsoft.Framework.Runtime; | ||
using Microsoft.Win32; | ||
|
||
namespace CultureInfoGenerator | ||
{ | ||
public class Program | ||
{ | ||
private readonly string _appName; | ||
private readonly string _appPath; | ||
|
||
public Program(IApplicationEnvironment appEnvironment) | ||
{ | ||
_appName = appEnvironment.ApplicationName; | ||
_appPath = appEnvironment.ApplicationBasePath; | ||
} | ||
|
||
public void Main(string[] args) | ||
{ | ||
var outputFilePath = args.Length > 0 ? args[0] : Path.Combine(_appPath, "../Microsoft.AspNet.Localization/Internal/CultureInfoList.cs"); | ||
var netFxVersion = Get45or451FromRegistry(); | ||
var windowsVersion = Environment.OSVersion; | ||
|
||
using (var writer = new StreamWriter(outputFilePath, false)) | ||
{ | ||
writer.WriteLine($@"// 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.Collections.Generic; | ||
namespace Microsoft.AspNet.Localization.Internal | ||
{{ | ||
public static class CultureInfoList | ||
{{ | ||
// This list of known cultures was generated by {_appName} using .NET Framework {netFxVersion} on | ||
// {windowsVersion}. | ||
// As new versions of .NET Framework and Windows are released, this list should be regenerated to ensure it | ||
// contains the latest culture names. | ||
public static readonly HashSet<string> KnownCultureNames = new HashSet<string> | ||
{{" | ||
); | ||
|
||
var cultures = CultureInfo.GetCultures( | ||
CultureTypes.NeutralCultures | ||
| CultureTypes.InstalledWin32Cultures | ||
| CultureTypes.SpecificCultures); | ||
|
||
var format = " \"{0}\""; | ||
|
||
for (int i = 0; i < cultures.Length; i++) | ||
{ | ||
var culture = cultures[i]; | ||
|
||
writer.Write(format, culture.Name); | ||
|
||
if (i < cultures.Length - 1) | ||
{ | ||
writer.WriteLine(","); | ||
} | ||
else | ||
{ | ||
// Last entry | ||
writer.WriteLine(); | ||
} | ||
} | ||
|
||
writer.WriteLine( | ||
@" }; | ||
} | ||
}"); | ||
} | ||
} | ||
|
||
// .NET Framework detection code copied from https://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx#net_d | ||
private static string Get45or451FromRegistry() | ||
{ | ||
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32) | ||
.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\")) | ||
{ | ||
var releaseKey = Convert.ToInt32(ndpKey.GetValue("Release")); | ||
return CheckFor45DotVersion(releaseKey); | ||
} | ||
} | ||
|
||
// Checking the version using >= will enable forward compatibility, | ||
// however you should always compile your code on newer versions of | ||
// the framework to ensure your app works the same. | ||
private static string CheckFor45DotVersion(int releaseKey) | ||
{ | ||
if (releaseKey >= 393273) | ||
{ | ||
return "4.6 RC or later"; | ||
} | ||
if ((releaseKey >= 379893)) | ||
{ | ||
return "4.5.2 or later"; | ||
} | ||
if ((releaseKey >= 378675)) | ||
{ | ||
return "4.5.1 or later"; | ||
} | ||
if ((releaseKey >= 378389)) | ||
{ | ||
return "4.5 or later"; | ||
} | ||
// This line should never execute. A non-null release key should mean | ||
// that 4.5 or later is installed. | ||
return "No 4.5 or later version detected"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"version": "1.0.0-*", | ||
"description": "Generates a list of known culture names from the OS using CultureInfo.GetCultures. This tool is intended to be run on Windows using full .NET Framework.", | ||
|
||
"dependencies": { | ||
"Microsoft.Framework.Runtime.Abstractions": "1.0.0-beta5-11739" | ||
}, | ||
|
||
"commands": { | ||
"CultureInfoGenerator": "CultureInfoGenerator" | ||
}, | ||
|
||
"frameworks": { | ||
"dnx451": { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.