您如何在ASP.NET MVC中处理静态列表的国际化? [英] How do you handle static list internationalization in asp.net mvc?

查看:112
本文介绍了您如何在ASP.NET MVC中处理静态列表的国际化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想让我的应用程序完全国际化...并且应该让用户以表格的形式从国家/地区列表(下拉列表)中进行选择...

Say I want my app to be completetely internationalized... and a user is supposed to select from a list (dropdown) of countries in a form...

国家/地区(目前)不是我数据库中的表格,但是我不确定要更改什么主意,但我愿意更改它.

Countries are not (currently) a table in my database but I am willing to change this although Im not sure whats the best idea.

例如,在西班牙,德国是Alemania.

for instance, in spanish Germany is Alemania.

许多国家/地区使用不同的名称,因此需要使用不同的语言.

many countries have different names so this needs to be in different languages.

我该怎么做?请帮忙.

为澄清而进行的

请记住,该国家/地区下拉列表与语言切换机制无关...我们的应用程序应该跟踪用户的出生地地址....所以我想要的是取决于当前选定的文化,将显示不同的波西尔布国家列表....重申我的例子

Keep in mind that this country dropdown has NOTHING to with the language switching mechanism... Our app is supposed to keep track of the birth place address of the users.... so what i want is that depending on the current selected culture a different list of possilbe countries will be shown.... to reiterate my example

  • 出生于德国并使用英语的应用程序的用户将 从下拉菜单中选择德国

  • A user who was born in Germany and is using the app in english will select Germany from the dropdwon

出生于德国且正在使用西班牙语的应用程序的用户将 从下拉列表中选择Alemania.

A user who was born in Germany and is using the app in spanish will select Alemania from the dropdown.

语言开关工作正常.任何用户都可以选择任何一种语言来显示应用程序,并且该语言将存储在网址中.

The language switch is working just fine. Any user can select any language to display the app on and that will be stored on the url.

推荐答案

要获得或多或少受.Net支持的国家/地区列表,您可以像下面这样使用CultureInfo和RegionInfo:

To obtain a list of countries more or less supported by .Net you can use CultureInfo and RegionInfo like this:

List<RegionInfo> allRegions = new List<RegionInfo>();
var specificCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in specificCultures)
{
    var info = new RegionInfo(culture.LCID);
    allRegions.Add(info);
}

这将为您提供所有国家/地区,其中有些国家会多次使用(因为使用了不同的语言).

This will give you all the countries, some of them multiple times (because different languages are in use).

RegionInfo具有一些有趣的属性:

RegionInfo has few interesting properties:

  • NativeName,将为您提供国家的翻译名称,但不幸的是,该名称对应于给定的语言环境标识符(LCID),即波兰语为"Polska",德国语为"Deutschland",依此类推;有些将被翻译成几种语言(这很有趣,因为美国既可以作为美国也可以作为Estados Unidos看到)
  • DisplayName您要找的东西应该是什么,但不幸的是,不是–微软忘记了"在.Net框架中进行翻译(也许可以,因为它不应该是Property)
  • 与名称不同的名称将为您提供两个字母的国家/地区代码.
  • NativeName that will give you translated name of the country but unfortunately one corresponding to given Locale Identifier (LCID), that is "Polska" for Poland, "Deutschland" for Germany and so on; some will be translated into few languages (that's interesting, because USA is visible both as United States and as Estados Unidos)
  • DisplayName what should be what you are looking for but unfortunately is not – Microsoft "forgot" to translate it in the .Net framework (maybe it is OK, for it should not be the Property then)
  • Name which unlike name suggests will give you two-letter country code.

那么您可以使用此信息做什么?从理论上讲,您可以使用翻译后的国家/地区名称-在这种情况下,您只需创建一个词典(Dictionary<int, string>)并添加具有相应NativeName字符串的LCID并将其用作下拉菜单的来源.
从理论上讲,在该国出生的人应该能够理解其至少一种语言(至少在大多数情况下会发生这种情况).

So what you can do with this information? Theoretically you can use translated countries names – in this case you would just create a dictionary (Dictionary<int, string>) and add LCID with corresponding NativeName string and use it as a source for your Drop down menu.
In theory one born in the country should be able to understand at least one of its languages (at least that happens most of the times).

但是,实际上,您可能希望将唯一的国家/地区列表翻译成您的应用程序当前所显示的语言.您可以使用上述方法获取国家/地区列表,并使用例如DisplayName(或EnglishName).在运行时,您将像其他字符串一样将其解析为翻译后的名称.由于这需要在后端进行,因此您将添加另一个资源文件(可以放置在App_GlobalResources中,没有关系),并在后面的代码中读取它.不再需要理论,就需要一些代码示例:

In reality though, you probably want to have unique list of countries translated into whatever language your application is displaying at the moment. You can use the method above to obtain list of countries and use for example DisplayName (or EnglishName). At runtime you would resolve it to translated name just like any other string. Since that need to happen on the backend, you would add another resource file (could be placed in App_GlobalResources, does not matter) and read it in your code-behind. No more theory, some code sample is required:

const string RESOURCE_FILE = "Countries";
Dictionary<string, string> countryNames = new Dictionary<string, string>();

var specificCultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo culture in specificCultures)
{
    var info = new RegionInfo(culture.LCID);
    var name = info.EnglishName;
    var translated = GetGlobalResourceObject(RESOURCE_FILE, name).ToString();
    countryNames[name] = translated;
}

如果要使用特定语言(CurrentUICulture以外的语言)读取名称,请将CultureInfo对象作为第三个参数传递给GetGlobalResourceObject().

If you want to read the name in a specific language (other than CurrentUICulture) pass CultureInfo object as a third parameter to GetGlobalResourceObject().

这篇关于您如何在ASP.NET MVC中处理静态列表的国际化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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