[Asp.net 5] Localization-easy-to-use Localization-global information,

Source: Internet
Author: User

[Asp.net 5] Localization-easy-to-use Localization-global information,

This article briefly introduces the Localization solution:

  • Microsoft. Framework. Globalization. CultureInfoCache Project
  • CultureInfoGenerator Project

Microsoft. Framework. Globalization. CultureInfoCache

The function of the CultureInfoCache project is to buffer the CultureInfo of each region in the same way as its name. There is only one class in this project (divided into two categories), which is super simple. Next we will introduce it briefly:

CultureInfoCache: Core Buffer class. Static Method for external violence and leakage: CultureInfo GetCultureInfo (string name), which can be obtained from various regions based on the language code (such as cn and en.

Public static partial class CultureInfoCache {private static readonly ConcurrentDictionary <string, CacheEntry> _ cache = new ConcurrentDictionary <string, CacheEntry> (); public static CultureInfo GetCultureInfo (string name) {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) {return new CacheEntry (cultureInfo: null);}); return entry. cultureInfo;} private class CacheEntry {public CacheEntry (CultureInfo cultureInfo) {CultureInfo = cultureInfo;} public CultureInfo {get ;}} CultureInfoCacheCultureInfoCache Part1 public static partial class CultureInfoCache {public static readonly HashSet <string> KnownCultureNames = new HashSet <string >{# region culture "ar ",......... "zh-CHT" # endregion };}CultureInfoCache Part2
  • The system uses ConcurrentDictionary <string, CacheEntry> to cache data.
  • ConcurrentDictionary is a thread-safe Dictionary table and Dictionary <,> function is similar
  • CacheEntry is actually an encapsulation of CultureInfo and is only used internally
  • GetOrAdd: If the dictionary contains the GetOrAdd method, it is returned directly. If the dictionary does not contain the GetOrAdd method, it is returned after creation. The first parameter is the search parameter, and the second parameter is the func parameter, which is used for creation.
  • KnownCultureNames: used to store the language CultureInfo supported by the current environment. (Related to the operating system and. net environment)
  • Because KnownCultureNames needs to be dynamically generated, the two parts are separated.


The role of this project is to generate the Part2 of Microsoft. Framework. Globalization. CultureInfoCache. To be honest, I think this project has a problem with this architecture. Is code re-generated every time? However, the existing source code is like this. I just share it with you based on the existing logic.

If there is a problem, there is a problem with this solution. The following two problems need to be manually modified:

  • Modify the constructor. When appEnvironment is null, the default value is given.
Public Program (IApplicationEnvironment appEnvironment) {_ appName = appEnvironment = null? "CultureInfoGenerator": appEnvironment. ApplicationName; _ appPath = appEnvironment = null? "": AppEnvironment. ApplicationBasePath ;}Program
  • Modify outputFilePath path: ../Microsoft. Framework. Globalization. CultureInfoCache/CultureInfoList. cs to.../Microsoft. Framework. Globalization. CultureInfoCache/CultureInfoList. cs.
Var outputFilePath = args. Length> 0? Args [0]: Path. Combine (_ appPath, "../Microsoft. Framework. Globalization. CultureInfoCache/CultureInfoList. cs ");OutputFilePath

After the modification, run the project directly and you will find that the CultureInfoList. cs file in Microsoft. Framework. Globalization. CultureInfoCache solution is changed.

This file also deserves our attention, that is, the relationship between the version and the. net environment.

 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";        }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.