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); } }
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
Just notice non-tech people maintaining localized strings that they mustn't take care about text in brackets.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With