Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is adding strings with placeholders (`{0}`) into resources a good idea?

Tags:

I have added a string into a resources file. My application will be localized.
But, is adding strings with placeholders ({0}) into resources a good idea?
What if some non-technical person does localization? Is there a way for them to screw it up, unknowingly?

If this isn't a good idea, what should I do?

Here is simple example. I will be using WPF resource dictionaries.

Example:

// Resource1.resx //        Name               |            Value //--------------------------------------------------------------- // RELATIONSHIP_STATUS_MSG   | {0} is in relationship with {1}.  //   class Program {     static void Main(string[] args)     {         string msg = string.Format(Resource1.RELATIONSHIP_STATUS_MSG,                                     "Romeo", "Juliot");         Console.WriteLine(msg);     } } 
like image 259
Pratik Deoghare Avatar asked May 25 '11 09:05

Pratik Deoghare


1 Answers

Well, I believe that it's a good idea because this is an easy and quick way of rendering parametrized and localized strings.

By the way, as you say in your question, non-tech people can break your localization strings because they don't understand what's "{0}". I've two "approaches" for solving that problem

  1. Just notice non-tech people maintaining localized strings that they mustn't take care about text in brackets.

  2. Use named placeholders: "{some-identifier}" and just use someTextResource.Replace("{some-identifier}", someTextVar).

About 2nd one, you can implement some method accepting an IDictionary<TKey, TValue> instance of substitution relations, where the key is the identifier to replace, and value the text to put replacing the identifier.

like image 69
Matías Fidemraizer Avatar answered Oct 04 '22 16:10

Matías Fidemraizer