ResourceManager可以自动管理Assembly中的多语言资源,这使得多语言支持变得很容易。在生成一个ResourceManager实例的时候,有多个构造函数可以选择,罗列如下:
ResourceManager()()() | Initializes a new instance of the ResourceManager class with default values. | |
ResourceManager(Type) | Creates a ResourceManager that looks up resources in satellite assemblies based on information from the specified Type. | |
ResourceManager(String, Assembly) | Initializes a new instance of the ResourceManager class that looks up resources contained in files derived from the specified root name using the given Assembly. | |
ResourceManager(String, Assembly, Type) | Initializes a new instance of the ResourceManager class that looks up resources contained in files derived from the specified root name using the given Assembly. |
对于第三个和第四个,第一个参数为 String baseName。 baseName的解释是这样的:
The root name of the resources. For example, the root name for the resource file named "MyResource.en-US.resources" is "MyResource".
实际上,这个名字应当是NameSpaceOfAssembly+ResourceFileName.
如:
AssemblyName: MyAssembly.Caculator
ResourceFileNames: MyResource.Caculator.en.resx
MyREsource.Caculator.it.resx
则baseName应该为:MyAssembly.Caculator.MyResource.Caculator。
如果resx文件位于某个文件夹下,则baseName中还要包含文件夹名字。
如:
resx文件位于Resource/NLV
baseName = MyAssembly.Caculator.Resource.NLV.MyResource.Caculator。
如果名字指定的不对,很可能会碰到调用GetString(String name)的时候,总是得不到想要的结果。因为ResourceManager实际上并没有加载指定的资源。