Springboot website resource internationalization based on browser

Source: Internet
Author: User
Tags i18n locale setting

Learn a little bit of programming every day PDF ebook, video tutorial free download:
Http://www.shitanlife.com/code

Proactively select resources based on browser Region 1. Create a resource file
  • Create messages directory under Resource directory
  • Create a messages_en_us.properties, messages_zh_cn.properties file. They are English and Chinese resources respectively.
  • The Messages.properties file is the default file.
  • Messages_en_us.properties Write content: Welcome = Welcome to login in Soa-watch systerm (中文版)
  • Messages_zh_cn.properties Write content: Welcome= Welcome to Soa-watch System (CH)
  • Messages.properties Write content: Welcome= Welcome to Soa-watch System (Default)
2. Configure Application.properties
   spring.messages.encoding=UTF-8   spring.messages.basename=/messages/messages
Using labels in 3.jsp pages
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
4. Modify the browser's locale
  • Enter in the browser address barabout:config
  • Modify Intl.accept_languages to view results
  • The above is Firefox browser
Implement control of resources in page link

Like the following page, click on the relevant link to complete the Web resource switch

1. Create Mylocaleresolver class Integrated Acceptheaderlocaleresolver class
public class MyLocaleResolver extends AcceptHeaderLocaleResolver {    private Locale myLocal;    @Override    public Locale resolveLocale(HttpServletRequest request) {        return myLocal==null?request.getLocale():myLocal;    }    @Override    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {        myLocal = locale;    }}
2. Create the I18nconfig class to initialize the Localeresolver object bean

The name of the object must be localeresolver,spring the container will be loaded automatically, otherwise it cannot be found.

@Configurationpublic class I18nConfig {private static Logger logger = LoggerFactory.getLogger(I18nConfig.class);    @Bean(name = "localeResolver")    public MyLocaleResolver myLocaleResolver(){        logger.info("#####cookieLocaleResolver---create");        MyLocaleResolver myLocaleResolver = new MyLocaleResolver();        myLocaleResolver.setDefaultLocale(Locale.ENGLISH);        logger.info("#####cookieLocaleResolver:");        return myLocaleResolver;}
Code implementation jumps in the 3.LoginControl class
  • Welcome page for welcome.jsp, display page
  • Language request for dynamic locale setting
@GetMapping("/welcome")public String welcome(){   logger.info("class:"+messageSource.getClass());    return "welcome";}@GetMapping("/language")public ModelAndView  language(HttpServletRequest request, HttpServletResponse response, String language){    Locale locale= request.getLocale();    logger.error(locale.toString());    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);    language=language.toLowerCase();    logger.info("language:"+language);    if(language==null||language.equals("")){        return new ModelAndView("welcome");    }else{        if(language.equals("zh_cn")){            localeResolver.setLocale(request, response, Locale.CHINA);        }else if(language.equals("en")){            localeResolver.setLocale(request, response, Locale.US);        }else{            localeResolver.setLocale(request, response, Locale.CHINA );        }    }    return new ModelAndView("redirect:welcome");}
4.welcome.jsp page Code
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

The implementation is complete and can be tested.

Learn a little bit of programming every day PDF ebook, video tutorial free download:
Http://www.shitanlife.com/code

Springboot website resource internationalization based on browser

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.