我在多语言网站上工作,在其中我使用了一些枚举,现在我们可以使这些枚举按照多语言?
我的枚举结构是
public enum abc
{
[Description{"multilingual text"}]
StatucActive = 1
}就像这样。我想在描述中写多语言文本。
发布于 2011-08-11 00:02:18
不,我们不能使用枚举作为多语言,但我有一个替代的选择,那就是使用资源文件,它在某些情况下像枚举一样工作。
请尝试资源文件,它将解决您的问题...
发布于 2011-08-04 12:55:03
您应该遵循以下步骤:
(1)准备resource.en-US.resx/resource.zh-CN.resx/etc等资源文件,每个资源文件都有键和值,文件之间键相同,语言不同值不同。
(2)定义您自己的DescriptionAttribute,如下所示:
public class LocalDescriptionAttribute : DescriptionAttribute
{
public string ResourceKey { get; set; }
public string CultureCode { get; set; }
//you can set a default value of CultureCode
//so that you needn't set it everywhere
public override string Description
{
get
{
//core of this attribute
//first find the corresponding resource file by CultureCode
//and then get the description text by the ResourceKey
}
}
}用法:
public enum MyTexts
{
[LocalDescription(CultureCode="zh-CN", ResourceKey="Title")]
Title = 0,
[LocalDescription(ResourceKey="Status")] //default CultureCode
Status = 1
}发布于 2011-08-04 12:18:07
翻译枚举的一种简单方法是为所需的每种语言创建一个值数组。
String language1[] = {"value","value2"};
String language2[] ={“其他值”,“其他value2"};
String multi = language2enumvalue;
枚举值成为翻译字符串数组的索引。
https://stackoverflow.com/questions/6936023
复制相似问题