Phil Haack 有一篇很棒的文章本地化 ASP.Net MVC 验证它专门指导您覆盖字符串。本文更多适用于DataAnnotations
比它确实ASP.net MVC
。因此,无论您使用什么方式,这都会有所帮助数据注释.
下面我列出了在 Visual Studio 中添加本地化资源的最简单步骤。
- 打开
Project Properties
dialog.
- 选择
Resources
tab.
- 点击创建一个新的默认
资源文件.
- This will create two files in your
Properties
folder.
- When 资源.resx有
打开了,改一下
Access Modifier
to Public
.
- 添加您的字符串。
添加额外的资源文件 for 特定文化你需要这样做。
- 右键单击您的
Project
in the
Solution Explorer
.
- Select Add -> New Item -> Resource
File.
- Name it
Resources.en-us.resx
。
(将“en-us”替换为适当的
代码)
- 单击添加
- 将其拖入
Properties
folder.
- Open 资源.en-us.resx并改变它
Access Modifier
to Public
.
- 添加您的字符串。
- 对您需要的每种文化重复上述步骤
支持。
在构建期间 VS 将转换.resx文件到.resource文件并为您构建包装类。然后您可以通过命名空间访问YourAssembly.Properties.Resources
.
有了这个 using 语句。
using YourAssembly.Properties;
您可以使用如下属性进行装饰:
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "MyStringName")]
注意:我用的是特性文件夹以保持一致性。要使用 App_GlobalResources 移动您的.resx文件并更改您的 using 语句以匹配目录名称。像这样:
using YourAssembly.App_GlobalResources;
编辑:您可以获得的最接近强类型资源名称的方法是执行以下操作:
public class ResourceNames
{
public const string EmailRequired = "EmailRequired";
}
然后你可以用这样的属性来装饰。
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = ResourceNames.EmailRequired)]
要启用自动客户端文化检测,请添加全球化部分 to the 网络配置 file.
<configuration>
<system.web>
<globalization enableClientBasedCulture="true" culture="auto:en-us" uiCulture="auto:en-us"/>
</system.web>
<configuration>
在这里,我启用了基于客户端的文化并设置culture和文化 to "auto“ 默认为”en-us".
创建单独的附属组件:
微软软件定义网络创建附属程序集文章也会有帮助。
如果您是卫星组件的新手,请务必阅读打包和部署资源.
过去创建附属程序集时,我发现使用 VS 构建事件很有用。这些是我将采取的步骤。
- 创建一个单独的
Class Library
我的解决方案中的项目。
- 创建或添加我的
.resx
文件到这个项目。
- Add a
Post-Build Event
to the 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
如果您不想使用资源生成器转换你的.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
。您仍然需要使用汇编链接器.
编辑:正如 wRAR 提醒我们的那样,如果您正在签署您的程序集您的密钥必须匹配.