-
Notifications
You must be signed in to change notification settings - Fork 751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move CultureInfoGenerator and CultureCache projects from Localization repo #52
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions
20
...ions.CultureInfoGenerator.Sources/Microsoft.Extensions.CultureInfoGenerator.Sources.xproj
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>41f14b0d-5f94-49c7-a5ea-12eb846c2bdb</ProjectGuid> | ||
<RootNamespace>Microsoft.Extensions.CultureInfoGenerator.Sources</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> |
110 changes: 110 additions & 0 deletions
110
src/Microsoft.Extensions.CultureInfoGenerator.Sources/Program.cs
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,110 @@ | ||
// 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 System.Linq; | ||
using Microsoft.Dnx.Runtime; | ||
using Microsoft.Win32; | ||
|
||
namespace Microsoft.Extensions.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 = Path.GetFullPath(args.Length > 0 ? args[0] : Path.Combine(_appPath, "../Microsoft.Extensions.Globalization.CultureInfoCache.Sources/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. | ||
|
||
// *************************** THIS FILE IS GENERATED BY A TOOL *************************** | ||
// To make changes to this file look at the CultureInfoGenerator project in this solution. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Extensions.Globalization | ||
{{ | ||
/// <summary> | ||
/// Contains a list of known culture names that can be used to create a <see cref=""System.Globalization.CultureInfo""/>. | ||
/// </summary> | ||
public static partial class CultureInfoCache | ||
{{ | ||
/// <summary> | ||
/// 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. | ||
/// </summary> | ||
public static readonly HashSet<string> KnownCultureNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase) | ||
{{" | ||
); | ||
|
||
var cultures = CultureInfo.GetCultures( | ||
CultureTypes.NeutralCultures | ||
| CultureTypes.InstalledWin32Cultures | ||
| CultureTypes.SpecificCultures); | ||
|
||
writer.WriteLine(string.Join($",{Environment.NewLine}", cultures.Select(c => $" \"{c.Name}\""))); | ||
writer.WriteLine( | ||
@" }; | ||
} | ||
}"); | ||
|
||
Console.WriteLine($"{cultures.Length} culture names written to {outputFilePath}"); | ||
} | ||
} | ||
|
||
// .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"; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Microsoft.Extensions.CultureInfoGenerator.Sources/project.json
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,17 @@ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, why is the generator a "Sources" package? Shouldn't it be a regular old console app package that can be run? |
||
"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.", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/aspnet/common" | ||
}, | ||
"dependencies": { | ||
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*" | ||
}, | ||
"commands": { | ||
"Microsoft.Extensions.CultureInfoGenerator.Sources": "Microsoft.Extensions.CultureInfoGenerator.Sources" | ||
}, | ||
"frameworks": { | ||
"dnx451": { } | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Microsoft.Extensions.Globalization.CultureInfoCache.Sources/CultureInfoCache.cs
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,62 @@ | ||
// 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.Concurrent; | ||
using System.Globalization; | ||
|
||
namespace Microsoft.Extensions.Globalization | ||
{ | ||
/// <summary> | ||
/// Provides read-only cached instances of <see cref="CultureInfo"/>. | ||
/// </summary> | ||
public static partial class CultureInfoCache | ||
{ | ||
private static readonly ConcurrentDictionary<string, CacheEntry> _cache = new ConcurrentDictionary<string, CacheEntry>(); | ||
|
||
/// <summary> | ||
/// Gets a read-only cached <see cref="CultureInfo"/> for the specified name. Only names that exist in | ||
/// <see cref="KnownCultureNames"/> will be used. | ||
/// </summary> | ||
/// <param name="name">The culture name.</param> | ||
/// <returns> | ||
/// A read-only cached <see cref="CultureInfo"/> or <c>null</c> a match wasn't found in | ||
/// <see cref="KnownCultureNames"/>. | ||
/// </returns> | ||
public static CultureInfo GetCultureInfo(string name) | ||
{ | ||
// Allow only known culture names as this API is called with input from users (HTTP requests) and | ||
// creating CultureInfo objects is expensive and we don't want it to throw either. | ||
if (name == null || !KnownCultureNames.Contains(name)) | ||
{ | ||
return null; | ||
} | ||
|
||
var entry = _cache.GetOrAdd(name, n => | ||
{ | ||
try | ||
{ | ||
return new CacheEntry(CultureInfo.ReadOnly(new CultureInfo(n))); | ||
} | ||
catch (CultureNotFoundException) | ||
{ | ||
// This can still throw as the list of culture names we have is generated from latest .NET Framework | ||
// on latest Windows and thus contains names that won't be supported on lower framework or OS versions. | ||
// We can just cache the null result in these cases as it's ultimately bound by the list anyway. | ||
return new CacheEntry(cultureInfo: null); | ||
} | ||
}); | ||
|
||
return entry.CultureInfo; | ||
} | ||
|
||
private class CacheEntry | ||
{ | ||
public CacheEntry(CultureInfo cultureInfo) | ||
{ | ||
CultureInfo = cultureInfo; | ||
} | ||
|
||
public CultureInfo CultureInfo { get; } | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we update this to whatever .NET 4.6 RTM uses?