Spring's i18n configuration and use

Source: Internet
Author: User
Tags getmessage i18n locale

Spring's i18n configuration:
<!--conf:i18n  --><bean id= "Messagesource" class= " Org.springframework.context.support.ReloadableResourceBundleMessageSource "><property name=" Fallbacktosystemlocale "value=" false "/><property name=" Usecodeasdefaultmessage "value=" true "/>< Property Name= "Defaultencoding" value= "UTF-8"/><property name= "Cacheseconds" value= "$"/><property name = "Basenames" ><list><value>classpath:com/hupu/nac/i18n/message</value></list></ Property></bean>

Spring's i18n use:

Package Com.joyce.enums;import java.util.locale;/** * Language enumeration * @author Joyce.luo * @date 2016-1-5 Morning 09:38:27 * @version V3.0 * @since tomcat6.0,jdk1.6 * @copyright Copyright (c) */public enum Enumlang {zh_cn ("ZH-CN", Locale.simplified_chines E), ZH_TW ("ZH-TW", Locale.traditional_chinese), en ("en", locale.english), ja ("ja", Locale.japan);p rivate String Lang; Private Locale locale;public String Getlang () {return lang;} public void Setlang (String lang) {this.lang = lang;} Public locale GetLocale () {return locale;} public void SetLocale (locale locale) {this.locale = locale;} Private Enumlang (String lang, locale locale) {This.lang = Lang;this.locale = locale;} /** * Gets the enumeration value according to the ID * @param the lang ID * @return ID corresponding to the enumeration value * @author Joyce.luo * @date 2015-5-8 Morning 11:02:45 * @version V3.0 * @sin CE tomcat6.0,jdk1.6 * @copyright Copyright (c) */public static Locale GetLocale (String lang) {for (Enumlang Enumlang : Enumlang.values ()) {if (Enumlang.getlang (). Equals (lang)) {return Enumlang.getlocale ();}} Return LoCale. 中文版;}}

Package Com.joyce.util.i18n;import Java.util.locale;import Javax.servlet.http.httpservletrequest;import Org.apache.commons.lang3.stringutils;import Org.apache.log4j.logger;import Org.apache.struts2.servletactioncontext;import Org.springframework.context.messagesource;import Com.hupu.nac.enums.enumlang;import Com.hupu.nac.util.cookieutil;import com.hupu.nac.util.springutil;/** * Spring i18n * @author Joyce.luo * @date 2016-1-5 a.m. 09:26:03 * @version V3.0 * @since tomcat6.0,jdk1.6 * @copyright Copyright (c) */public class I18nutil {private static final Logger Logger = Logger.getlogger (i18nutil.class);p rivate static Message Source Messagesource = (messagesource) springutil.getapplicationcontext (). Getbean ("Messagesource");p rivate static  Final object[] Objs = null;/** * Get internationalized Information * @param key corresponding key * @param locale language * @param objects parameter * @return Internationalization information * @author Joyce.luo * @date 2016-1-5 a.m. 10:19:00 * @version V3.0 * @since tomcat6.0,jdk1.6 * @copyright Copyright (c) */public Static STRing getMessage (String key, locale locale, object...objects) {if (Stringutils.isempty (key)) {return stringutils.empty;} if (null = = locale) {HttpServletRequest request = Servletactioncontext.getrequest (); if (null = = Request) {locale = locale. 中文;} else {String lang = cookieutil.getcookiebyname (Request, "language"). GetValue (); if (Stringutils.isempty (lang)) {locale = Locale.english;} else {locale = Enumlang.getlocale (lang);}}} Logger.info ("Spring I18---> key:[" + key + "],lang:[" + locale.getlanguage () + "],params:[" + objects + "]"); return me Ssagesource.getmessage (Key, objects, locale);} /** * Get international information * @param key corresponding key * @param locale language * @return Internationalization information * @author Joyce.luo * @date 2016-1-5 a.m. 10:19:00 * @versi On V3.0 * @since tomcat6.0,jdk1.6 * @copyright Copyright (c) */public static string GetMessage (String key, Locale Loc ALE) {return getMessage (key, Locale, OBJS);} /** * Get international information * @param key corresponding key * @param objects parameter * @return Internationalization information * @author Joyce.luo * @date 2016-1-5 10:19:00 *@version V3.0 * @since tomcat6.0,jdk1.6 * @copyright Copyright (c) */public static string GetMessage (string key, Obje Ct... Objects) {return getMessage (key, null, objects);}  /** * Get international information * @param key corresponding key * @param lang language * @return Internationalization information * @author Joyce.luo * @date 2016-1-6 a.m. 11:30:03 * @version V3.0 * @since tomcat6.0,jdk1.6 * @copyright Copyright (c) */public static string GetMessage (string key, String lang) {return GetMessage (key, Enumlang.getlocale (Lang));} /** * Get international information * @param key corresponding key * @return Internationalization information * @author Joyce.luo * @date 2016-1-5 Morning 10:19:00 * @version V3.0 * @since to mcat6.0,jdk1.6 * @copyright Copyright (c) */public static String GetMessage (string key) {return getMessage (key, NULL, OBJS);}}

For reference only, the interested person may study in depth, I hold the guidance to start the attitude to write this article

Spring's i18n configuration and use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.