【问题标题】:Asp.Net resource manager always returns neutral culture resourcesAsp.Net 资源管理器总是返回中性文化资源
【发布时间】:2009-06-27 14:43:03
【问题描述】:

我正在使用嵌入式 .resx 来本地化一个 asp.net 应用程序。似乎不管现在的文化是什么,中立的文化资源总是被退回来的。我用来检索值的代码如下:

protected string GetResource(string name)
    {
        return Localization.ResCore.ResourceManager.GetString(name, System.Threading.Thread.CurrentThread.CurrentCulture);
    }

我在一页中指定文化是“es-PE”(秘鲁)。当我中断 GetResource 函数时,我可以验证 CurrentCulture 是“es-PE”,并且 ResourceManager 包含与此文化相对应的 ResourceSet。但是,返回的字符串始终来自中性文化集。

我嵌入的文件命名如下:

  • ResCore.resx
  • ResCore.es.resx
  • ResCore.es-PE.resx

感谢任何帮助。

【问题讨论】:

    标签: c# asp.net localization embedded-resource culture


    【解决方案1】:

    如果你想使用资源,你可以改变你的第二个参数

    System.Threading.Thread.CurrentThread.CurrentCulture
    

    System.Threading.Thread.CurrentThread.CurrentUICulture
    

    【讨论】:

      【解决方案2】:

      您为什么不使用GetLocalResourceObjectGetLocalResourceObject

      【讨论】:

      • 我的印象是,要使用 GetGlobalResourceObject,资源文件必须位于 Web 应用程序根目录的 App_GlobalResources 文件夹中。我想保留嵌入在程序集中的资源文件。
      • App_GlobalResources 路由对于执行代码的测试是不可访问的。将您的资源嵌入到 dll 中使您的代码可测试。
      【解决方案3】:

      只是想知道为什么需要嵌入它?您不能只添加到 App-LocalResources 和 App-GlobalResources 并从那里使用它吗?

      此外,您会发现如果不调用 base.InitializeCulture(),该语言将无法正常工作。您将创建一个基本页面并从中继承。像这样:

      protected class BasePage : System.Web.UI.Page
      {
           protected override void InitializeCulture(object sender, EventArgs e)
           {
                this.Culture = Resources.Culture = Thread.CurrentThread.CurrentUICulture;
      
                base.InitializeCulture();
           }
      }
      

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-01
        • 2020-12-12
        • 1970-01-01
        • 1970-01-01
        • 2014-05-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多