Write in front
To consider supporting internationalization in the development of the project, the framework chooses the Spring MVC framework, where the relevant data are queried and how spring MVC is configured and internationalized.
Specific implementation
For the internationalization of spring MVC here I am based on the session, the following is done:
1. First we need to configure the following in the XML configuration file:
<bean id= "Messagesource" class= "Org.springframework.context.support.ResourceBundleMessageSource" > < !--internationalization information is in the file name-- <property name= "basename" value= "Lang.messages"/> <!-- If you cannot find the corresponding code in the Internationalized resource file, use this code as the name--- <property name= "Usecodeasdefaultmessage" value= "true"/> </bean>
The core configuration here is to configure the resource file path "Lang.messages" because the project is built through MAVEN, so the language resource files can be placed directly under the resources path of the MAVEN project.
2, based on the session or the cookie to intercept the configuration, the following practices:
<mvc:interceptors> <!--international operation interceptors if you are using (Request/session/cookie), you must configure-- <bean class= " Org.springframework.web.servlet.i18n.LocaleChangeInterceptor "/> </mvc:interceptors> <!-- Local parser--><bean id= "Localeresolver" class= "Org.springframework.web.servlet.i18n.SessionLocaleResolver"/ >
3. Write controller code to switch languages.
@RequestMapping ("/lang") @ResponseBody public String Lang (HttpServletRequest request) { string langtype = Request.getparameter ("Langtype"); if (Langtype.equals ("zh")) { locale locale = new Locale ("zh", "CN"); Request.getsession (). SetAttribute (Sessionlocaleresolver.locale_session_attribute_name,locale); } else if (langtype.equals ("en")) { locale locale = new locale ("en", "US"); Request.getsession (). SetAttribute (Sessionlocaleresolver.locale_session_attribute_name,locale); } else request.getsession (). SetAttribute (Sessionlocaleresolver.locale_session_attribute_name, Localecontextholder.getlocale ()); return null; }
What is known is that the locale locale is set by the language type passed by the front end, and then spring MVC internally loads the. porperties file for the specified language according to the locale's dynamic selection, and the front end is called by a specific expression.
3, prepare language documents, here Examples of Chinese, English:messages_en.properties and messages_zh.properties
In the form of key-value pairs, the contents of the front-end interface (various languages) are stored, and the contents of the English-language files are simple to read.
Ms.sysname=manager System[email protected] Cqms.langen=englishms.langzh=chinese
4, front-end interface call.
The project uses the Velocity template engine build page, which is called in the. vm file in the following form:
#springMessage ("Ms.copyright")
Spring MVC Internationalization Configuration