asp.netasp.net-mvcinternationalizationcultureinfo

ASP.NET MVC Registering custom culture and internationalization


I have an ASP.NET MVC 4 project and I've registered the custom culture in it because I want to have a client-specific translation.

I call the following helper method with parameters like RegisterCulture("en-GB-CustA", "English (Customer A)", "en-GB"); This call is done in Application_Start event handler of the MvcApplication : HttpApplication class.

private static void RegisterCulture(string cultureCode, string cultureName, string baseCultureCode)
{
    var ci = new CultureInfo(baseCultureCode);
    var ri = new RegionInfo(ci.Name);
    var builder = new CultureAndRegionInfoBuilder(cultureCode, CultureAndRegionModifiers.None);
    builder.LoadDataFromCultureInfo(ci);
    builder.LoadDataFromRegionInfo(ri);

    builder.CultureEnglishName = cultureName;
    builder.CultureNativeName = cultureName;

    try
    {
        builder.Register();
    }
    catch (InvalidOperationException)
    {

    }
}

The method is fairy simple, it basically creates new culture based on existing one and replaces it's name.

Now in my Global.asax just for the testing purposes I've put the following code to MvcApplication class to switch current thread for the custom one.

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    var ci = new CultureInfo("en-GB-CustA");

    Thread.CurrentThread.CurrentUICulture = ci;
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}

I've also included two resources files. One is called Test.resx which is for the default texts, the second one is Test.en-GB-CustA.resx. I've added a simple string resource there called Title with two different values.

Test.resx => "Hello World!"
Test.en-GB-CustA => "Hello from custom culture!"

I've also put on one of my view the code to display this title (I've added ViewRes as s CustomToolNamespace for both resource files for simplification).

@ViewRes.Test.Title

Unfortunatelly even though I've set the custom culture as descibed before I'm getting the detault "Hello world" value all the time. What am I missing here?


Solution

  • I ended up renaming the code form my custom culture so something like en-XX. I've also have to define both TwoLetterISOLanguageName and ThreeLetterISOLanguageName and it registered properly.