在多语言应用程序中使用的类库中表示性别的最佳方式
我正在创建包含一些常用类(例如人员、地址等)的类库。该库将在多语言应用程序中使用,我正在寻找表示人的性别的最方便的方法。
理想情况下,我希望能够像这样编写代码:
Person person = new Person { Gender = Genders.Male,
FirstName = "Nice",
LastName = "Dude" }
if (person.Gender == Genders.Male)
Console.WriteLine("person is Male");
Console.WriteLine(person.Gender); //Should output: Male
Console.WriteLine(person.Gender.ToString("da-DK"));
//Should output the name of the gender in the language provided
List<Gender> genders = Genders.GetAll();
foreach(Gender gender in genders)
{
Console.WriteLine(gender.ToString());
Console.WriteLine(gender.ToString("da-DK"));
}
你会做什么?枚举和专门的性别类?那么本地化又如何呢?
编辑:我想指出,这个问题至少与如何编写一些能够编写上述代码的类一样多,我没有性别类或性别枚举,我试图弄清楚如何编写启用上述代码的代码。性别课程会是什么样子?您会使用枚举来表示性别吗?
问候
杰斯帕·豪格
I'm creating class library with some commonly used classes like persons, addresses etc. This library will be used in an multilingual application, and I am looking for the most convenient way to represent a persons gender.
Ideally I would like to be able to code like this:
Person person = new Person { Gender = Genders.Male,
FirstName = "Nice",
LastName = "Dude" }
if (person.Gender == Genders.Male)
Console.WriteLine("person is Male");
Console.WriteLine(person.Gender); //Should output: Male
Console.WriteLine(person.Gender.ToString("da-DK"));
//Should output the name of the gender in the language provided
List<Gender> genders = Genders.GetAll();
foreach(Gender gender in genders)
{
Console.WriteLine(gender.ToString());
Console.WriteLine(gender.ToString("da-DK"));
}
What would you do? An enumeration and a specialized Gender class? But what about the localization then?
Edit: I'd like to point out that this question is at least as much about how to code some classes that enables writing code as above, I don't have a Gender class or gender enumeration, I'm trying to figure out how to write code that would enable the above code. What would the Genders class look like? Would you use an enum for the Genders?
Regards
Jesper Hauge
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过使用资源字符串来解决本地化问题。
您可以为每个性别值创建一个资源字符串,并将其翻译为您支持的每种文化。
You can solve localization issues by using resource strings.
You can create a resource string for each Gender-value, and translate it for each culture that you support.
我不会使用枚举,可能会选择使用内部构造函数创建
Gender
类以及一个Genders
静态类来包含可用选项作为属性。这与 .NET 中使用的方法基本相同,例如用于System.Drawing.Color
及其关联的Colors
类。在我看来,如果你有一个类而不是一个枚举,那么处理全球化会更容易。
I would not use an enumeration and probably would choose to create the
Gender
class with an internal constructor and also aGenders
static class to contain the available options as properties. This would be basically the same approach that is used in .NET for example for theSystem.Drawing.Color
and it's associatedColors
class.It's easier to deal with globalization if you have a class instead of a enum in my opinion.
您的业务和数据类不应该关心您正在执行本地化的事实,因为本地化是(应该是)完全显示端的操作。准确使用你拥有的东西,因为它是可读且可维护的。
为了本地化这些值,.NET 已经有一个内置资源管理器,可以根据
CurrentUICulture
值自动选择适当的资源值。对于此特定场景,您可以使用枚举中的ToString()
值作为资源文件中的键。这将允许您为不同的文化创建不同的文件(按照惯例和资源管理器的期望),每个文件具有不同的"Male"
和"Female"
值>。http://msdn.microsoft.com/en-us/magazine/cc163609。 aspx
http://msdn.microsoft .com/en-us/library/aa309421%28VS.71%29.aspx
Your business and data classes should not care about the fact that you're doing localization, as localization is (should be) an entirely display-side operation. Use exactly what you have, as it's readable and maintainable.
To localize the values, .NET already has a built-in resource manager that automatically selects the appropriate resource value based upon the
CurrentUICulture
value. For this specific scenario, you could use theToString()
value from your enumeration as the key in the resource file. This would allow you to create different files for different cultures (as is the convention and what the resource manager expects), each with a different value for"Male"
and"Female"
.http://msdn.microsoft.com/en-us/magazine/cc163609.aspx
http://msdn.microsoft.com/en-us/library/aa309421%28VS.71%29.aspx
另一种选择是在枚举类型上创建扩展方法。例如:
然后你可以简单地写
而不是
Another option is to create an extension method on your enumerated type. For example:
And then you can simply write
instead of