DTI-Matt DTI-Matt - 1 year ago 121
C# Question

How to include Variables in Localized Strings?

I'm trying to display a message to the user along the lines of:

"User 5 could not be added"

But how can I add variables to a string that is being placed in a .resx file? I've trying searching for things like "Variables in Localization" "Globalization with Variables" etc, but came up dry.

If I weren't localizing I would write:

Console.Write("User " + userNum + " could not be added");

How can this be accomplished with resources?

Answer Source

In teams where I've done internationalization, we generally also created a resource for the format string, something like USER_COULD_NOT_BE_ADDED_FORMAT, and called String.Format (or your environment's equivalent) by passing that resource's value as the format pattern.

Then you'll do Console.Write(String.Format(resourceManager.GetString("USER_COULD_NOT_BE_ADDED_FORMAT"), userNum));

Most localizers either have training in the format strings used by the system they are localizing, or they are provided with guidance in the localization kit that you provide them. So this is not, for example, as high a barrier as making them modify code directly.

You generally need to add a loc comment to the resource ID to explain the positional parameters.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download