【问题标题】:Setting cultureinfo does not give correct strings from local resource files设置cultureinfo 不会从本地资源文件中提供正确的字符串
【发布时间】:2013-04-26 18:35:19
【问题描述】:

我有一个网站,其页面名为 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 加载资源。

有什么想法吗?

【问题讨论】:

  • 你为什么只在一个地方设置CurrentUICulture
  • @Oded ,你是什么意思? :-)
  • 在您的第二个代码示例中,您有Thread.CurrentThread.CurrentUICulture = info;。你永远不会在其他任何地方设置CurrentUICulture。您应该同时设置它(和 CurrentCulture 到想要的文化。
  • 我可能很笨或者很慢,但是为什么要设置 CurrentUICulture 几次呢?在 Page_Load 中一次还不够吗?好像没看懂,能详细点吗? :)

标签: asp.net webforms culture


【解决方案1】:

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

问题出在我的 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 中删除了所有全球化信息
  • 我确保所有内容都是小写的,还有我的新 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;

}

【讨论】:

    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    相关资源
    最近更新 更多