17
votes

I have client that wants to specifiy their own version of localized content for a subset of my string resources.

For simplicity here is basic example:
Lets say I have 2 localized strings (showing english content)
PageTitle="Hello World"
PageDescription="This is a more wordy version of Hello World!"

I wish to localize these so I have resource files.

  • Strings.resx (contains my English string)
  • Strings.fr-ca.resx (contains my French-Canadian strings)
  • Strings.fr-ca-clientX.resx (contains my strings for a Client whom is French-Canadian and therfore very picky;) - just joking)

Ideally "Strings.fr-ca-clientX" can specify only the strings they want to "override". In other words they may just wish to change the PageTitle and continue using the PageDescription from the "fr-ca" resource file.

So how do I go about this in .NET? Ideally I would just create the resx file and specify the culture in my "Web.config" and it should work...

<globalization uiCulture="fr-ca-clientX" culture="fr-ca-clientX" />

However, this does not work. "The tag contains an invalid value for the 'culture' attribute" is my first obsticle.

Thanks,
Justin

3
I wouldn't recommend this way of providing localization. It is far from easy, but for such demands I would probably create some Resource Facade which will be single point of decision on what strings to load at runtime.Paweł Dyda

3 Answers

2
votes
public void AddCustomCulture(string cultureName, string baseCulture)
    {
        var cultureBuilder = new CultureAndRegionInfoBuilder(cultureName, CultureAndRegionModifiers.None);

        cultureBuilder.LoadDataFromCultureInfo(new CultureInfo(baseCulture));

        var region = baseCulture.Substring(3, 2);

        cultureBuilder.LoadDataFromRegionInfo(new RegionInfo(region));

        cultureBuilder.Register();
    }
2
votes

You can create a new culture with the following code:

        //Get culture info based on Great Britain
        CultureInfo cultureInfo = new CultureInfo( "en-GB" );
        RegionInfo regionInfo = new RegionInfo( cultureInfo.Name );

        CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder( txtCultureName.Text, CultureAndRegionModifiers.None );

        cultureAndRegionInfoBuilder.LoadDataFromCultureInfo( cultureInfo );
        cultureAndRegionInfoBuilder.LoadDataFromRegionInfo( regionInfo );

        // Custom Changes
        cultureAndRegionInfoBuilder.CultureEnglishName = txtCultureName.Text;
        cultureAndRegionInfoBuilder.CultureNativeName = txtNativeName.Text;

        cultureAndRegionInfoBuilder.Register();

I have written a post on creating an app to do just that..

http://wraithnath.blogspot.com/search/label/Globalization

1
votes

You probably need to create your own culture and register it. You'll find MSDN article on that topic here.

You don't need to alter culture attribute, it should stay at "fr-CA", as uiCulture attribute is responsible for loading strings from resources.