Localizing texts

Majority resources in applications are string and therefore first part of tutorial points to this topic. To 'localize' property using Localizer you must call Add method or one of 3 overloads. Overload with the least parameters needs instance to object, its property name which want to localize, and name of resource key.

Localizer.Add(item1ToolStripMenuItem, "Text", "L0001");
Localizer.Add(label1, "Text", "L0004");
Localizer.Add(this, "Text", "L0013");

This method does two things: sets resource to specified instance's property and stores this data. From now is possible to make instant refresh when changing user language:

Localizer.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
Localizer.Refresh();

As I mentioned before, changing culture via Localizer fires Localizer.CultureChanged event. While changing language by Localizer.CurrentCulture you can be notified about this change. Calling Localizer.Refresh(); refreshes all added items.

Lets look at another Add overloads.

// formatting localized text
public static void Add(object instance,
			 string propertyName, 
			 string resourceKey, 
			 string format)

Localizer.Add(label1, "Text", "L0004", "{0}:");

// specifying resource manager
public static void Add(object instance,
			 string propertyName,
			 string resourceKey,
			 ResourceManager resourceManager)

ResourceManager resourceManager = new ResourceManager("Sample.Resources", Assembly.GetExecutingAssembly());

Localizer.Add(button1, "Text", "L0006", resourceManager);

// specifying all parameters
public static void Add(object instance,
			 string propertyName,
			 string resourceKey,
			 string format,
			 ResourceManager resourceManager)

Localizer.Add(label2, "Text", "L0016", "{0}*", resourceManager);

Remove method serves to remove localized property from Localizer.

public static void Remove(object instance, string propertyName)

Localizer.Remove(label2, "Text");

Of course you can also get string directly by GetString method like in System.Resources.ResourceManager.

public static string GetString(string resourceKey)

public static string GetString(string resourceKey, string format)

public static string GetString(string resourceKey, ResourceManager resourceManager)

public static string GetString(string resourceKey, CultureInfo cultureInfo)

public static string GetString(string resourceKey, string format, CultureInfo cultureInfo)

public static string GetString(string resourceKey, string format, ResourceManager resourceManager)

public static string GetString(string resourceKey, string format, ResourceManager resourceManager, CultureInfo cultureInfo)

string text = Localizer.GetString("L0012", "{0}..");

MessageBox.Show(Localizer.GetString("L0012"));

For more complex localizing or special situation can be used overloads with ResourceManager and CultureInfo. Sample application are inluded on project download page. The next part will focus on localizing structs, objects and streams.

Last edited Nov 13, 2011 at 6:31 PM by CSharpDeveloping, version 3

Comments

No comments yet.