Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get resource strings from ASP.NET markup?

Hi I have an assembly called like X.Common.DLL. There is some resources files for multilanguage app. Let's say it Language.resx Language.en-US.resx....etc....

I have a web application which contains this above dll as reference...

So how can I use this resources file in my web applications markup side?

Text="<%$ Resources:Class, ResourceKey %>" is not valid because of "Class" name is in another assembly...

like image 512
Arda Avatar asked Dec 24 '10 09:12

Arda


1 Answers

You can easily create a wrapper class that does something like this

public class ResourceWrapper
{
     private ResourceManager resourceManager;

     public ResourceWrapper()
     {
         resourceManager = new ResourceManager("Namespace.Common", Assembly.Load("x.common"))
     }

     public string String(string resourceKey)
     {
         return ResourceManager.GetString(resourceKey);
     }
 }     

Finding the correct name for the first param to new ResourceManager(...) can be a bit tricky sometimes. To make it easier for yourself you can call like this:

Assembly.Load("x.common").GetManifestResourceNames() and check the returned results.

If you create a static wrapper, you can make the resource calling code as simple as this:

<%= Resource.String("MyResourceKey") %>
like image 104
Steven K. Avatar answered Oct 10 '22 13:10

Steven K.