Reputation: 10502
I am using spring MVC. In my app a user should be able to post comments/text in multiple language (english, japneese,chineese,polish etc). These post will be stored in DB also. So how can i enable multi language support in my UI side and server side
What are the things i need to do. I seen some internationalization example but my confusion is that it use properties file to store meaning of each word. and that is the issue that how can i store each and every words. Is it something i need? An example will be good enough
Upvotes: 0
Views: 4124
Reputation: 111
For Spring l18n with database usage extend AbstractMessageSource. Java conifg:
@Bean
public MessageSource messageSource(){
DataBaseMessageSource source = new DatabaseMessageSource();
return source;
}
@Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(Locale.ENGLISH); //setup default locale
return resolver;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("lang"); //will change current user locale when hit url with ?lang
registry.addInterceptor(interceptor);
}
and
public class DatabaseMessageBandle extends AbstractMessageSource {
@Autowired
private FooRepository fooRepo;
@Override
protected MessageFormat resolveCode(String code, Locale locale) {
String message = getMessage(code, locale);
MessageFormat messageFormat = createMessageFormat(message, locale);
return messageFormat;
}
@Override
protected String resolveCodeWithoutArguments(String code, Locale locale) {
return getMessage(code, locale);
}
private String getMessage(String code, Locale locale) {
String message = fooRepo.getMeesageByCodeAndLocale(code, locale); //better not do this
return message;
}
}
and if you need to get current locale in controller:
@RequestMapping
public String index(Locale locale) { //you will have it
//or use LocaleContextHolder.getLocale, and it will return current thread locale
return "index";
}
Upvotes: 0
Reputation: 364
Most websites have Language link (i.e. en for English, fr for French, etc.). Therefore, you sort out the differing languages depending on the language link that the user has chosen.
Another way, would be to have some sort of auto-detection, so if the user enters some common words that belong specifically to a certain language, then you can highlight the auto selected language and also give the user the option to override the auto-detection and manually select the language. A good example of this is Google's translate website.
Also, make sure that every component from front-end to back-end is set for UTF-8 encoding. That is, your web/application server (e.g. Tomcat), your application framework (e.g. Spring) and your database including your IDE setting and that all files including jsp/html files are saved as UTF-8 encoding and have UTF-8 declared (such as having... <%@ page language="java" session="false" pageEncoding="UTF-8" contentType="text/html; charset=utf-8"%>
and <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
in your jsp/html files).
Upvotes: 0
Reputation: 308763
Yes, you need to do what the examples tell you. You don't have to store the meaning, but you do have to have one property for every string you want to internationalize in the UI.
You need to realize that the Spring I18N examples only deal with UI display in different languages. Multi-language in the database will require a separate effort.
You need a lot more expertise than you'll get from an example here or on the web.
Upvotes: 2