如何在类中本地化枚举值 [英] How to localize enum values in the class

查看:83
本文介绍了如何在类中本地化枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends,

我的代码中遇到一个问题。我将在下面解释我的场景: -

我有枚举值,如

public enum myValues

{

0 - 已导入

1 - 已导出

}



在我的代码中,我将枚举值与我的比较'输入'值。



if(input.Contains(myValues.Imported))

{

//我的IF循环得到了在我的代码逻辑中处理.....

}



观察到的问题:

虽然我在比较中myValues中的英语语言包括Imported或Exported,它的工作正常,它进入if循环,我的逻辑得到处理。



但是当我改变语言时它不会不行。例如,我已将操作系统语言从英语更改为意大利语。我的输入值来自importato('意大利语''导入')或esportato(意大利语'导出')。在这种情况下,我们只比较了导入和导出的枚举值,因此if循环变为false并且移动到else情况。



我请求大家请给我建议如何处理Enum值的本地化。



我希望你能更好地理解我的情景。



请帮帮我!!!!在此先感谢......



我尝试过:



我尝试使用文化来比较目前正在使用的语言。但是我无法实现它。

Hello Friends,
I have facing one issue in my code. I will explain below of my scenarios :-
I have enum values like
public enum myValues
{
0 - Imported
1 - Exported
}

In my code I have compared the enum values with my 'input' value.

if (input.Contains(myValues.Imported))
{
//My IF Loop got processing in my code logic.....
}

Issue observed:
While I compared in English languvage in myValues consists of either Imported or Exported and its working fine and it enters to if loop and my logic got processing.

But When I change the language it doesn't works. For instance, I have changed the OS language from English to Italian. My input values comes importato ('Imported' in Italian language) or esportato ('Exported' in Italian language). In that case we have compared the enum values only Imported and Exported so the if loop got false and it moves to else case.

I request you all please give me suggestion how to handle the localization for Enum values.

I hope you understand better on my scenario.

Please help me!!!! Thanks in advance......

What I have tried:

I have tried using culture to compared which language currently is being worked. But I could not able to achieve it.

推荐答案

我认为这里有两个问题:



1.全球化的本地化问题,这是您需要在整个应用程序范围内实施的策略。通常,这将涉及将依赖于语言的字符串存储为资源,然后,根据用户的系统范围文化设置,确保适当的字符串(或其他项目,如文化相关的小数点分隔符等)使用[ ^ ]。



2.验证给定字符串的问题映射到'Enum:
I think there are two issues here:

1. the "global" issue of "localisation," which is a strategy you need to implement on an application-wide basis. typically, this will involve storing language-dependent strings as resources, and then, depending on the user's system-wide "culture settings," making sure the appropriate strings (or other items, like culture dependent decimal-point separators, etc.) are used [^].

2. the issue of validating a given string maps to a specific Enumeration value in an 'Enum:
public enum TestEnum
{
    NoValue = 0,
    one,two,three
}

public TestEnum StringToTestEnum(string input, bool ignoreCase = false)
{
    TestEnum result;
    TestEnum.TryParse(input, ignoreCase, out result);
    return result;
}

这里使用特殊的枚举值NoValue可以区分不能用给定的字符串进行转换。



我认为你需要做一些关于你的应用程序未来可能需要支持的语言数量的战略决策,以及是否会使用非罗马字符集。 Web上有许多编写良好的资源,在这里,在CodeProject上,处理语言本地化问题,如:[ ^ ],[ ^ ],[ ^ ]。



imho,处理如何使代码接受变量名称等,只用两种语言,对于应用程序中的某些对象来说是浪费时间。



但是,有一些方法可以差别编译你的代码库,包括使用#define / #Undef / #If修饰符,使用MEF:[ ^ ]在运行时根据变量等加载代码。您可以使用类的ConditionalAttributes,以及返回Type为'void的方法:[ ^ ]。

Here the use of a special Enumeration value "NoValue" allows you to distinguish that no conversion is possible with a given string.

I think you need to make some strategic decisions about the number of languages your application may need to support in the future, and if non-Roman character sets will be used. There are many well-written resources on the web, and here, on CodeProject, dealing with language localisation issues, like: [^], [^], [^].

imho, dealing with how to make your code accept variable names, etc., in only two languages, for only certain objects in your application is a waste of time.

However, there are ways to differentially compile your code base, including use of #Define/#Undef/#If modifiers, using MEF: [^] to load code at run-time based on variables, etc. You can use ConditionalAttributes on Classes, and on methods whose return Type is 'void: [^].


你不能。这些名称只能在你的代码中使用。



你必须为每个值提供一些属性,例如DescriptionAttribute [ ^ ]。您可以找到有关如何本地化说明的示例此处 [< a href =https://www.google.com/?ion=1&espv=2#q=description%20attribute%20localizationtarget =_ blanktitle =New Window> ^ ]。
You can't. Those names are to be used only inside your code.

You'd have to supply some attribute to each value, such as DescriptionAttribute[^]. You can find examples of how to localize the Description here[^].


首先,您的比较需要认真改进。进行任何字符串比较都非常糟糕,特别是重复。这应该会给你更好的想法:

First of all, your comparison needs serious improvement. It's really bad to do any string comparisons, especially repeatedly. This should give you much better idea:
static myValues? ParseMyValues(string input) {
    try {
        return (myValues)System.Enum.Parse(typeof(myValues), input);
    } catch { return null; }
}



我使用'?'来使用 nullable type ,因为解析可能会失败。如果 input 等于枚举值的有效名称之一,这是枚举类型的静态字段,与声明它们完全相同,则会成功。敏感。在所有其他情况下,将抛出异常,我建议捕获并表示此解析为null的失败。现在,您可以将枚举值与同一类的枚举值进行比较,将null作为无效输入单独处理。



要特别注意具有相同基础整数值的成员的枚举类型。在我的简单示例中,它们不会破坏执行,但您应该更好地了解问题,我在文章 枚举类型不枚举!解决.NET和语言限制



现在,如果要本地化解决方案该怎么办?首先,您不应该本地化和编程标识符。众所周知的本地化方法基于资源。 进口应保持进口。如果你想要Importato,它应该只是 importato 只能输入资源并由用户输入。那么,如何进行比较呢?



解决方案1提出了一个具有属性的想法,但此解决方案并不符合全球化/本地化技术。在我的枚举系列的另一篇文章中,我也使用了属性,但是以更高级的方式;其中一个属性将表示值重定向到属性定义的资源。您可以在本文中找到全面的解释和源代码: 人类可读的枚举元数据



-SA


I used nullable type by using '?', because parsing can fail. It will be successful if input is equal to one of the valid names of the enumeration values, which are the static fields of your enumeration type, exactly as they are declared, case-sensitive. In all other cases an exception will be thrown, which I suggested to catch and represent this failure to parse as null. Now you can compare enumeration values with enumeration values of the same class, processing null as invalid input separately.

Be extra careful with enumeration types having members with identical underlying integer values. In my simple example, they won't spoil the execution, but you should better be aware of the problem, which is described and solved in my article Enumeration Types do not Enumerate! Working around .NET and Language Limitations.

Now, what to do if you want to localize the solution? First of all, you should not localize and program identifiers. Well-known localization methods are based on resources. "Imported" should remain "Imported". If you want "Importato", it should be "importato" only into to resources and entered by the users. So, how to do the comparison?

One idea is suggested by Solution 1, with attribute, but this solution is not inline with the globalization/localization technology. In my other article from the enumeration series, I also use attribute, but in more advanced way; and one of the attributes redirects the representation values to a resource defined by the attribute. You can find the comprehensive explanation and source code in this article: Human-readable Enumeration Meta-data.

—SA


这篇关于如何在类中本地化枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆