学习.NET (2) 语言和国家地区 下 Language and Region (2nd Half)

来源:互联网 发布:算量软件 编辑:程序博客网 时间:2024/05/06 19:13
Part II: Language and Region (2nd Half)
1.       CultureInfo used to contain all the information, including Calendar, DateTimeFormatTextInfo, as so on, but concerning of strings for a multi-languages application, a simple implement is that create the resource assembly only contains the string for different languages, not for each culture.
2.       CurrentCulture propertyof CultureInfo specifies the formatting, such as datetime, compare, numberic; CurrentUICulture property specifies the resource loading.
3.       Microsoft recommends that move all localizable resources to separate resource-only DLLs. It is totally aggressing that do not hard code the strings or other resources.
4.       Using System.Resources.ResourceManager to retriever resources based on culture.
5.       FCL support a package and deploy technology named satellite assembly for retrieving resource from culture-specific resources. Satellite assemblies only contain resource files, or loose resources such as .gif files. In the satellite assembly deployment model, you create an application with a default assembly (the main assembly) and several satellite assemblies. You should package the resources for the default or neutral assembly with the main assembly and create a separate satellite assembly for each language that your application supports. Because the satellite assemblies are not part of the main assembly, you can easily replace or update resources corresponding to a specific culture without replacing the application's main assembly.
6.       FCL also support several classes to read/edit or build the resource. All the resource support via interface IResourceReaderInterface and IResourceWriterInterface. There are 4 classes inherited from the interfaces in the FCL. ResourceReader, ResourceWriter, this two classes read/write at default binary resource format file. ResXResourceReader, ResXResourceWriter, however, there support read/write resx file.
7.       You can create just text file to contain strings, the format must be ‘key=value’, same as ini file. And you must use resx or resource file to contain other objects. But assembly can only contain .resouce file, that means, the text file or resx file must compile into resource file before embedded into resource DLL, Resource File Generator (resgen.exe) is created to do this job.
8.       The .NET Framework uses a hub and spoke model to package and deploy resources. The hub is the main assembly that contains the nonlocalizable executable code and the resources for a single culture, called the neutral or default culture. The default culture is the fallback culture for the application. Each spoke connects to a satellite assembly that contains the resources for a single culture, but does not contain any code.
9.       Resource fallback process:
         i.            The runtime first checks the global assembly cache for an assembly matching the requested culture for your application.
       ii.            The global assembly cache can store resource assemblies that are shared by many applications. This frees you from having to include specific sets of resources in the directory structure of every application you create. If the runtime finds a reference to the assembly, it searches the assembly for the requested resource. If it finds the entry in the assembly, it uses the requested resource. If it does not find the entry, it continues the search.
      iii.            The runtime next checks the directory of the currently executing assembly for a directory matching the requested culture. If it finds the directory, it searches that directory for a valid satellite assembly for the requested culture. The runtime then searches the satellite assembly for the requested resource. If it finds the resource in the assembly, it uses it. If it does not find the resource, it continues the search.
      iv.            The runtime next searches the global assembly cache again, this time for the parent assembly of the requested resource. If the parent assembly exists in the global assembly cache, the runtime searches the assembly for the requested resource.
       v.            The parent is defined as the appropriate fallback culture. Consider parents as best-fit candidates; providing any resource is preferable to throwing an exception. This process also allows you to reuse resources. You need to include a particular resource at the parent level only if the child culture does not need to localize the requested resource. For example, if you supply satellite assemblies for en (neutral English), en-GB (English as spoken in the UK), and en-US (English as spoken in the US), the en satellite would contain the common terminology, and the en-GB and en-US satellites could provide overrides for only those terms that differ.
      vi.            The runtime next checks the directory of the currently executing assembly to see if it contains a parent directory. If a parent directory exists, the runtime searches the directory for a valid satellite assembly for the parent culture. If it finds the assembly, the runtime searches the assembly for the requested resource. If it finds the resource, it uses it. If it does not find the resource, it continues the search.
    vii.            The runtime next searches parent assemblies, as in the previous step, through many potential levels. Each culture has only one parent, but a parent might have its own parent.
   viii.            If the culture that was originally specified and all parents have been searched and the resource is still not found, the resource for the default (fallback) culture is used. Beginning with the .NET Framework version 2.0 release, you can specify that the ultimate fallback location for resources is a satellite assembly, rather than the main assembly. By using the NeutralResourcesLanguageAttribute with the UltimateResourceFallbackLocation enumeration, you can control whether the ultimate fallback location for resources is in the main assembly, or in a satellite assembly.
10.   My solution (I): For building a multiple languages application, and can be used in ASP.Net application and Windows Form application both: Create an assembly for loading string, and create satellite assemblies for this assembly. Be aware of it: must specify the CurrentUICulture of the thread who will call the method. Code of this assembly:
    public class ACLanguages
    {
        // Constructor
        private ACLanguages()
        {
        }
 
        // Load strings
        static public string LoadString(string strKey)
        {
//            rm = new ResourceManager("MyImages", this.GetType().Assembly);
//            return rm.GetString(strKey, cur_culture);
            return rm.GetString(strKey);
        }
 
        // Members
        static private ResourceManager rm = new ResourceManager("Languages.Strings", Assembly.GetExecutingAssembly());
        static CultureInfo cur_culture = Thread.CurrentThread.CurrentUICulture;
    }
11.   Create a bat file to build the satellite assemblies, you can see ACLanguages.cs code above, and AssemblyInfo.cs is used to control some information of output library, and the default culture also defined in this file..
@echo off
@Set Path=C:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Bin;C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727;C:/Program Files/Microsoft Visual Studio 8/VC/bin;C:/Program Files/Microsoft Visual Studio 8/Common7/IDE;C:/Program Files/Microsoft Visual Studio 8/VC/vcpackages;%PATH%
@Set LIB=C:/Program Files/Microsoft Visual Studio 8/VC/lib;C:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Lib;%LIB%
@Set INCLUDE=C:/Program Files/Microsoft Visual Studio 8/VC/include;C:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/include;%INCLUDE%
@Set NetSamplePath=C:/Program Files/Microsoft Visual Studio 8/SDK/v2.0
@Set VCBUILD_DEFAULT_CFG=Debug^|Win32
@Set VCBUILD_DEFAULT_OPTIONS=/useenv
 
mkdir bin
mkdir resobjs
mkdir bin/en
mkdir bin/zh-CHS
mkdir bin/zh-CHT
mkdir bin/jp
 
resgen Strings.txt resobjs/AlvaChien.Languages.Strings.resources
resgen Strings.zh-CHS.txt resobjs/AlvaChien.Languages.Strings.zh-CHS.resources
resgen Strings.zh-CHT.txt resobjs/AlvaChien.Languages.Strings.zh-CHT.resources
resgen Strings.jp.txt resobjs/AlvaChien.Languages.Strings.jp.resources
 
csc /reference:C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/System.dll /optimize+ /out:bin/ACLanguages.dll /resource:resobjs/AlvaChien.Languages.Strings.resources /target:library Codes/ACLanguages.cs Codes/AssemblyInfo.cs
al /t:lib /embed:resobjs/AlvaChien.Languages.Strings.zh-CHS.resources /culture:zh-CHS /out:bin/zh-CHS/ACLanguages.resources.dll
al /t:lib /embed:resobjs/AlvaChien.Languages.Strings.zh-CHT.resources /culture:zh-CHT /out:bin/zh-CHT/ACLanguages.resources.dll
al /t:lib /embed:resobjs/AlvaChien.Languages.Strings.jp.resources /culture:jp /out:bin/jp/ACLanguages.resources.dll
 
pause
@echo on
12.   Put into your mind, do remember to use UNICODE format if you using text file as string container like me. Anyway, resx file will not occur this problem.
13.   Another solution is using CreateFileBaseResourceManager to manual load the resources file.
14.   The End.
原创粉丝点击