0

我有一个网站,其页面名为buystuff.aspx。我创建了两个本地资源:buystuff.aspx.resxbuystuff.aspx.da-dk.resx

这很好用,如果我使用 da-DK 设置进入网站,我会得到那个版本,如果我输入其他任何东西,我会得到默认值。

但是,我想要的是以编程方式设置它。当用户输入 buystuff.aspx 时,他们应该被强制进入英语(en-US)版本,如果他们输入 buystuff.aspx?language=da,他们应该被强制进入 da-dk 版本。

以下代码不能解决问题:

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

我还尝试了以下方法,但没有奏效:

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;
}

在调试模式下,我可以告诉代码按应有的方式运行。但是,当加载buybtc.aspx(并且我的CurrentLanguage 变量是string.empty)时,它仍然从buystuff.aspx.da-dk.resx 加载资源。

有任何想法吗?

4

1 回答 1

0

好的,我自己解决了(那些时间一去不复返了 - 永远;))。

问题出在我的 web.config 中,出于 SEO 原因,我有几条规则:

 <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>

我还在 web.config 中指定了我的文化,所以无论我做什么,它总是相同的文化。

所以解决方案:

  • 我从 web.config 中删除了所有全球化信息
  • 我确保所有内容都是小写的,还有我的 new CultureInfo("da-dk");

我的代码现在是这样的:

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;

}
于 2013-04-28T13:06:30.370 回答