首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在其他项目中使用resx文件本地化web.sitemap

在其他项目中使用resx文件本地化web.sitemap
EN

Stack Overflow用户
提问于 2010-08-03 16:58:50
回答 2查看 1.9K关注 0票数 1

我正在开发一个web应用程序,并且我正在使用文化信息本地化。我的应用程序的所有本地化资源都在另一个仅包含资源(resx)文件的项目中。之所以使用此架构,是因为我有其他应用程序使用相同的资源。

我现在的问题是web.sitemap的本地化。目前,我在项目中有一个web.sitemap的resx文件,我使用以下语法引用它

title=‘$资源:SiteMapRes,CLIPS_LIST’

描述=‘$资源:SiteMapRes,CLIPS_LIST

问题是,当我使用其他项目中包含的资源时,这种方法不起作用。

有谁知道如何解决我的问题吗?

诚挚的问候,

何塞

EN

回答 2

Stack Overflow用户

发布于 2010-08-21 07:26:11

站点地图使用默认的本地化提供程序来调用包含在站点地图节点中的本地化表达式。默认提供程序不允许通过表达式设置外部程序集。改变此行为的唯一方法是创建您自己的本地化提供程序。This文章展示了如何做到这一点。在设置您自己的提供程序之后,您可以使用如下表达式从外部程序集访问资源:

正如您将在本文中看到的那样,提供程序的实际实现并不是很困难。

问候你,罗宾

票数 1
EN

Stack Overflow用户

发布于 2014-08-06 13:16:47

我将采取的方法是切换到外部DI,然后实现一个可以从另一个程序集中读取资源的自定义IStringLocalizer类。下面是一个有效的示例。我也在GitHub上创建了一个demo application

代码语言:javascript
复制
using System;
using System.Collections.Specialized;
using System.Resources;

namespace MvcSiteMapProvider.Globalization
{
    public class ResourceManagerStringLocalizer
        : IStringLocalizer
    {
        public ResourceManagerStringLocalizer(
            ResourceManager resourceManager
            )
        {
            if (resourceManager == null)
                throw new ArgumentNullException("resourceManager");
            this.resourceManager = resourceManager;
        }
        protected readonly ResourceManager resourceManager;

        /// <summary>
        /// Gets the localized text for the supplied attributeName.
        /// </summary>
        /// <param name="attributeName">The name of the attribute (as if it were in the original XML file).</param>
        /// <param name="value">The current object's value of the attribute.</param>
        /// <param name="enableLocalization">True if localization has been enabled, otherwise false.</param>
        /// <param name="classKey">The resource key from the ISiteMap class.</param>
        /// <param name="implicitResourceKey">The implicit resource key.</param>
        /// <param name="explicitResourceKeys">A <see cref="T:System.Collections.Specialized.NameValueCollection"/> containing the explicit resource keys.</param>
        /// <returns></returns>
        public virtual string GetResourceString(string attributeName, string value, bool enableLocalization, string classKey, string implicitResourceKey, NameValueCollection explicitResourceKeys)
        {
            if (attributeName == null)
            {
                throw new ArgumentNullException("attributeName");
            }

            if (enableLocalization)
            {
                string result = string.Empty;
                if (explicitResourceKeys != null)
                {
                    string[] values = explicitResourceKeys.GetValues(attributeName);
                    if ((values == null) || (values.Length <= 1))
                    {
                        result = value;
                    }
                    else if (this.resourceManager.BaseName.Equals(values[0]))
                    {
                        try
                        {
                            result = this.resourceManager.GetString(values[1]);
                        }
                        catch (MissingManifestResourceException)
                        {
                            if (!string.IsNullOrEmpty(value))
                            {
                                result = value;
                            }
                        }
                    }
                }
                if (!string.IsNullOrEmpty(result))
                {
                    return result;
                }
            }
            if (!string.IsNullOrEmpty(value))
            {
                return value;
            }

            return string.Empty;
        }
    }
}

然后,您可以将其注入到您的DI配置模块中(显示的是StructureMap示例,但任何DI容器都可以)。

首先,您需要通过将其添加到excludeTypes变量来指定不自动注册IStringLocalizer接口。

代码语言:javascript
复制
var excludeTypes = new Type[] {
// Use this array to add types you wish to explicitly exclude from convention-based  
// auto-registration. By default all types that either match I[TypeName] = [TypeName] or 
// I[TypeName] = [TypeName]Adapter will be automatically wired up as long as they don't 
// have the [ExcludeFromAutoRegistrationAttribute].
//
// If you want to override a type that follows the convention, you should add the name 
// of either the implementation name or the interface that it inherits to this list and 
// add your manual registration code below. This will prevent duplicate registrations 
// of the types from occurring. 

// Example:
// typeof(SiteMap),
// typeof(SiteMapNodeVisibilityProviderStrategy)
    typeof(IStringLocalizer)
};

然后提供ResourceManagerStringLocalizer (及其依赖项)的显式注册。

代码语言:javascript
复制
// Configure localization

// Fully qualified namespace.resourcefile (.resx) name without the extension
string resourceBaseName = "SomeAssembly.Resources.Resource1";

// A reference to the assembly where your resources reside.
Assembly resourceAssembly = typeof(SomeAssembly.Class1).Assembly;

// Register the ResourceManager (note that this is application wide - if you are 
// using ResourceManager in your DI setup already you may need to use a named 
// instance or SmartInstance to specify a specific object to inject)
this.For<ResourceManager>().Use(() => new ResourceManager(resourceBaseName, resourceAssembly));

// Register the ResourceManagerStringLocalizer (uses the ResourceManger)
this.For<IStringLocalizer>().Use<ResourceManagerStringLocalizer>();

然后,只需适当地指定资源即可。您需要以基本名称(在本例中为SomeAssembly.Resources.Resource1)开始,然后将资源的键指定为第二个参数。

代码语言:javascript
复制
<mvcSiteMapNode title="$resources:SomeAssembly.Resources.Resource1,ContactTitle" controller="Home" action="Contact"/>

请注意,获得正确的BaseName是使其正常工作的关键。请参阅以下MSDN文档:http://msdn.microsoft.com/en-us/library/yfsz7ac5(v=vs.110).aspx

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3395009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档