Click here to Skip to main content
Click here to Skip to main content

Gender Aware Localization in ASP.NET

, 21 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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:

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

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)

Share

About the Author

Talya Gendler
Software Developer
Israel Israel
No Biography provided

Comments and Discussions

 
GeneralJust curious PinmemberJohnny J.21-Apr-09 21:00 
I'm by no means questioning your work or your article.
 
I'm just wondering: What use could you have for having a "male" and a "female" culture? I mean: What in an application would be different depending on gender? Can you give an example?
 
Cheers,
Johnny J.
JokeRe: Just curious Pinmemberleonardas22-Apr-09 0:27 
GeneralRe: Just curious PinmemberJohnny J.22-Apr-09 0:44 
AnswerRe: Just curious PinmemberTalya Gendler22-Apr-09 10:43 
GeneralRe: Just curious PinmemberJohnny J.22-Apr-09 20:29 
GeneralRe: Just curious PinmemberProJee29-Apr-09 3:48 
GeneralRe: Just curious PinmemberTalya Gendler29-Apr-09 22:45 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 21 Apr 2009
Article Copyright 2009 by Talya Gendler
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid