- 右键点击您在
Solution Explorer
Project
。
- 选择添加 - >新建项目 - >资源 文件。
- 将其命名为
Resources.en-us.resx
。 (替换 'EN-US' 适当 代码)
- 单击添加
- 将其拖到
Properties
文件夹。
- 开放Resources.en-us.resx和改变它的
Access Modifier
到Public
。
- 添加您的字符串。
- 重复每个文化你需要支持 。
在构建VS将的.resx文件转换为.resource文件,并建立包装类为您服务。然后您可以通过名称空间YourAssembly.Properties.Resources
进行访问。
使用此使用语句。
using YourAssembly.Properties;
你可以用这样的属性修饰:
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "MyStringName")]
注:我使用的属性文件夹中的一致性。要使用App_GlobalResources将您的.resx文件移动到那里,并更改您的使用语句以匹配目录名称。就像这样:
using YourAssembly.App_GlobalResources;
编辑:你可以得到强类型资源名称最接近的将是做这样的事情:
public class ResourceNames
{
public const string EmailRequired = "EmailRequired";
}
然后,您可以用这样的属性装饰。
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = ResourceNames.EmailRequired)]
要启用自动客户培养检测添加globalizationsection到的web.config文件。
<configuration>
<system.web>
<globalization enableClientBasedCulture="true" culture="auto:en-us" uiCulture="auto:en-us"/>
</system.web>
<configuration>
在这里,我已经启用基于客户端的区域性和设置文化和的UICulture为“自动”与“EN-US”默认。
创建单独的附属程序集:
的MSDN Creating Satellite Assemblies文章也将有所帮助。 如果您是新来的卫星组件,请确保您阅读Packaging and Deploying Resources。
在过去创建附属程序集时,我发现使用VS构建事件很有用。这些是我会采取的步骤。
- 创建我的解决方案的独立
Class Library
项目。
- 创建或添加我的
.resx
文件到此项目。
- 添加一个
Post-Build Event
到Project Properties
对话框。 (类似下面)
样品VS生成后脚本:
set RESGEN="C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\resgen.exe"
set LINKER="C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\al.exe"
set ASSEMBLY=$(TargetName)
set SOURCEDIR=$(ProjectDir)
Set OUTDIR=$(TargetDir)
REM Build Default Culture Resources (en)
%RESGEN% %SOURCEDIR%en\%ASSEMBLY%.en.resx %SOURCEDIR%en\%ASSEMBLY%.resources
REM Embed Default Culture
%LINKER% /t:lib /embed:%SOURCEDIR%en\%ASSEMBLY%.resources /culture:en /out:%OUTDIR%%ASSEMBLY%.resources.dll
REM Embed English Culture
IF NOT EXIST %OUTDIR%en\ MKDIR $%OUTDIR%en\
%LINKER% /t:lib /embed:%SOURCEDIR%en\%ASSEMBLY%.resources /culture:en /out:%OUTDIR%en\%ASSEMBLY%.resources.dll
REM These are just a byproduct of using the project build event to run the resource build script
IF EXIST %OUTDIR%%ASSEMBLY%.dll DEL %OUTDIR%%ASSEMBLY%.dll
IF EXIST %OUTDIR%%ASSEMBLY%.pdb DEL %OUTDIR%%ASSEMBLY%.pdb
如果你不希望使用ResGen.exe转换您.resx
文件,你可以做这样的事情。
using System;
using System.Collections;
using System.IO;
using System.Resources;
namespace ResXConverter
{
public class ResxToResource
{
public void Convert(string resxPath, string resourcePath)
{
using (ResXResourceReader resxReader = new ResXResourceReader(resxPath))
using (IResourceWriter resWriter = new ResourceWriter(
new FileStream(resourcePath, FileMode.Create, FileAccess.Write)))
{
foreach (DictionaryEntry entry in resxReader)
{
resWriter.AddResource(entry.Key.ToString(), entry.Value);
}
resWriter.Generate();
resWriter.Close();
}
}
}
}
一个潜在的抽奖背上来执行转换这种方式是引用System.Windows.Forms.dll
的需要。您仍然需要使用Assembly Linker。
编辑:由于wRAR提醒我们您是否在签署装配您的钥匙must match。
我知道这一点,但我不想更改所有属性。 – wRAR 2010-04-05 08:49:21