0

asp.netアプリケーションでリソースを使用しており、クライアントでキーと値のペアを使用できるようにする必要があります。HttpHandler を使用して特定のリソース ファイルを取得できましたが、使用しているコードでは標準の継承が機能しません。


ベースのPM.resxファイルには、次のものが含まれています。

key: a value: AAAAAA
key: b value: BBBBBB
key: c value: CCCCCC

PM.pt.resxファイルには次のものが含まれます

key: a value: ptptpt

ptブラウザ上またはコード化された文化を持っている場合、ptファイルには単一のエントリしかなく、ベース ファイルには残りのキーと値のペアが含まれているため、次のものが返されることを期待しています。

key: a value: ptptpt
key: b value: BBBBBB
key: c value: CCCCCC

しかし、私は次のことしか得ていません。

key: a value: ptptpt

JavaScript を生成するために使用している c# コードは次のとおりです。

public void ProcessRequest(HttpContext context)
    {
        ResourceManager rm = new ResourceManager("Resources.PM", System.Reflection.Assembly.Load("App_GlobalResources"));
        Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR");//for testing culture change
        //ResourceSet resourceSet = rm.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
        ResourceSet resourceSet = rm.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true);

        string r = string.Empty;

        if (resourceSet != null)
        {
            foreach (DictionaryEntry entry in resourceSet)
            {
                string resourceKey = entry.Key.ToString();
                object resource = entry.Value.ToString();
                r += "\"" + entry.Key.ToString() + "\": \"" + entry.Value.ToString() + "\", ";
            }
        }

        r = "var q = {" + r + " culture: \"" + Thread.CurrentThread.CurrentCulture.ToString() + "\"};";
        r += "console.log(q);$.each(q, function(k, v){console.log(k + \": \" + v)});";
        context.Response.Write(r);
    }

コードで継承を機能させるにはどうすればよいですか?

4

1 に答える 1

0

私は自分の質問に対する解決策を見つけました。2つ作成しましたResourceSets。1 つは既定の言語用 (既定のリソース用) で、もう 1 つは現在の言語用 (言語固有のリソース用) です。次に、一連の文字列を作成し、dictionary最初に言語固有のキーと値のペアを追加し、次にデフォルトのキーと値のペアを追加しました。言語固有のキーが既に存在する場合、デフォルトの言語キーは追加されません。

public void ProcessRequest(HttpContext context)
    {
        string responseString = string.Empty;
        CultureInfo currentCulture = CultureInfo.CurrentUICulture;

        ResourceManager rm = new ResourceManager("Resources.PM", System.Reflection.Assembly.Load("App_GlobalResources"));

        CultureInfo defaultCulture = new CultureInfo("en-US");

        ResourceSet currentRS = rm.GetResourceSet(currentCulture, true, true);
        ResourceSet defaultRS = null;

        if (defaultCulture != currentCulture)
        {
            defaultRS = rm.GetResourceSet(defaultCulture, true, true);
        }

        Dictionary<string, string> translations = new Dictionary<string, string>();

        if (currentRS != null)
        {
            foreach (DictionaryEntry entry in currentRS)
            {
                try {
                    translations.Add(entry.Key.ToString(), entry.Value.ToString());
                }
                catch (Exception e) { }
            }
        }

        if (defaultRS != null)
        {
            foreach (DictionaryEntry entry in defaultRS)
            {
                try {
                    translations.Add(entry.Key.ToString(), entry.Value.ToString());
                }
                catch (Exception e){}
            }
        }

        foreach (KeyValuePair<String, String> entry in translations)
        {
            responseString += "\"" + entry.Key.ToString() + "\": \"" + entry.Value.ToString() + "\", ";
        }

        responseString = "var translations = {" + responseString + " culture: \"" + currentCulture.ToString() + "\"};";
        context.Response.Write(responseString);

        Compress(context);
        //SetHeadersAndCache(absolutePath, context);
    }

注: ここでは、キーと値のペアのエスケープについて説明しませんでした。これは、私のコードでフォローアップする必要があるものです。

これが他の誰かの助けになることを願っています!

于 2012-09-21T21:22:24.650 に答える