【问题标题】:Does Assembly.GetExecutingAssembly().GetManifestResourceNames() return resources from App_LocalResources?Assembly.GetExecutingAssembly().GetManifestResourceNames() 是否从 App_LocalResources 返回资源?
【发布时间】:2017-04-17 20:19:30
【问题描述】:

我的项目中有全局和本地嵌入资源,如图所示。

Resources files in my project with build action as embedded resources

我有一个函数ResourceText如下

             public static string GLOBAL_RESOURCES = "SampleClient.App_GlobalResources.Global.resources";

                /// <summary>
                /// Used in JavaScript/front code to return resource translations for current page or global resource file
                /// </summary>
                /// <param name="pResourceKey"></param>
                /// <returns></returns>
                /// <remarks></remarks>
                public string ResourceText(string pResourceKey, bool pGlobalResource = false)
                {
                    if (string.IsNullOrEmpty(pResourceKey)) throw new ArgumentNullException("ResourceKey cannot be empty");

                    if (pGlobalResource)
                    {
                        // Find the value from the global resource

                        ResourceManager tResourceManager = new System.Resources.ResourceManager(GLOBAL_RESOURCES.Replace(".resources", ""), this.GetType().BaseType.Assembly);
                        tResourceManager.IgnoreCase = true;

                        string tTranlsation = tResourceManager.GetString(pResourceKey);

                        return string.IsNullOrEmpty(tTranlsation) ? pResourceKey : tTranlsation;
                    }
                    else
                    {
                        string[] tAssemblyNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
                        try
                        {
                            if (tAssemblyNames.Length >= 1) // There is a local file associated
                            {
                                // Get value from the local resource
                                string tAssemblyName = this.Page.GetType().BaseType.FullName.Insert(this.Page.GetType().BaseType.FullName.LastIndexOf(".") + 1, "App_LocalResources.");


                                string tResName = (from n in tAssemblyNames where n.Contains(tAssemblyName + ".aspx") select n).First().Replace(".resources", "");
                                ResourceManager tResourceManager = new System.Resources.ResourceManager(tResName, this.GetType().BaseType.Assembly);

                                tResourceManager.IgnoreCase = true;


                                string tTranlsation = tResourceManager.GetString(pResourceKey);
                                return string.IsNullOrEmpty(tTranlsation) ? pResourceKey : tTranlsation;
                            }
                        }

                        catch (Exception ex)
                        {
                            throw (ex);
                            // Check the local resources
                        }

                    }
                    // Fall back
                    return pResourceKey;
                }

在我的 aspx 页面中称为

 <input type="search" id="inputCustomerGroupGridSearch" placeholder="<%= ResourceText("PlaceholderSearch")%>" />
            <button type="button" id="buttonNewCustomerGroup" style="float: right" class="PrimaryButton"><%=ResourceText("ButtonNew")%></button>

当我调试ResourceText函数时,这行代码

string[] tAssemblyNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();

仅返回“SampleClient.App_GlobalResources.Global.resources”而不是“SampleClient.Modules.Customers.App_LocalResources.Customers.resources”。为什么Assembly.GetExecutingAssembly().GetManifestResourceNames()没有返回App_LocalResources中的资源?

【问题讨论】:

    标签: c# asp.net localization globalization


    【解决方案1】:

    已解决:我在我的 bin 文件夹中添加了强类型程序集 App_LocalResources.resources.dll。然后在执行Assembly.GetExecutingAssembly().GetManifestResourceNames()时列在资源名称中;

    所以,我可以为全局和本地资源调用我的 ResourceText 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多