views:

93

answers:

4

Hello, working on internationalizating a web application, I am a bit blocked, because I don't know how to make my .properties dinamic. I mean, to get automatically static text from the application. Now I have, for instance, this one MessageBundle_de_DE.properties:

greetings=Hallo!  
inquiry=Wie geth's?   
farewell=Tchüs!

But this is static, I wrote theese three couples (key, value).
How could I make my keys according to my application?? I guess this is possible, regarding on documentation.

Using Spring framework and JSP technology.

Thanks in advance, I know this is maybe too general question.

+1  A: 

If you aren't using a web application framework, I would suggest using the Spring Framework, which provides useful functionality for web applications, including internationalization.

Bernard
Thanks, I was already using Spring framework, and I understand how easy could be internationalize my application, but I am block, because I don't know how to continue and link the web with the configuration
mujer esponja
A: 

The best way to internalize your application if you don't use any framework is to use database to store all your translations. Just a simple class which would load translations from db by the key and your problem is solved.

Vladimir Ivanov
I am using Spring framework. Thanks
mujer esponja
@Vladimir: It is not the best way to internationalize your application. It is far from the best, actually. It might be easy from developer stand point, but it will be a nightmare for Localization Engineers. It is a lot easier to correct (i.e. shorten) the translations when they are stored in properties files. Plus, how you suppose to modify CSS classes via DB?
Paweł Dyda
@Pawel Why do you need to store css in db? And second, all you need for localization engineers is a simple script which puts all the localization data to the db.
Vladimir Ivanov
@Vladimir: I do not, but it is often needed to tweak CSS classes. As for the second part, funny thing but I had a meeting today with other Globalization Professionals and we talked about it. This idea is just brilliant, but there are two issues: 1. You will need another build to verify the fix. 2. All it takes is just one simple mistake (i.e. by translators) and the script won't install these translations.
Paweł Dyda
+1  A: 

Since you're using spring, add the following bean to your application context:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages/messages" />
    <property name="cacheSeconds" value="0" />
</bean>

In your /WEB-INF/messages folder, create all the messages.properties files that you need i.e. messages_en_GB.properties, messages_DE.properties etc.

Then in your jsps, use the following spring provided tag:

<spring:message code="some.property.name" />

By default the locale should be the locale your user has set in their browser. To allow them to select a different Locale (and thus the correct language), you can also add this to your application context:

<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>

Then, you just need links that include a locale property like /?local=de to change to a German translation.

GaryF
Thank you so much!! Just 2 more questions: Where to put Messages.bundle and also, in code="some.property.name", do you mean code="Messages_de_DE.properties.key"?? And how to specify in which languaje I want the translation??
mujer esponja
I've added a little more on how to pick your translation. Your messages.properties go in the folder I mention in my post. When I say code="some.property.name", the value there should be the key for whatever value you want output. In your given example this would be, for example, code="greetings".
GaryF
Thanks again, but in the file I am testing this, I get Error Traced[line: 51] The prefix "spring" for element "spring:message" is not bound.
mujer esponja
You'll need to include the Spring taglib on your page:<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
GaryF
thanky very much for all your answers
mujer esponja
A: 

Looking for an easy and powerful way to internationalize your apps? Try gettext: http://www.gnu.org/software/gettext/manual/gettext.html it's used by most open-source software for Linux, but it can also work with Java. The steps are easy:

  • Write your program normally, as if it were non-localized: System.out.println("Hello world!!!")
  • Somewhere in the code create public static class named (say) S with method _. This method name should be short as will be used often. It should delegate to Gettext or ResourceBundle APIs to get the message.
  • All strings to localize surround with _(...): System.out.println(_("Hello world!!!"))
  • Run gettext command to extract all such _ strings
  • Translate extracted file to target languages
  • Run gettext again to compile and check such files

This you can add localization to any application (whether it will be written in Java, C, Python or whatever) very easily.

iirekm