Read values from resx files? - asp.net

How to read strings from the MyResource.resx file from c#.
I am not calling this from the asp.net page, rather i am calling from my bussiness logic.
Assembly assembly =
this.GetType().Assembly;
ResourceManager resourceManager = new
ResourceManager("MessagesResource",
assembly);
resourceManager.GetString("SCHEME_UNQ");
here i am getting exception,
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
"MessagesResource.resources" was
correctly embedded or linked into
assembly "App_Web_eerdggo8" at compile
time, or that all the satellite
assemblies required are loadable and
fully signed.
How can i fix this issue????

Once you add a resx file to your project, Visual Studio will automatically generate a designer class which allows you to read value (In the properties of the resx file you must have Custom Tool: ResXFileCodeGenerator). For example if you add Messages.resx to your project you could directly read values from it:
string value = Messages.SomeResourceKey;

Related

Localization/Resource files in Class Library

I am trying to achieve globalization in a .NET class library. In a .NET web application this seems to work fine. I can add multiple resource files under the App_GlobalResources.
e.g.
LocalizedText.resx
LocalizedText.fr.resx
However resource files are handled differently in Class Libraries. I add a resource file by
1) Opening the Properties for the Class Library
2) Clicking on the Resources Tab
3) Clicking on the link to create a “default resources file”
With this model it seems to only want to allow one default resources file. I can rename files and seem to get around this “one file” limitation but if I produce resource files with the same names as above there appears to be no code generated for the “LocalizedText.fr.resx” file. If I reference a string in code like so…
myControl.Text = Properties.LocalizedText.MyLocalizedText;
It references the LocalizedText.resx file for the value (ignoring the fact that I have the culture set to French). I’m guessing that whatever auto-generates the code for the designer file sees that there is already a “LocalizedText” class and doesn’t generate the necessary code.
Is there not a way (equivalent to the web application project) that I can use multiple resource files in a Class Library, named differently for each culture, and be able to easily access this in code (trusting .NET to switch appropriately depending on the culture info)?
Many Thanks
First: create a folder in your project named 'MyFolder'.
Second: add a resource file named 'MyResourceFile' to the folder.
Third: where you want access the resource values, import:
using System.Reflection;
using System.Resources;
using MyProject.MyFolder.MyResourceFile;
and create a property that can access the your resource. Like this:
public static ResourceManager oResourceManager = new ResourceManager("MyProject.MyFolder.MyResourceFile", typeof(MyResourceFile).Assembly);
Fourth: Get the data from your property:
oResourceManager.GetString("ResourceKey", System.Globalization.CultureInfo.CurrentCulture);

Localization using resource files

I have a problem with resx based localization.
It's an ASP.NET MVC3 project. I have two assemblies: Web and Resource. Web contains all MVC related stuff, and Resource contains resx files for localization(I am building site with 3 different languages available like file.resx, file.en-AU.resx, file.en-US.resx).
Now the weird thing is that this is all working really nicely on my local machine using VS2010 and IIS when I add the Resource project reference as a Project, but when I add the reference to the Resource.dll in my bin - it doesn't work.
I tried to output Thread.CurrentThread.CurrentUICulture in my views, just before outputting localized strings. It gives me selected culture, which is right, but nevertheless strings are rendered as a fallback value, not localized one.
Could anyone tell me why it wouldn't work if I add the reference as a dll file in my bin?
If I am not wrong you should also add to your bin the generated bin, en-au and en-us folders that you should find in the bin of resource project
another option ensure you have the resource file with public accessor

Read a specific resx file within a ClassLibrary

I have a ClassLibrary with a C# Class and a folder with resx files.
I have to read a specific Resx file by culture within the C# class.
I tried the following code (both GetLocalResourceObject and GetGlobalResourceObject), but I get this error: 'The relative virtual path is not allowed here'.
HttpContext.GetLocalResourceObject("Folder.ResxFileName", "ResxKey", new CultureInfo("it-IT"))
How do I have to set the Resx file path to get it working?
(The ClassLibrary dll is referenced in a website project)
You've just to use the ResourceManager exposed by the class library (with the namespace 'ClassWithResx'):
ClassWithResx.Resources.ResourceManager.GetString("ResxKey", System.Globalization.CultureInfo.GetCultureInfo("en-US"));
this works supposing you've a Resources.resx and Resources.en-US.resx (or whatelse) file in the root of your class library, and you've to mark with the custom tool the access modifier of the resource strings to "Public".

Using Resource .resx file in Class library project

I have used resource file(.resx) file in a class library project to store some error messages. When I set the "Build Action" to "Embedded Resource" for the resx file and deploy it works fine. But I would like to separate the resource file from the dll since I may need to change the error messages in resx file in future without the need to recompiling the class library project. I tried the other option in "Build Action" property Content,resource, etc but nothing seems to be working in the way I require. When I use these property I am getting the below error,
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure was correctly embedded or linked into assembly at compile time, or that all the satellite assemblies required are loadable and fully signed.
Is there any way to resolve this error and make it work?
Resource files have to be set to embedded - that's how they work.
You can create another assembly containing the resources and reference it - this way you can redeploy the updated resources. It requires a bit more work in your code (loading the assembly in order to be able to get the embedded resources).
However, from your description (text messages that need to be editable after deployment), perhaps storing these in configuration is a better option (in particular if you are not localizing).

ASP.NET - what files store method signature for xml web service?

i find this question tricky but what files store method signature info for xml webservice?
.dll, .asmx, .wsdl, .disco ?
TIA
It goes into a DLL once compiled, but it's in a code file when added to the project (referred to as a proxy class, it's in whatever language your project is). When you add a web reference it's imported this way. Updating the reference updates the .cs (or vb, whatever you're using).
If you want to see what happens, you can generate the code file yourself, see MSDN for details.
So overall: It is WSDL when fetched, converted to a code file when included (or updated), then finally compiled with the rest of your code into the DLL.

Resources