creating custom CultureInfo for country, language combination
Asked Answered
W

3

13

I am working on a .net 4.5 application that needs to be mult lingual supporting multi cultures etc.

The following is sample list of Countries/Languages

  • Russia / Russian
  • Belgium / French
  • Belgium / Dutch

For all the above, there is a CultureInfo object that can be created based upon the above culture names

  • ru-RU
  • fr-BE
  • nl-BE

When the user enters the site, I set Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture to the culture created with the above names eg.

Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-BE", false)
Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-BE", false)

All the above names are valid culture names.

No however I need to added another culture name, language of English in Russia. But the problem is the code en-RU is not valid so when I create new CultureInfo("en-RU", false) I get a CultureNotFoundException as en-RU is not valid.

With this culture, I want the formatting etc of Russia around numbers, dates etc, but I want the language on the site to be English. Is it possbile to create custom CultureInfo objects for invalid culture names with the behaviour of the country (Russia)?

Wendiwendie answered 16/10, 2012 at 14:26 Comment(0)
A
8

Is it possbile to create custom CultureInfo objects for invalid culture name

Yes.

Make use of CultureAndRegionInfoBuilder to help create the custom CultureInfo. (NB. once you have code to create a culture, the settings to do this can be saved to XML and then loaded, eg. from a resource, which should need less code at runtime.)

An example project how to do that, plus downloadable code, can be found at CodeProject. An important note from the article is that, even though this class sits in the System.Globalization namespace, it is not available by default, quote:

We’ll start by referencing the assembly sysglobl.dll and add a using statement for the namespace System.Globalization.

Aquaplane answered 16/10, 2012 at 14:33 Comment(6)
Here's a good tutorial for doing this. It's written for .NET 2.0, so if you're using a newer version of .NET you might want to check if it's not deprecated.Purtenance
@Purtenance the link is dead, unfortunatelySparing
@Sam, sorry to hear that. here's the last archived version. Please be patient with the loading time :PPurtenance
What looks like the same article can be found here, too. Apparently, it was taken from a chapter of a book.Floridafloridia
CultureAndRegionInfoBuilder is not available in 4.6 anymore!?Gurias
@Gurias Note which assembly it is in: not included in project templates by default.Aquaplane
T
6

You may have to mix and mix and match - have a culture object for English which you use for English text but a culture object of ru-Ru (Russian) for numeric formatting.

Or even better create a custom culture combining the two

Create custom culture in ASP.NET

Takishatakken answered 16/10, 2012 at 14:30 Comment(1)
This answer was first and linked to an appropriate SO article. This deserves to be the answer.Seedbed
A
-1

You should use CultureInfo.InvariantCulture Property

The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo.InvariantCulture also retrieves an instance of the invariant culture. It can be used in almost any method in the System.Globalization namespace that requires a culture. The objects returned by properties such as CompareInfo, DateTimeFormat, and NumberFormat also reflect the string comparison and formatting conventions of the invariant culture.

CultureInfo.InvariantCulture Property

This is other option Custom Global Application Culture

Atavism answered 16/10, 2012 at 14:32 Comment(1)
You did not understand the question.Shutter

© 2022 - 2024 — McMap. All rights reserved.