Victor
Victor

Reputation: 14622

Translation implementation

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:

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

Answers (3)

FvZ
FvZ

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

Nesim Razon
Nesim Razon

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

davehale23
davehale23

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

Related Questions