15

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?

Eric J.
  • 147,927
  • 63
  • 340
  • 553
DTI-Matt
  • 2,065
  • 9
  • 35
  • 60

5 Answers5

15

You can't do this directly.

What you can do is place a token - a specific string that can be replaced with string.Replace with the value of the variable.

A good candidate for this would be the built in string formatting:

Console.Write(string.Format("User {0} could not be added", userNum));

Assuming userNum has the value 5, the result would be:

User 5 could not be added

You can localize this string with the composite format specifiers.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
11

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.

JasonTrue
  • 19,244
  • 4
  • 34
  • 61
  • 2
    Could you just go a bit further into how I can add such a "loc comment to the resource ID"? Or provide me a link to read more? Thanks! :) – DTI-Matt Jun 11 '12 at 13:10
  • Also, while I have you here since you seem to have experience with these things, is it best to have a single resx file for all the strings in a given project, or would it be better to separate them into multiple resx files, say, one for each form? (My resx files themselves are stored in a separate Globalization project) – DTI-Matt Jun 11 '12 at 13:37
  • 1
    Assuming you're using the .resx format, the localization comment is just "Comment" in the resx editor UI, or `` in the xml right after the `` element. – JasonTrue Jun 11 '12 at 17:28
  • 1
    I recommend using multiple resx files, but more for source control sanity than anything else. Usually you create a packaged up .zip file (or similar) to send to localizers, and there shouldn't be any incremental cost for the flexibility of multiple files. – JasonTrue Jun 11 '12 at 17:32
  • If one wants to do something similar, but when applying directly at a cshtml file, it can do it like this: @String.Format(@Localizer["USER_COULD_NOT_BE_ADDED_FORMAT"].Value, userNum) – dchang Oct 08 '21 at 10:33
6

Use Composite Formatting like so:

Console.Write("User {0} could not be added", userNum);

This way you would localize "User {0} could not be added".

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
4

you can do that its simple enter image description here

new lets see how

String.Format(Resource_en.PhoneNumberForEmployeeAlreadyExist,letterForm.EmployeeName[i])

this will gave me dynamic message every time

by the way I'm useing ResXManager

Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
3

I would use string.Format

http://msdn.microsoft.com/en-us/library/system.string.format.aspx

Console.Write(string.Format("User {0} could not be added", userNum));