0

I have a website with a page called buystuff.aspx. I've created two local resources: buystuff.aspx.resx and buystuff.aspx.da-dk.resx.

This works fine, and if I enter the site with a da-DK setting, I get that version, and if I enter with anything else, I get the default.

However, what i want, is to set this programmatically. When the user enters buystuff.aspx they should be forced into the english (en-US) version, and if they enter buystuff.aspx?language=da, they should be forced into the da-dk one.

The following code doesn't do the trick:

private void SetupLanguage()
{
    if (!string.IsNullOrEmpty(CurrentLanguage))
    {
        if (CurrentLanguage == "da")
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
            return;
        }
    }
    Culture = "en-US";
    UICulture = "en-US";
}

I have also tried the following which didn't work:

private void SetupLanguage()
{
    if (!string.IsNullOrEmpty(CurrentLanguage))
    {
        if (CurrentLanguage == "da")
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
            return;
        }
    }
    CultureInfo info = CultureInfo.CreateSpecificCulture("en-US");
    Thread.CurrentThread.CurrentUICulture = info;
    Thread.CurrentThread.CurrentCulture = info;
}

In the debug mode, I can tell the code is run as it should. However, when loading buybtc.aspx (and my CurrentLanguage variable is string.empty), it still loads resources from buystuff.aspx.da-dk.resx.

Any ideas?

Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182
  • Why are you only setting `CurrentUICulture` in one place? – Oded Apr 26 '13 at 18:39
  • @Oded , what do you mean? :-) – Lars Holdgaard Apr 26 '13 at 18:56
  • In your second code sample, you have `Thread.CurrentThread.CurrentUICulture = info;`. You never set `CurrentUICulture` anywhere else. You should be setting it (and `CurrentCulture` to the wanted culture at the same time. – Oded Apr 26 '13 at 19:02
  • I might be stupid or slow, but why should you set CurrentUICulture several times? Isn't once in Page_Load enough? I don't think I get it, could you ellaborate? :) – Lars Holdgaard Apr 26 '13 at 19:53

1 Answers1

0

Ok, solved it myself (gone are those hours - forever ;) ).

The problem was in my web.config, where I had several rules for SEO reasons:

 <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
          <match url="*"/>
          <conditions>
            <add input="{HTTP_HOST}" pattern="btcglobe.com"/>
          </conditions>
          <action type="Redirect" url="http://www.btcglobe.com/{R:0}"/>
        </rule>
        <!--To always remove trailing slash from the URL-->
        <rule name="Remove trailing slash" stopProcessing="true">
          <match url="(.*)/$"/>
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="{R:1}"/>
        </rule>
        <rule name="LowerCaseRule1" stopProcessing="true">
          <match url="[A-Z]" ignoreCase="false"/>
          <conditions>
            <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml)|(asmx))$" ignoreCase="true" negate="true"/>
          </conditions>
          <action type="Redirect" url="{ToLower:{URL}}"/>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

I also had specified my culture in the web.config, so no matter what I did, it was always the same culture.

So the solution:

  • I removed any globalization info from the web.config
  • I made sure everything was in lowercase, also my new CultureInfo("da-dk");

My code behind is now like this:

private void SetupLanguage()
{
    if (!string.IsNullOrEmpty(CurrentLanguage))
    {
        if (CurrentLanguage == "da")
        {
            CultureInfo dkinfo = CultureInfo.CreateSpecificCulture("da-dk");
            CultureInfo.DefaultThreadCurrentCulture = dkinfo;
            CultureInfo.DefaultThreadCurrentUICulture = dkinfo;
            Thread.CurrentThread.CurrentCulture = dkinfo;
            Thread.CurrentThread.CurrentUICulture = dkinfo;

            return;
        }
    }
    CultureInfo info = CultureInfo.CreateSpecificCulture("en-us");
    CultureInfo.DefaultThreadCurrentCulture = info;
    CultureInfo.DefaultThreadCurrentUICulture = info;
    Thread.CurrentThread.CurrentCulture = info;
    Thread.CurrentThread.CurrentUICulture = info;

}
Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182