Subfolders in App_GlobalResources (ASP.NET)

Is it possible to put resource files (.resx) in subfolders inside App_GlobalResources?

For example:

/App_GlobalResources/someresources/myfile.resx

/App_GlobalResources/someresources/myfile.fr-fr.resx

/App_GlobalResources/othereresources/otherfile.resx

/App_GlobalResources/othereresources/otherfile.fr-fr.resx

Or are all .resx files placed directly inside App_GlobalResources?

If you can use subfolders, how do you programmatically access resources in subfolders?

+7
resources internationalization localization globalization
source share
2 answers

Technically, yes, it is possible, but there are some pitfalls. First, let me show you an example. Suppose my App_GlobalResources folder looks like this:

 /App_GlobalResources /Test TestSubresource.resx TestResource.resx 

Each resource file has one entry called "TestString". I added each resource file using the Visual Studio menu to create a class for me. By default, all classes added to the App_GlobalResources folder will have the same Resource namespace. Therefore, if I want to use the class generator, and I want Test in the namespace, I need to go into the TestSubresource.Designer.cs file and manually change the namespace. As soon as I do this, I can do the following:

 var rootResource = Resources.TestResource.TestString; var subResource = Resources.Test.TestSubResource.TestString; 

I can also reference them using GetGlobalResourceObject :

 var rootResource = GetGlobalResourceObject( "TestResource", "TestString" ); var subResource1 = GetGlobalResourceObject( "TestSubresource", "TestString" ); 

Note that I still use "TestSubresource" as a means to reference the resources in this file, even if it is in a subfolder. Now one of the catch is that all files must be unique in all folders in App_GlobalResources or your project will cause a runtime error. If I add a resource called "TestResource.resx" to / Test, it will output the following runtime error:

The resource file '/App_GlobalResources/TestResource.resx' cannot be used because it conflicts with another file with the same name.).

This is true even if I change the namespace on the new resource.

So, in conclusion, yes, it is possible, but you increase the likelihood of a runtime error due to two identically named resource files in different parts of the App_GlobalResources folder App_GlobalResources , which are allowed by the file system but not .NET.

+12
source share

It is possible. At least I did it.

Inside the website, I added the App_GlobalResources folder. Inside, I created another folder called "MyFolder" and placed the file MyResource.resx inside. The Resx file contains one pair of MyKey1 - MyValue1.

Using the GetResource method of the following class, I successfully retrieved "MyValue1" for name = "MyKey1"

 static class Class1 { static Assembly FindGlobalResAssembly() { foreach(Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { if(asm.FullName.StartsWith("App_GlobalResources.")) return asm; } return null; } public static object GetResource(string name) { Assembly asm = FindGlobalResAssembly(); if(asm == null) return null; return new ResourceManager("Resources.MyResource", asm).GetObject(name); } } 

This approach also works in trust.

Folders do not seem to have any meaning when accessing resources from code.

+1
source share

All Articles