views:

126

answers:

2

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 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)

What about simple cases like "User" vs "User: " vs "User - "? Can you think of any culture where ':' would be in a different place or maybe a different character would be used?

The advantage is cutting the number of resource by the factor of 2-3 in my application.

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

+2  A: 

You've identified the main drawback yourself when you ask the question:

Could someone give me an example (language) where this would not work correctly?

You are relying on the order of the words being the same in all languages. I'm not a language expert by any means, but I have worked on applications that were translated into other languages (typically French and German) and word order when building up strings was always an issue.

You'll probably be OK for simple phrases - the ones you give for example, but anything more complicated and you'll need to have separate resources for each one.

ChrisF
I would also define resources line NameValue = "{0}: {1}" and NameValueUnit = "{0}: {1} ({1})" so it would be possible to reorder the words
Hrvoje Prgeša
+1  A: 

Aside from issues with plural/singular, (which are not specific to this problem), I think this is safe for the majority of european languages since it is not using free text but mostly quantity and appelations: e.g. Command: the name of a command. Volume: quantity. You'll need to deal with word order, having a the template defined by the resource, but this is still only one resource for "format type" - the words in the template are still parameterized.

Even so, I would implement it using a strategy where you use the composition approach by default, but have the option to override for specific languages so the strings can be specified explicitly where the default composition rules break down.

mdma