Avoiding resource (localizable string) duplication with String.Format

Posted by Hrvoje Prgeša on Stack Overflow See other posts from Stack Overflow or by Hrvoje Prgeša
Published on 2010-06-10T16:22:36Z Indexed on 2010/06/10 16:32 UTC
Read the original article Hit count: 209

I'm working on a application (.NET, but not relevant) where there is large potential for resource/string duplication - most of these strings are simple like:

  • Volume: 33
  • Volume: 33 (dB)
  • Volume
  • 33 dB
  • Volume (dB)
  • Command - Volume: 33 (dB)

where X, Y and unit are the same.

Should I define a new resource for each of the string or is it preferable to use String.Format to simplify some of these, eg.:

String.Format("{0}: {1}", Resource.Volume, 33)
String.Format("{0}: {1} {2}", Resource.Volume, 33, Resource.dB)
Resource.Volume
String.Format("{0} ({1})", 33, Resource.dB)
String.Format("{0} ({1})", Resource.Volume, Resource.dB)
String.Format("Command - {0}: {1} {2}", Resource.Volume, 33, Resource.dB)

I would also define string formats like "{0}: {1}" in the resources so there would be a possibility of reordering words... I would not use this approach selectivly and not throughout the whole application..

And how about:

 String.Format("{0}: {1}", Volume, Resource.Muted_Volume) 
 // = Volume: Muted
 Resource.Muted_Volume
 String.Format("{0}: {1} (by user {2})", Volume, Resource.Muted_Volume, "xy") 
 // = Volume: Muted (by user xy)

The advantage is cutting the number of resource by the factor of 4-5.

Are there any hidden dangers of using this approach? Could someone give me an example (language) where this would not work correctly?

© Stack Overflow or respective owner

Related posts about localization

Related posts about resources