开发者

How do you handle static list internationalization in 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...

Countr开发者_如何学Pythonies are not (currently) a table in my database but I am willing to change this although Im not sure whats the best idea.

for instance, in spanish Germany is Alemania.

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

How can I do this? please help.

edit for clarification:

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

  • 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.


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 has few interesting properties:

  • 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.

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).

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;
}

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


The user selects their region, you store it in their session or the URL using a route to contain the region code. You can read it in an action filter such as done here:

ASP.Net MVC switching Cultures after compile for initial load

If you choose the route method

ASP.NET (MVC) routes internationalization

but that seems to be a bit more work for your scenario, so the first link will be your best bet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜