MoonKnight
MoonKnight

Reputation: 23833

How to Handle String Literals in Resource (.resx) Files

All, I have recently been learning a lot about localisation and the use of resource files (.resx). For a particular code, I have a .resx (and ascociated localised versions .de-DE.resx etc.) for each WinForm and two for the code (one for object strings [column names, etc.] and one for user messages [those displayed in message boxes etc.]). For the object strings I have no problem what-so-ever, but for the messages I have the following problem...

Take the message 'Search "SomeString" (23 hits in 3 files)', where is a tab-space at the start of the message. To create such a message I use code like

String.Format("\tSearch \"{0}\" ({1} hits in {2} files)\n",
              strSearchString,
              nTotalHits,
              nNumberOfFiles);

The problem is now, when I include the string "\tSearch \"{0}\" ({1} hits in {2} files)\n" in the resource file I get the following output at runtime

\tSearch \"SomeString\" (23 hits in 3 files)\n

Clearly it is not treating the string literals as C# itself does. I know that to create the new line in the resource file I have to use Shift + Return, but what do you do for tabs et al.? Please don't say copy in to Word or Notepad++ and add the tab, copying back after or I will poke myself in the eye.

What is the best way to convert the string literals contained in a resource file to actual string literal that are interpreted correctly by C#?

Note that this sort of conversion is only going to happen with messages, so I am even willing to consider a runtime conversion.

Thanks for your time.

Upvotes: 1

Views: 1887

Answers (1)

Matthew Watson
Matthew Watson

Reputation: 109537

I'm afraid you're going to be poking yourself in the eye, because that's exactly what you have to do. And I, too, am dismayed by this.

You could include the tab as a separate item, like this:

String.Format("{0}Search \"{1}\" ({2} hits in {3} files)\n",
          "\t",
          strSearchString,
          nTotalHits,
          nNumberOfFiles);

You can also type Alt-012 on the numeric keypad to enter a tab.

Finally, you could "escape" the tabs yourself by doing string.Replace("\\t", "\t") before passing the string to string.Format() and putting a "\t" into the string resource.

It's all a bit unsatisfactory tho! (And I'm sure you've already thought of all these ideas yourself...)

Upvotes: 5

Related Questions