如何在C#中表示国家和语言?

发布于 2024-08-16 22:57:57 字数 497 浏览 4 评论 0原文

我将从 xml 中检索此数据,以便为数千个对象初始化它。

因此,如果 MyObject 具有 CountryLanguage 属性,那么它们应该是什么,以及它们应该如何在代码和 xml 中表示。

我正在考虑在代码中使用 Enum

我只是在寻找其他人的意见来找到最好的方法来做到这一点。 BCL 中是否包含所有语言和国家/地区,以便我可以使用 min 而不必编写一个?

另外,CountryLanguage 不应组合使用。 语言不会代表指定国家/地区所使用的语言。

如何在代码和 Xml 中最好地实现这一点。

我应该这样做吗?:

<Language>English</Language>

I will retrieve this data from an xml to initialize it for thousands of objects.

So if MyObject has a Country and Language property, what should they be, and how should they be represented both in code and in xml.

I am thinking of using an Enum in code.

I am just looking for other people's opinions to find the best way to do this. Is all Languages and Countries even in the BCL so I can use the minstead of writing one?

Also Country and Language shouldn't be combined. The Language will not represent the language spoken in the specified Country.

How to best implement this both for code and in the Xml.

Should I do it like?:

<Language>English</Language>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

╰沐子 2024-08-23 22:57:57

考虑查看 CultureInfo 接受字符串的构造函数 作为示例(例如“ en-us”)

提供有关特定的信息
文化(称为“语言环境”
非托管代码开发)。这
信息包括名称
文化、书写系统、
使用的日历和格式
日期并对字符串进行排序。

它对于现实世界中实际使用的内容相当具体(例如,国家/语言对有意义)。您还可以创建自己的文化

您指定该语言可能不是该国家/地区使用的语言。您能具体说明一下您将使用它做什么吗?没有更多信息,您似乎正在尝试定义类似于 文化信息

或者,您还可以定义一个简单的对象,它只具有两个属性(国家/地区和语言),其中国家/地区是 ISO 3166-2 字符串和语言是 ISO 639-1 字符串 正如爱德华·洛珀(Edward Loper)建议如下

您可以将 ISO 代码列表存储在 XML 文件中,并使用 传统技术。我建议 CultureInfo,因为您指定您正在寻找 BCL 中已有的内容。

一般来说,框架设计指南不鼓励使用枚举开集:

不要对开集使用枚举(例如
作为操作系统版本、名称
您的朋友等)。

您可以采用混合方法,定义两个静态类,其中包含一堆静态 readonly 字符串,如

// ISO-3166-2 codes
public static Countries
{
   public static readonly string France = "FR";
   ...
}

// CultureInfo style codes
public static Languages
{
   public static readonly string BritishEnglish = "en-GB";
}

更新: 根据您的评论,这是专门针对电影的,您仍然可以使用 CultureInfo 来表示它的制作文化和文化的内容。除此之外的任何事情对于微软来说都可能过于政治化而无法参与使其成为操作系统的一部分(参见 这个)。因此,您必须定义自己的。

Consider looking at CultureInfo's constructor that takes a string for examples (e.g. "en-us")

Provides information about a specific
culture (called a "locale" for
unmanaged code development). The
information includes the names for the
culture, the writing system, the
calendar used, and formatting for
dates and sort strings.

It's fairly specific to what's actually out there being used in the real world (e.g. the country/language pairs make sense). You can also create your own cultures.

You specified that the language might not be the one spoken in the country. Could you clarify what exactly you'll use it for? Without further information, it seems like you're trying to define something similar to a CultureInfo.

Alternatively, you could also define a simple object that just had the two properties (Country and Language) where Country is an ISO 3166-2 string and Language is an ISO 639-1 string as Edward Loper suggests below.

You could store a list of the ISO codes in an XML file and parse them using traditional techniques. I suggested CultureInfo because you specified that you were looking for something already in the BCL.

Using enums is, in general, discouraged by the Framework Design Guidelines for open sets:

DO NOT use an enum for open sets (such
as the operating system version, names
of your friends, etc.).

You could take a hybrid approach where you define two static class that has a bunch of static readonly strings as in

// ISO-3166-2 codes
public static Countries
{
   public static readonly string France = "FR";
   ...
}

// CultureInfo style codes
public static Languages
{
   public static readonly string BritishEnglish = "en-GB";
}

UPDATE: Based on your comment that this is specifically for movies, you could still use a CultureInfo for the culture where it was produced and the culture of the content. Anything beyond that is probably too political for Microsoft to be involved with making it part of the OS (c.f. this). Thus, you'd have to define your own.

凉薄对峙 2024-08-23 22:57:57

ISO 639-2(语言名称表示代码)提供了相当完整的人类语言列表,以及可用于这些语言的标准代码:

http://www.loc.gov/standards/iso639-2/php/code_list.php

类似地,ISO 3166-1 给出所有国家/地区的标准代码:

http://en.wikipedia.org/wiki/ISO_3166-1< /a>

ISO 639-2 (Codes for the representation of names of languages) gives a fairly complete list of human languages, along with standard codes you can use for those languages:

http://www.loc.gov/standards/iso639-2/php/code_list.php

Similarly, ISO 3166-1 gives standard codes for all countries:

http://en.wikipedia.org/wiki/ISO_3166-1

南街九尾狐 2024-08-23 22:57:57

我将使用预构建的序列化库从 xml 保存和检索(例如 本教程 )。另外,我会避免使用枚举,而只使用字符串或 CultureInfo 之类的东西。

I would save and retrieve from xml using a prebuilt serialization library (e.g. this tutorial). Also, I would avoid using an enum and simply use strings or something like CultureInfo.

你与清晨阳光 2024-08-23 22:57:57

在这种情况下,您可以使用元素的 xml:lang 属性,如下所述:http://docs.oasis-open.org/dita/v1.1/OS/archspec/xmllang.html

正如它所建议的,您然后使用“country-LANGUAGE " 区域设置的标识符。这可用于在反序列化时创建 CultureInfo 对象,即我将在代码中使用 CultureInfo 对象。

This might be the case where you can use the xml:lang attribute of an element as described here: http://docs.oasis-open.org/dita/v1.1/OS/archspec/xmllang.html

As it suggests, you then use the "country-LANGUAGE" identifiers for locales. This can be used to create a CultureInfo object when deserializing, i.e. I would use CultureInfo objects in my code.

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