Spring Boot and Thymeleaf (1): Internationalization

Source: Internet
Author: User
Tags access properties i18n locale

In Thymeleaf there is a message expression: #{...}, which enables internationalization.

In my use of this feature, encountered a problem, according to Java EE development of the Subversion Spring boot combat above the code, there are the following issues, I believe many people have encountered.

?? Home.welcome_zh_cn??

Recommend a blog here, there are solutions. Play Spring boot--internationalization

I also want to record my own code.

A. Catalog Preview

Here I did not according to the information, the properties file is built in the index.html same level directory, nor the name is named: index.properties

That's the way it was before, but there's the problem.

Two. properties File contents

Messages.properties:

Welcome=welcome here!  Welcome   Welcome!

This is the time when there is no language match to show

Messages_en_us.properties:

Welcome=welcome here!

Messages_zh_cn.properties:

Welcome= Welcome Welcome!

Three. Controller

 PackageOrg.elvin.learn.springboot.controller;ImportOrg.elvin.learn.springboot.pojo.Book;ImportOrg.joda.time.DateTime;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.GetMapping;Importorg.springframework.web.bind.annotation.RequestMapping;Importjava.util.ArrayList;Importjava.util.List;/*** Author:elvin * DATE:2017/12/13 9:29 * Description:*/@Controller @requestmapping ("Thy") Public classThycontroller {@GetMapping ("Index")     PublicString Index (model model) {Model.addattribute ("Color", "red"); return"Thy/index"; }}

Four. View

<! DOCTYPE html>/*accessing properties in model by [[$ {}]]*/. bgcolor {background-Color:[[${color}]}</style>class= "Panel Panel-primary" > <divclass= "Panel-heading" > class= "Panel-title" > Welcome class= "Panel-body" > <!--$ {} variable expression, you can access properties in model--<spanclass= "bgcolor" th:utext= "#{welcome}" >hahaha</span> </div></div></body>

Five. Results

Use Firefox (you can adjust the display language).

The first time it was opened, it was already normal to display. No more errors.

At this point, adjust the Firefox default display language

Refresh page:

Someone may have noticed that I have two links here that I can use to toggle the display. In the way above, it is not possible to switch so. Some changes need to be made at this time

Six. Custom display

Adding a configuration file

 Packageorg.elvin.learn.springboot.conf;ImportJava.util.Locale;Importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.web.servlet.LocaleResolver;ImportOrg.springframework.web.servlet.config.annotation.InterceptorRegistry;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;ImportOrg.springframework.web.servlet.i18n.LocaleChangeInterceptor;Importorg.springframework.web.servlet.i18n.sessionlocaleresolver;@ Configuration@enableautoconfiguration@componentscan Public classLocaleconfigextendsWebmvcconfigureradapter {@Bean Publiclocaleresolver Localeresolver () {sessionlocaleresolver SLR=NewSessionlocaleresolver (); //Default Language//Slr.setdefaultlocale (locale.us);        returnSLR; } @Bean Publiclocalechangeinterceptor Localechangeinterceptor () {Localechangeinterceptor LCI=NewLocalechangeinterceptor (); //Name of parameterLci.setparamname ("Lang"); returnLCI; } @Override Public voidaddinterceptors (Interceptorregistry registry) {Registry.addinterceptor (Localechangeinterceptor ()); }}

The default language here can be set or not set. If not set, it will determine which message is displayed according to the browser's default language.

Seven. Results

You can choose to show that language according to Lang.

Spring Boot and Thymeleaf (1): Internationalization

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.