Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

9.3 I18nInterceptor


May 14, 2021 JFinal manual



The I18nInterceptor interceptor is an internationalized component for web applications, and here are the examples used in the freemarker template:

Configure I18nInterceptor as a global interceptor first
public void configInterceptor(Interceptors me) { me.add( new I18nInterceptor());
}

You can then get internationalized data _res freemarker by simply simply using the object
${_res.get("msg")}

The above code intercepts action requests by configuring I18nInterceptor, and then obtains internationalization data in the freemarker template file with an object named _res, and the specific workflow of I18nInterceptor is as follows:

  • l An attempt was made to obtain the locale parameter from the request through controller.getPara ("_locale") and, if so, to save it to a cookie
  • If controller.getPara ("_locale") does not get the parameter value, an attempt is made to obtain the locale parameter through controller.getcookie ("_locale").
  • If the above two steps still do not get the locale parameter value, use I18n. The value of defaultLocale is used as the locale value
  • Using the locale value obtained in the previous three steps, you get the Res object through I18n.use and pass the Res object to the page for use through controller.setAttr ("_res," res).
  • If I18nInterceptor. If isSwitchView is true, it also changes the view value of the render, switching the overall template file, and the source code can be viewed in detail.


The variable names "_locale" and "_res" in step I18nInterceptor above can be specified when the I18nInterceptor object is created and the default value is used when not specified. You can also customize more personalized features by inheriting I18nInterceptor and covering getLocalPara, getResName, getBaseName.


In some web systems, pages require too much internationalized text, and css and html are very different because of internationalization, for this scenario, you can make multiple sets of internationalized views with the same name, and store those views in locale subdirection classification, and finally use I The 18nInterceptor interceptor dynamically switches views based on locale, instead of interstates the text in the view one by one, simply set I18nInterceptor.isSwitchView to true, saving time and effort.