Is there a better way of managing localized strings?
Asked Answered
G

2

12

I work on a product where we have to worry a bit about localization. Currently, this is the workflow for when I have to use(or add) a localized string:

  1. Search resources.resx file(which has hundreds of items)
  2. If found, then copy the name. Otherwise, add a new string and copy the name
  3. Then, use ResourceFactory.ResourceMgr.GetString("MY_MAGIC_STRING") (where ResourceMgr is just a static field to a ResourceManager)

This 3 step process for any strings is a real pain. Are there any patterns or ways to make this process easier?

Gabo answered 14/1, 2013 at 17:11 Comment(4)
Be careful about reusing the same string, there are phrases/sentences/words that have to be translated differently into other languages, depending on context. E.g. conjugation of verbs in Slavic languages depending on the subject of the sentence.Platinumblond
@Platinumblond of course. We usually try to keep things all together(especially by using String.Format style strings) and let the translators tell us when there's something we need to changeGabo
If this a clean code thing or make it execute faster?Cochard
@Blam for clean code primarily(though more performance never hurts)Gabo
N
5

Auto-generated files with access to each individual string are much easier to use - set "Custom tool" for RESX file to PublicResXFileCodeGenerator.

Code would look like:

using MyProject.Resources;
...
localizedText = Resources.SomeReasonableName;

Side notes:

  • having multiple RESX files along with auto-generated IDs have additional benefit of intellisense giving you reasonable number of choices.
  • depending on how translation is handled you may be better not worrying about duplicated text in RESX file (except maybe OK/cancel kind of strings). It may be easier to deal with duplicated strings at translation time.
Namara answered 14/1, 2013 at 17:32 Comment(2)
But this will only make step 3 slightly better, so it's not that big of an improvement, no?Platinumblond
@svick, your call. "Slightly" as in "compile time vs. run-time checks". Also if strings are organized into files by feature you get intellisense that with reasonable number of choices, if all in one file - not so much. But at least you get compile time errors instead of run-time.Namara
B
0

There is this Java solution that might give you some ideas: http://rodionmoiseev.github.com/c10n/

The idea is to store translations in the source code itself, using annotations on interface methods. Then, by using a special utility, you can dynamically create proxies (classes dynamically implementing the interface) that would return localised string value when invoking the interface method.

This way, "MY_MAGIC_STRING" is replaced with a call to MyMagicString() method, which gives you some spelling/type safety and makes it more refactoring friendly.

Bohs answered 15/1, 2013 at 14:42 Comment(1)
Posting an answer that contains just a link is not very useful. Could you add a short explanation of what the library does?Platinumblond

© 2022 - 2024 — McMap. All rights reserved.