1

私は Web アプリケーションを開発しており、カルチャ情報のローカリゼーションを使用しています。アプリケーションのすべてのローカリゼーション リソースは、リソース (resx) ファイルのみを含む別のプロジェクトにあります。このアーキテクチャが使用されるのは、同じリソースを使用する他のアプリケーションがあるためです。

私の問題は、web.sitemap のローカライズです。現在、プロジェクトに web.sitemap への resx ファイルがあり、次の構文を使用して参照しています

title='$Resources:SiteMapRes,CLIPS_LIST'
description='$Resources:SiteMapRes,CLIPS_LIST

問題は、他のプロジェクトに含まれるリソースを使用するときに、このアプローチが機能しなかったことです。

私の問題を解決する方法を知っている人はいますか?

よろしくお願いします、

ホセ

4

2 に答える 2

1

サイトマップは、デフォルトのローカリゼーション プロバイダーを使用して、サイトマップ ノードに含まれるローカリゼーション式を呼び出します。既定のプロバイダーでは、式を使用して外部アセンブリを設定することはできません。この動作を変更する唯一の方法は、独自のローカリゼーション プロバイダーを作成することです。この記事では、これを行う方法を示します。独自のプロバイダーをセットアップした後、次のような式を使用して、外部アセンブリからリソースにアクセスできます。

この記事で説明するように、プロバイダーの実際の実装はそれほど難しくありません。

よろしく、ロビン

于 2010-08-20T23:26:11.050 に答える
1

私が取るアプローチは、外部 DI に切り替えてから、別のアセンブリからリソースを読み取ることができるカスタム IStringLocalizer クラスを実装することです。これが実際の例です。GitHub にもデモ アプリケーションを作成しました。

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 インターフェイスを自動的に登録しないように指定する必要があります。

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 (およびその依存関係) の明示的な登録を提供します。

// 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) で開始し、リソースのキーを 2 番目の引数として指定する必要があります。

<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

于 2014-08-06T05:16:47.440 に答える