Localization: what is the best practice for using .NET resource file? RRS feed

  • Question

  • We use .NET Resource files to store resource key/value for translation.

    For an example, we have a form called "Form1.vb" which includes two labels (FirstName,  LastName, and Birthday), if we set "Localizable" to "true" in design mode.

    VS2010 will automatically generate Form1.resx. Then we add more resource file for different languages. Here we use French as an example. We add two more French resource files:

    First way:

    Form1.fr.resx (FirstName, LastName, BirthDay)

    Form1.fr-FR.resx (Birthday)

    We know the resource file has fallback feature.

    If the Form1.fr.resx already includes translation value for "FirstName" and "LastName" and for locale "fr-FR" there is no different than "fr" (here we make assumption), we normally do not add "FirstName" and "LastName" in the Form1.fr-FR.resx file. Instead we use resource fallaback feature. "Birthday" has different string for "fr" and "fr-FR", so we add Birthday "fr-FR" string into From1.fr-FR.resx.

    If we set locale as "fr-Fr" and when the Form1 loading and can not find the "FirstName" and "LastName", it looks for Form1.fr.resx file, it found and load them into UI.

    Second way: We include all resource keys/values in the Form1.fr-FR.resx. In that way, when we load Form1, we only look for Form1.fr-FR.resx.

    Form1.fr.resx (FirstName, LastName, BirthDay)

    Form1.fr-FR.resx (FirstName, LastName, BirthDay)

    Our question: which way is better? In our case, we have more than thousand resource files in our project.

    We try to think, if we have thousand resource files, we have to open two thousand resource files in first way or even three thousand files (if some newly added resource strings have not been translated into "fr" or "fr-Fr"). In second case, we only need to open one thousand resource file.

    Is the second way gain performance? Thx!



    • Moved by Bob Shen Wednesday, May 22, 2013 8:20 AM
    Tuesday, May 21, 2013 8:20 PM


  • I think technically both way can do but I personaly prefer the first one because of smaller resulting total file size.

    Think the second example as non-normalized table in database structure, where duplicated data exist across the tables.

    However I don't get why you'd think the first way has more file than the second one? The resulting numbers should nearly be the same (just number of files, not size). And for Forms that has no difference in translation between fr and fr-FR, you can actually omit creating the second file if it matters.

    If you mean the number of files to be "touched" to find out the string, I can reassure you that it only matters for ASP.NET forms. For WinForm applications the resources are compiled into the binaries so it doesn't affect the physical access time. And because of the fact that strings in "string resources" are not interned, depending on how much string are exactly the same in these similar locales, the second method will case the resulting binaries significantly bigger and therefore affect performance. (Bigger binary size = longer load time + bigger chance of cache miss)

    • Marked as answer by Mike Feng Monday, May 27, 2013 10:54 AM
    Wednesday, May 22, 2013 9:30 AM