Reputation: 3563
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.
Upvotes: 0
Views: 1084
Reputation: 57
To internationalize applications I implemented a Message Compiler, which creates the resource bundle files and constant definitions as Java enums or static final strings for the keys from one single source file. So the constants can be used in the Java source code, which is a much safer way than to use plain strings. The message compiler cannot only be used for Java. It creates also resource files and constants for Objective-C or Swift and can be extended for other programming environments.
Upvotes: 0
Reputation: 9406
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:
This you can add localization to any application (whether it will be written in Java, C, Python or whatever) very easily.
Upvotes: 0
Reputation: 24340
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.
Upvotes: 2
Reputation: 43098
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.
Upvotes: 0
Reputation: 7961
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.
Upvotes: 1