views:

1045

answers:

1

I'm localizing application and need to provide JSON representation of local and global resources for JS part of application for all views. My current idea is I'd implement HtmlHelper extension methods like GetLocalResourcesJSON/GetGlobalResourcesJSON which should encode all resource keys+values and return them JSON encoded as string (I'd implement caching as well).

At the moment I'm able to retrieve single specific key from global or local resource belonging to current view (using httpContext.GetGlobalResourceObject/GetLocalResourceObject), but I'm not able to find out how to retrieve whole resource object and iterate all its keys+values. Is there any method how to achieve this?

it looks like ResourceProviderFactory could be the the key to this problem, but it's not accessible publicly anywhere. I could instantiate ResourceExpressionBuilder and use reflection to retrieve the provider using GetLocal/GlobalResourceProvider() methods, but I don't like using reflection here at all...

A: 

If you can get a hold of a ResourceManager you can use the GetResourceSet() method to return all the localized strings for a given culture.

App_ GlobalResources will get compiled into the "Resources" namespace and you can access the ResourceManager there (i.e. Resources.General.ResourceManager) For App_ LocalResources it's a bit trickier, which is one of the reasons why I tend to not use App_ LocalResources. See this [post][1] for one possible solution.

<dl>
<% foreach (DictionaryEntry entry in Resources.Global.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) { %>
  <dt><%= entry.Key %></dt>
  <dd><%= entry.Value %></dd>
<% } %>
</dl>

Returns:

<dl>
  <dt>SiteName</dt>
  <dd>The Site Name</dd>

  <dt>Copyright_Text</dt>
  <dd>Copyright 2009</dd>

  <dt>Copyright_Html</dt>
  <dd>Copyright &copy; 2009</dd>
</dl>

[1]: http://blog.mattweber.name/?p=39"ASP.NET - Getting values from Resx file"

Talljoe
at the moment I solved it using reflection like this: var resourceManager = ResourceExpressionBuilder.GetLocalResourceProvider(virtualPath) .CreateResourceManager();and than use resourceManager.GetResourceSet() to iterate over all resource items.I don't like this dirty solution though ;(
Buthrakaur