Earlz
Earlz

Reputation: 63835

Is there a better way of managing localized strings?

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?

Upvotes: 12

Views: 958

Answers (2)

rodion
rodion

Reputation: 15029

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.

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

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.

Upvotes: 5

Related Questions