Solved

Access the .resx file in runtime.

Posted on 2010-08-16
10
425 Views
Last Modified: 2012-08-13
Hello,

I want to access the .resx file in runtime.

For example: I have a message for each language and I need to access in accordance with the CurrentUICulture and show to the user.

How can I do?

Thanks!
0
Comment
Question by:Rybius
  • 4
  • 4
  • 2
10 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 33445022
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33445033
Walkthrough: Using Resources for Localization with ASP.NET  
http://msdn.microsoft.com/en-us/library/fw69ke6f(VS.80).aspx

Introduction to the resources .resx and .resources files: Part I (and following)
http://www.c-sharpcorner.com/UploadFile/yougerthen/105032008041615AM/1.aspx
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33445095

Add a resource file named 'Resources.resx'
Add a string resource in resx file  named Hello with value "Hello"
In the code, add the line:
MessageBox.Show(Resources.Hello)
This will show the message "Hello"

To localise it:
Add a resource file named 'Resources.fr.resx' (note the part "fr" in the name; this means this is the resource for French)
Add a string resource named Hello with value "Hello in French"

That's it. If you run the same code in a machine with french settings, the same message box will display "Hello in French".

If the machine is in English settings, French settings can be forced by setting value CurrentUICulture like:
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-FR");
MessageBox.Show(Resources.Hello)

Hope this helps.


0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Rybius
ID: 33445489
Hi, kris per!

MessageBox.Show (Resources.Hello);
This way is not possible to access a string resource in the Local_Resources.
Hello is not recognized.

Using Resources.Resource1.Hello, I can access the Hello only if in Global Resources.
How can  I do to access a resource string if in Local_Resources?

Can you help me?

Thanks!
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33445529

Try like:

MessageBox.Show (Properties.Resources.Hello);

OR

Add the Properties namespace in usings like:

using WindowsApplication1.Properties.
0
 

Author Comment

by:Rybius
ID: 33445566
Hi, Dhaest!


Following the example and doing like this, I could not access the contents of Hello that is in Local_Resources.
The str returns NULL.

            ResourceManager rm = new ResourceManager("Resources.Resource1",
            Assembly.GetExecutingAssembly());

            String str = rm.GetString("Hello");
           Console.WriteLine(str);

Can anyone help me?

Thanks!
0
 

Author Comment

by:Rybius
ID: 33445599
I am using a WebApplication and there is no "properties"
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33445610

For web app:

For global resources, Resources.Resource1.Hello will work...
 
For local resources, you have to use:

string helloValue = GetLocalResourceObject("Hello")

in the page code..
0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33445636

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string helloValue = GetLocalResourceObject("Hello").ToString();
        }
    }

Open in new window

0
 

Author Comment

by:Rybius
ID: 33445718
Yes!

Now it worked!

Thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question