I18nInterceptor


I18nInterceptor interceptor is an international component provided for web applications. The following is an example used in the freemarker template:

//First configure I18nInterceptor as a global interceptor
public void configInterceptor(Interceptors me) { me.add(new I18nInterceptor());
}

// Then you can get the internationalized data through the _res object in freemarker
${_res.get("msg")}

The above code is configured with I18nInterceptor to intercept the action request, and then the internationalized data can be obtained through the object named _res in the freemarker template file , The specific workflow of I18nInterceptor is as follows:


  • l attempts to obtain the locale parameter from the request through controller.getPara("_locale"), If obtained, save it to cookie
  • If controller.getPara("_locale") does not obtain the parameter value, try Obtain the locale parameter through controller.getCookie("_locale")
  • If the locale parameter value is still not obtained in the above two steps, use the value of I18n.defaultLocale. Use the locale value
  • ##Use the locale value obtained in the previous three steps, get the Res object through I18n.use(locale), and passcontroller.setAttr(“_res”, res) passes the Res object to the page for use
  • If I18nInterceptor.isSwitchView is true, the view value of render will also be changed to implement the overall template file Switch, please view the source code for details.


The variable names "_locale" and "_res" in the above steps I18nInterceptor can be specified when creating the I18nInterceptor object. , the default value will be used if not specified. You can also customize more personalized functions by inheriting I18nInterceptor and overriding getLocalPara, getResName, and getBaseName.

In some web systems, there are too many texts that need to be internationalized on the page, and the css and html are also very different due to internationalization. For this application scenario, first directly create multiple sets of internationalization with the same name. views, and store these views in locale subdirectories. Finally, use the I18nInterceptor interceptor to dynamically switch views according to locale. Instead of having to perform international switching of the text in the view one by one, you only need to set I18nInterceptor.isSwitchView to true, which saves time and effort.