Solved

Access the .resx file in runtime.

Posted on 2010-08-16
10
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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