0

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.

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.

nacho10f
  • 5,816
  • 6
  • 42
  • 73
  • The question is why you want to do this? Why you need this information in the first place? Another question would be why you need to force users to choose country if that (usually) could be detected from web browser? – Paweł Dyda Jul 13 '11 at 06:19
  • I will edit for clarification – nacho10f Jul 13 '11 at 15:11

2 Answers2

1

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

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79
  • I hope C# is OK for you since you forgot to select language. You could surely get more answers just by tagging questions correctly... – Paweł Dyda Jul 13 '11 at 20:01
  • I just saw that it is Asp.Net-MVC question... Well, I believe you will still be able to use this solution (maybe with slight modifications). – Paweł Dyda Jul 13 '11 at 20:03
0

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.

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71