internationalization of a web application
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.
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.
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.
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.
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.
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.
精彩评论