I have general design question.
How would you localize names of fields of a class in GWT ?
Imagine I have bean with imaginary annotation such as
class Person
{
@LocalizedName(value = "{Person.name}")
String name;
@LocalizedName(value = "{Person.address}")
String address;
}
I'll have resource bundle for English and German where these two values will be specified.
At some point in run-time I'll have instance of Class<Person>
and some String
that holds name of a field (e.g. name
).
I want somehow automagically get localized version of the name.
I know I can create my MyMessages interface and call its getString()
but that would require specifying convoluted method names such as MyMessages.person_address()
for every field of class that being localized which is not only uncool but also tend to break if I rename the property during refactoring.
That's how I do it today and it's very ugly.
Ideas ? Maybe I can use Generators ? Something else ? How you do it ?
If you want to do i18n in GWT, you should use their built in mechanisms. Google's apps are usually built to 320 permutations with GWT, so this method clearly works for tons of i18n and should not be that hard to implement.
Your options are:
Personally I perfer the UiBinder case. Whenever you need to use a string in a widget (i.e. Title, label, etc), you just wrap the string like so:
<ui:msg description="my great title">You're Title!</ui:msg>
Now when you compile, GWT will gather all of the strings together and identify them by MD5 hash. Use the same word/phrase multiple times, and it just appears once in your i18n properties files that GWT dynamically compiles for you from all the unique strings it finds. Now just translate that file and name it name_DE and GWT will swap all of the English words out for the German in the German permutation.
NOTE: Only the message interfaces are available from classes. You must use them for dynamically update strings in the app.
See the GWT i18n guide for more.
Viel Glück.