Solved

Access the .resx file in runtime.

Posted on 2010-08-16
10
424 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now