dawdler-client-plug-i18n
Module Introduction
Provides injection of I18nOperator on the web side.
1. Adding the Dependency to pom
<groupId>club.dawdler</groupId>
<artifactId>dawdler-client-plug-i18n</artifactId>
2. Usage
2.1 Using I18n
In a web environment, the locale is automatically detected according to the following priority:
- The
localeparameter in the URL (for example:?locale=zh_CN) - The
Accept-LanguageHTTP request header - The system default locale
Simply use the @I18nInjector annotation to mark a global variable as an I18nOperator-typed variable.
@Controller
public class UserController{
@I18nInjector("messages")// messages is the base name of the resource file, without the properties suffix
I18nOperator i18nOperator;
@RequestMapping(value = "/getUser", method = RequestMethod.GET)
public User getUser(String userId) {
String message = i18nOperator.get("user.welcome");// use the i18nOperator object
System.out.println(message);
return null;
}
}
2.2 Using It in an Interceptor
public class MyInterceptor implements HandlerInterceptor {
@I18nInjector("messages")// messages is the base name of the resource file, without the properties suffix
I18nOperator i18nOperator;
@Override
public boolean preHandle(Object controller, ViewForward viewForward, RequestMapping requestMapping) throws Exception {
String message = i18nOperator.get("interceptor.prehandle");// use the i18nOperator object
System.out.println(message);
return true;
}
@Override
public void postHandle(Object controller, ViewForward viewForward, RequestMapping requestMapping, Throwable ex) throws Exception {
String message = i18nOperator.get("interceptor.posthandle");// use the i18nOperator object
System.out.println(message);
}
@Override
public void afterCompletion(Object controller, ViewForward viewForward, RequestMapping requestMapping, Throwable ex) {
String message = i18nOperator.get("interceptor.aftercompletion");// use the i18nOperator object
System.out.println(message);
}
}