Configure and use i18n in Spring

Source: Internet
Author: User
Tags i18n

Configure and use i18n in Spring
Spring i18n Configuration:

 
 
  
  
  
  
  
   
    
     classpath:com/hupu/nac/i18n/message
    
   
  
 

Use i18n OF Spring:

Package com. joyce. enums; import java. util. locale;/*** language enumeration * @ author Joyce. luo * @ date 09:38:27 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2016 */public enum EnumLang {ZH_CN ("zh-CN ", locale. SIMPLIFIED_CHINESE), ZH_TW ("zh-TW", Locale. TRADITIONAL_CHINESE), EN ("en", Locale. ENGLISH), JA ("ja", Locale. JAPAN); private 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;}/*** obtain the enumerated value by ID * @ param lang ID * @ return ID corresponding to the enumerated value * @ author Joyce. luo * @ date 11:02:45 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */public static Locale getLocale (String lang) {for (EnumLang enumLang: EnumLang. values () {if (enumLang. getLang (). equals (lang) {return enumLang. getLocale () ;}return Locale. ENGLISH ;}}

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. joyce. enums. enumLang; import com. joyce. util. cookieUtil; import com. joyce. util. springUtil;/*** Spring i18n * @ author Joyce. luo * @ date 09:26:03 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2016 */public class I18NUtil {private static final Logger logger = Logger. getLogger (I18NUtil. class); private static MessageSource messageSource = (MessageSource) SpringUtil. getApplicationContext (). getBean ("messageSource"); private static final Object [] objs = null; /*** obtain internationalization information ** @ param key corresponds to the key * @ param locale language * @ param objects parameter * @ return internationalization information * @ author Joyce. luo * @ date 10:19:00 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2016 */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. ENGLISH;} 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 messageSource. getMessage (key, objects, locale);}/*** get internationalization information * @ param key corresponds to the key * @ param locale language * @ return internationalization information * @ author Joyce. luo * @ date 10:19:00 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2016 */public static String getMessage (String key, Locale locale) {return getMessage (key, locale, objs);}/*** get internationalization information * @ param key corresponds to the key * @ param objects parameter * @ return internationalization information * @ author Joyce. luo * @ date 10:19:00 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2016 */public static String getMessage (String key, Object... objects) {return getMessage (key, null, objects );} /*** obtain internationalization information ** @ param key corresponding key * @ param lang language * @ return internationalization information * @ author Joyce. luo * @ date 11:30:03 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2016 */public static String getMessage (String key, String lang) {return getMessage (key, EnumLang. getLocale (lang);}/*** obtain internationalization information * @ param key corresponds to the key * @ return internationalization information * @ author Joyce. luo * @ date 10:19:00 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2016 */public static String getMessage (String key) {return getMessage (key, null, objs );}}

For reference only. Interested parties can study this article in depth.

Related Article

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.