Duplicated values in resx files : what's the best approach to organize resource file entries?

StackOverflow https://stackoverflow.com/questions/19322191

  •  30-06-2022
  •  | 
  •  

Frage

I've been through much of the MSDN about localization but I'm still wondering how I should organize my resx files.

I usually encounter resx files having multiple keys with the same value which seems to be wrong to me. Typically, this happens when there is different keys for each screen/user control displaying the same text. In that cases, resx files look like this :

ScreenXXX_ControlXXX_DisableButtonText = Disable
ScreenYYY_ControlYYY_DisableButtonText = Disable
...

Some may argue that this brings flexibility by letting you change a specific text without impacting the other but obviously, changing "Disable" on all screen would be a pain. I feel like it makes much more sense to have a unique key that would express the "Disable feature" concept and that would be shared by multiple controls. Something like :

DisableFeatureButtonText = Disable

So questions are :

  • What is the recommended approach to create resource entries ? Should I create an entry that is "contextual" (= related to its location in the UI) or "semantical" ?
  • In which cases is it considered normal to have duplicated values in your resx files ?
  • More generally, Is there any convention or guidelines on these matters ?

Thanks

War es hilfreich?

Lösung

I am facing a situation very similar to yours were I have multiple screens and in many cases, control states, text, etc are duplicated across screens. Originally, I was going to just split the resx files up by screen and have duplicate key/value pairs, but when you really think about it, they are the same today, but that doesn't mean that one key/value pair could mean two different things in the future. Anyways, here is what I would do:

I would have a common resx file that contained keys that are common to all screens, etc. I would then have a resx files that are unique to the screen, but these resx files could also contain keys that are in the common resx and then you could have a method that would would check if their is a value in the specific resx and if it finds it, it uses that, otherwise, it uses the value from the common resx file.

I don't really think there is a convention, I typically name my keys the same way I name my variables.

I think it is OK to have duplicate keys if in the future, those keys, could potentially mean something else or need to have different values.

I like to think of resx files like database tables. You can stuff resx key/value into one table and have one long denormonalized table or you can split up the resx key/value into smaller tables. I prefer the latter because it allows for better scalability in the future.

Andere Tipps

You can use a global Default.resx file for all your resources that you use on multiple places. What you wanna put in there is up to you. I don't think there is a general rule. But things like Save, Disable, etc. are surely worth putting in there.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top