C # - MessageBox - Localization of messages in resources and line breaks

I would like to show a MessageBox (WinForms) with a string from resources with line breaks.

example without resources (WORKS):

string someMsg = "Message. Details:\n" + someDetails; MessageBox.Show(someMsg); 

Result:

Message. Details:

here are some details

When I move the line "Message. Details: \ n" to "Resources":

 string someMsg = GlobalStrings.MsgBoxJustTest + someDetails; MessageBox.Show(someMsg); 

Result:

Message. Details: \ nThere are some details.

When I moved the line from "\ n" to resources, then MessageBox.Show () stops interpreting it as a new line.

Edit: I am thinking of: someMsg.Replace (@ '\ n', Environment.NewLine); but it is still pretty unpleasant for such a simple thing.

+7
source share
4 answers

if you add this to resources that it doesn’t accept \ n, like escape-charecter Just open your resource file in notepad to see this and cahnge in the XML (resx) file

or

Enter your data in a notebook with a new line. Copy this and paste into your resource editor

edit:

or

Enter / paste your data into the resource editor user interface, select \ n and replace it with the actual line break using Shift-Enter .

+10
source

You can do something like this (as long as your not .net 2.0):

 public static class StringExt { public static String FixNewLines(this String str) { return str.Replace(@'\n',Environment.NewLine); } } 

And then:

 string someMsg = GlobalStrings.MsgBoxJustTest + someDetails; MessageBox.Show(someMsg.FixNewLines()); 

However, this will affect ALL lines in your application (namespace area)

This is a dirty fix, but it is a quick fix.

Personally, I just correct my logic to the end, and do not do something like the above.

+1
source

Perhaps you can open the resx file as code and add line breaks directly in XML

OR

Maybe they get lost when reading due to the escape character, maybe try using \\

0
source

One simple solution is to keep the placeholders in the resource strings. For instane, this line is stored in * .resx under the key "MessageDetails": "Message. Details: {0} {1}". Then in your code use it like this:

MessageBox.Show (String.Format (GlobalStrings.MessageDetails, Environment.NewLine, @ "Message"));

The advantage here is portability, as you can see.

0
source

All Articles