Click here to Skip to main content
15,883,531 members
Articles / Web Development / ASP.NET

Gender Aware Localization in ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
21 Apr 2009CPOL2 min read 29.4K   94   10   7
Implementing localization which depends on the gender of the registered user (ASP.NET).

Introduction

A while ago, I needed to implement localization depending also on the gender of the registered user, something obviously more relevant to languages other than English (for example, mine - Hebrew). After playing around a bit with writing my own custom ResourceManager/Provider classes, I decided to follow a suggestion I received for a much simpler way - create my own custom cultures for each required language.

Step 1 - Create your own custom culture

For each language required, create a custom "female" culture as a clone of the original culture. For example, the following code creates a custom culture named 'he-IL-f' which is a clone of 'he-IL'. Install the culture on your machine. You can verify the installation was successful by viewing your “C:\Windows\Globalization” folder after running the exe.

C#
CultureAndRegionInfoBuilder cib = 
  new CultureAndRegionInfoBuilder("he-IL-f", CultureAndRegionModifiers.None);
// Populate the new CultureAndRegionInfoBuilder object with culture information.
CultureInfo ci = new CultureInfo("he-IL");
cib.LoadDataFromCultureInfo(ci);

// Populate the new CultureAndRegionInfoBuilder object with region information.
RegionInfo ri = new RegionInfo("IL");
cib.LoadDataFromRegionInfo(ri);

cib.Parent = ci;

//CultureAndRegionInfoBuilder.Unregister("he-IL-f");
cib.Register();

* The above code needs admin rights to execute, so if in Vista, run the exe “as admin”.

The fallback culture of the new culture will be the original one – this is why we have this line:

C#
cib.Parent = ci;

This enables us to only enter the strings that need changing in the "female" RESX files.

Step 2 - Create the correspondingly names RESX files

Notice that in the “female” RESX files, you only need to insert those strings which differ from their “male” version.

Step 3 - Set the thread's UICulture property

C#
protected override void InitializeCulture()
{
    string culture = "";

    if (Request.Form["DropDownListCulture"] != null)
         {
                 string gender = Request.Form["DropDownListGender"];
        bool isFemale = (gender == "Female");

        culture = Request.Form["DropDownListCulture"];
        dir = (culture == "he-IL") ? "rtl" : "ltr";

        if (isFemale)
        {
            culture += "-f";
        }
    }

    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
    base.InitializeCulture();
}

In my example I used the InitializeCulture event of the page to perform the logic. In more complex systems, we’d probably want to move this logic to a more centralized location (perhaps some event in the Application level). I receive two parameters from the form – language and gender. Of course, in a real application, we would have this information per user and use it automatically upon login.

Using the code / Points of interest

  1. In the source code supplied, we register three custom cultures: en-US-f, he-IL-f, and fr-FR-f.
  2. You can verify this by checking your “C:\Windows\Globalization” folder after running the exe.

  3. Note that the French culture was registered without this line of code: cib.Parent = ci;.
  4. This means that instead of falling back to fr-FR, it falls back to the default culture (en-US, in our case).

    This is demonstrated when using the global resource ‘Talya’ (at the bottom of the page): its default in English is ‘talya’, and it is overridden in the he-IL and fr-FR cultures, but not in their female versions.

    When viewed with the he-IL-f culture, it falls back to the Hebrew resource ‘טליה’. However, when viewed with the fr-FR-f culture instead of falling back to ‘talya (French)’, it falls back to the English ‘talya’. This is because we didn’t set the French female culture’s parent to the French culture.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralJust curious Pin
Johnny J.21-Apr-09 21:00
professionalJohnny J.21-Apr-09 21:00 
JokeRe: Just curious Pin
leonardas22-Apr-09 0:27
leonardas22-Apr-09 0:27 
GeneralRe: Just curious Pin
Johnny J.22-Apr-09 0:44
professionalJohnny J.22-Apr-09 0:44 
AnswerRe: Just curious Pin
Talya Gendler22-Apr-09 10:43
Talya Gendler22-Apr-09 10:43 
GeneralRe: Just curious Pin
Johnny J.22-Apr-09 20:29
professionalJohnny J.22-Apr-09 20:29 
GeneralRe: Just curious Pin
ProJee29-Apr-09 3:48
ProJee29-Apr-09 3:48 
GeneralRe: Just curious Pin
Talya Gendler29-Apr-09 22:45
Talya Gendler29-Apr-09 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.