Reputation: 14622
I have created to .resx
files: Translate.English.resx
and Translate.Romanian.resx
that contain strings like this:
MainWindowTitle : English Value
and in the Romanian file:
MainWindowTitle : Valoarea in Romana.
Now my question is: how tcan I force the program to interpret the the code like here:
string
called lang
, firstly equal with en
.MainWindow.Text = Translate.English.MainWindowTitle
Now I want it to be simpler to implement, so beside:
switch(lang){
case "ro":
MainWindow.Text = Translate.English.MainWindowTitle;
break;
case "en":
MainWindow.Text = Translate.Romanian.MainWindowTitle;
break;
}
So, beside that, I want it to be something like:
lang = "ro";
MainWindow.Text = Translate.(lang value).MainWindowText
How can I do this?
Upvotes: 2
Views: 782
Reputation: 45
All the guys above are right. Beside that you can also change the language in real time with something like that.
private void RefreshResources(Control ctrl, ComponentResourceManager res)
{
ctrl.SuspendLayout();
res.ApplyResources(ctrl, ctrl.Name, CurrentLocale);
foreach (Control control in ctrl.Controls)
RefreshResources(control, res); // recursion
ctrl.ResumeLayout(false);
}
If you want a complete example check my blog here
Upvotes: 0
Reputation: 9794
Choose Add New Item and Selected Resource Files in your project.
Give a name like String.resx, String.ro.resx, String.tr.resx, String.fr.resx.
Sample usage for 4 different language:
Console.WriteLine(Localize("name", "ro"));
Console.WriteLine(Localize("name", "en"));
Console.WriteLine(Localize("name", "fr"));
Console.WriteLine(Localize("name", "tr"));
public static string Localize(string name, string languageCode)
{
return Strings.ResourceManager.GetString(name, new CultureInfo(languageCode));
}
Upvotes: 0
Reputation: 4372
Check out this post. You'll have to rename your resx files to something like "translate.en.resx" and "translate.ro.resx". Then you can do something like this:
CultureInfo.GetCultureInfo("en");
or
CultureInfo.GetCultureInfo("ro");
MainWindow.Text = translate.MainWindowText;
Upvotes: 1