簡體   English   中英

我們可以用c#在asp.net中創建枚舉多語言嗎

[英]Can we make enum multilingual in asp.net with c#

我正在使用多語言網站,並且我使用了一些枚舉,現在我們可以根據多語言制作這些枚舉?

我的枚舉結構是

public enum abc
{
  [Description{"multilingual text"}]
  StatucActive = 1
}

像這樣。 我想在描述中寫多語言文字。

你應該按照以下步驟:

(1)准備資源文件,例如resource.en-US.resx / resource.zh-CN.resx / resource.zh-CN.resx 每個資源文件都有鍵和值,它們的鍵在文件之間是相同的,值在語言上是不同的。

(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
}

不,我們不能使用枚舉作為多語言,但我有一個替代選項,使用資源文件,它在某些情況下像枚舉一樣工作。

請嘗試資源文件,它將解決您的問題....

翻譯枚舉的簡單方法是為您想要的每種語言創建一個值數組。

String language1 [] = {“value”,“value2”};

String language2 [] = {“other value”,“other value2”};

String multi = language2 [enumvalue];

您的枚舉值將成為您的字符串翻譯數組的索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM