做一个关于“Creating a satellite assembly for culture specific resource file and using it in the executing assembly”的练习

添加了resources 文件之后 并在其中添加Name(Key) 、value

注:在以****.resx命名的resources文件中label1.Text不起作用(原因不明)

 

在Visual Studio .NET 命令提示框窗口中打入“Resgen MyResource.zh-CN.resx”生成MyResource.zh-CN.resources的二进制文件
通过在命令行窗口添加一下代码生成MyApplication.resources.dll文件

Al /t:lib /embed:D:\Exercise3\Exercise\MyApplication\MyResource.zh-CN.resources,MyApplication.MyResource.zh-CN.resources /culture:zh-CN /out:D:\Exercise3\Exercise\MyApplication\bin\zh-CN\MyApplication.resources.dll

在添加Button_click方法:

代码
1  private void button1_Click(object sender, EventArgs e)
2  {
3          messageId = label1.Text;
4          CultureInfo ci = new CultureInfo("zh-CN");
5          ResourceManager resmgr = new ResourceManager("MyResource.zh-CN", Assembly.GetExecutingAssembly());
6          label1.Text = resmgr.GetString(messageId, ci);
7  }

 

 

可是运行到第六行就报错:MissingManifestResourceException was unhandled
Could not find any resources appropriate for the specified culture or the neutral culture.
Make sure "MyResource.zh-CN.resources" was correctly embedded or linked into assembly "MyApplication" at compile time, or that all the satellite assemblies required are loadable and fully signed.

出错的原因:

ResourceManager("MyResource.zh-CN", Assembly.GetExecutingAssembly());中的第一个参数应该为Project名+resource 的文件名

应该改成:“Project名.resource 文件名”

代码
1         private void button1_Click(object sender, EventArgs e)
2         {
3             string  messageId = label1.Text;
4             CultureInfo ci = new CultureInfo("zh-CN");
5             ResourceManager resmgr = new ResourceManager("MyApplication.MyResource_zh", Assembly.GetExecutingAssembly());
6             string msg = resmgr.GetString(messageId, ci);
7             label1.Text = msg;
8 
9         }

 


 

可是还是不成功,于是我重建一个resource文件,以MyResource_zh为名,发现系统自动在资源文件下添加了一个.cs文件

其中有需要lookup的字段:

代码
/// <summary>
///   Looks up a localized string similar to Chinese.
/// </summary>
internal static string 中文 {
  
get {
          
return ResourceManager.GetString("中文", resourceCulture);
      }
}

 

 正确添加name与value之后,程序正确运行

 

注:

1、以*****.特定区域属性(如:en-US、zh-CN)命名的resource文件不会生成.cs文件(原因不详)

2、资源文件中所填的Name 和 Vale为程序中所要翻译的源和目标结果

 

posted on 2010-03-22 15:14  赤色彗星  阅读(943)  评论(0编辑  收藏  举报