简体   繁体   English

我们可以用c#在asp.net中创建枚举多语言吗

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

I am working on multilingual site and in which i used some enum and now it can be possible that we can make theses enum as per multilingual? 我正在使用多语言网站,并且我使用了一些枚举,现在我们可以根据多语言制作这些枚举?

My enum structure is 我的枚举结构是

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

like this. 像这样。 i want to write multilingual text in description. 我想在描述中写多语言文字。

You should follow the steps: 你应该按照以下步骤:

(1) prepare the resource files eg resource.en-US.resx / resource.zh-CN.resx /etc. (1)准备资源文件,例如resource.en-US.resx / resource.zh-CN.resx / resource.zh-CN.resx Each resource file has keys and values, their keys are the same between files, values are different in languages. 每个资源文件都有键和值,它们的键在文件之间是相同的,值在语言上是不同的。

(2) define your own DescriptionAttribute , something like this: (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
        }
    }
}

The usage: 用法:

public enum MyTexts
{
    [LocalDescription(CultureCode="zh-CN", ResourceKey="Title")]
    Title = 0,
    [LocalDescription(ResourceKey="Status")]   //default CultureCode
    Status = 1
}

No we can't use enum as a multilingual but i have an alternate option that is use resource file it working like an enum in some situations. 不,我们不能使用枚举作为多语言,但我有一个替代选项,使用资源文件,它在某些情况下像枚举一样工作。

please try resource file and it will solve your problem.... 请尝试资源文件,它将解决您的问题....

An easy way to translate an enumerated would be to create an array of values for each language you want. 翻译枚举的简单方法是为您想要的每种语言创建一个值数组。

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

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

String multi = language2[enumvalue]; String multi = language2 [enumvalue];

Your enum value become the index to your string array of translations. 您的枚举值将成为您的字符串翻译数组的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 asp.net 中制作多语言网站 - how to make multilingual site in asp.net 您可以在ASP.net文本框中创建“分隔符”吗? (C#) - Can you make a “separator” in ASP.net Textboxes? (C#) 如何使用c#枚举在ASP.Net MVC中创建一组多选复选框? - How do I make a group of multi-select checkboxes in ASP.Net MVC with c# enum? 如何使用Asp.net 3.5和C#3.0创建动态多语言Web应用程序 - How to create dynamic multilingual web application using Asp.net 3.5 and c# 3.0 如何将枚举列表显示为字符串C#ASP.NET - How to display enum list into a string C# ASP.NET ASP.NET中的多语言网站 - Multilingual website in ASP.NET 我们可以在asp.net c#web应用程序中嵌入twitter,linkedin,facebook,buzz的授权吗? - Can we embed authorization for twitter, linkedin, facebook, buzz in asp.net c# web application? 我们可以使用 asp.net 和 c# 从本地主机发送邮件吗? - Can we send mails from localhost using asp.net and c#? 我们可以使用 C# 和 Z9E0DA8438E1E38A1Cweb 方法在 static 中调用 Page_Load 事件吗? - Can we call Page_Load event in static web method using C# and ASP.NET 我们如何在没有 ASP.NET 的情况下使用 C# 和 JS 框架来开发单页应用程序? - How can we use C# without ASP.NET with a JS framework for developing Single Page Applications?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM