I am using resources on my asp.net application and need to make the key/value pairs available on the client. I have been able to get them there for a particular resource file by using an HttpHandler, but the standard inheritances are not working with the code I am using.
Base PM.resx file contains the following
key: a value: AAAAAA
key: b value: BBBBBB
key: c value: CCCCCC
PM.pt.resx file contains the following
key: a value: ptptpt
When I have a culture of pt
on my browser or coded, I expect to get the following returned since the pt
file only has a single entry in it and the base file contains the rest of the key/value pairs.
key: a value: ptptpt
key: b value: BBBBBB
key: c value: CCCCCC
But I am only getting the following.
key: a value: ptptpt
The c# code that I am using to generate the JavaScript is as follows.
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);
}
How can I get the inheritance to work in the code?
I have figured out a solution to my question. I created two ResourceSets
; one for the default language (for the default resource) and another for the current language (for the language specific resource). I then created a dictionary
of strings and first added the language specific key / value pairs, then the default key / value pairs. If there is a language specific key already existing, then the default language one will not be added.
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);
}
Note: I did not illustrate escaping my key and value pairs here. This is something that I have to follow-up on in my code.
I hope this helps someone else down the line!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.