在多语言应用程序中使用的类库中表示性别的最佳方式

发布于 2024-08-30 09:58:57 字数 859 浏览 5 评论 0原文

我正在创建包含一些常用类(例如人员、地址等)的类库。该库将在多语言应用程序中使用,我正在寻找表示人的性别的最方便的方法。

理想情况下,我希望能够像这样编写代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巾帼英雄 2024-09-06 09:58:58

您可以通过使用资源字符串来解决本地化问题。
您可以为每个性别值创建一个资源字符串,并将其翻译为您支持的每种文化。

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.

放肆 2024-09-06 09:58:58

我不会使用枚举,可能会选择使用内部构造函数创建 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 a Genders static class to contain the available options as properties. This would be basically the same approach that is used in .NET for example for the System.Drawing.Color and it's associated Colors class.

It's easier to deal with globalization if you have a class instead of a enum in my opinion.

安穩 2024-09-06 09:58:58

您的业​​务和数据类不应该关心您正在执行本地化的事实,因为本地化是(应该是)完全显示端的操作。准确使用你拥有的东西,因为它是可读且可维护的。

为了本地化这些值,.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 the ToString() 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

厌味 2024-09-06 09:58:58

另一种选择是在枚举类型上创建扩展方法。例如:

public static class GenderExtensions
{
    public static string ToLocalizedText(this Gender gender)
    {
         // Lookup Localized content based on state of 'gender'
         return "This person is male";
    }
}

然后你可以简单地写

gender.ToLocalizedText()

而不是

gender.ToString()

Another option is to create an extension method on your enumerated type. For example:

public static class GenderExtensions
{
    public static string ToLocalizedText(this Gender gender)
    {
         // Lookup Localized content based on state of 'gender'
         return "This person is male";
    }
}

And then you can simply write

gender.ToLocalizedText()

instead of

gender.ToString()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文