Springboot Diary--International Chapter

Source: Internet
Author: User
Tags locale

It sounds like a big international, starting with the browser language, or the page in the English and Chinese switch, the page text in other languages and the language to switch, such as:

We want this feature to be implemented, click Chinese, the page is Chinese, click English is English.

Internationalization Configuration

Then we see, springboot by default is in the language of your browser to switch between English and Chinese, configuration files, we can write in resources:

1. Create a new package called "i18n", which we will use to store the internationalized configuration, and then under this package, we will build a few properties configuration files to configure the language:

mode, we create 3 files, which are login.properties when no language configuration is in effect; the login_zh_cn.properties of Chinese entry into force; the login_en_us.properties in force in English;

This is the combination of the following lines: File name _ region _ language. Properties; When we name the makefile, idea will also help us identify that this is an internationalized configuration package that is automatically converted to the following pattern:

Of course, as in the mode, we need to add the configuration file, directly in the package right-click New can be

Convenient for many ~

2. We will make some changes in these configuration files, first click on the Login_en_us configuration file, and then click the Resource Bundle button below, switch the editing mode:

Follow the method, click the plus sign, add a key, we named Login.tip is the page used to the meaning of the hint

Next, according to the amount we need to convert the page, to do the configuration, such as (I will wait for the login page to be used to configure all):

Not yet, don't worry, look complicated, learn to be simple, behind we need to refer to these values on the page, see.

HTML page-configuration

Based on the previous article, we learned about the role of the @ and # in the syntax of Thymeleaf, and see how to use it first, directly:

In front of the form we will all the required parameters in the form of #{xx.yy}, in accordance with the configuration of the internationalization parameters are set, in order to use the template, we need the Th:text and other parameters to replace the original parameters. (Note that the original page has the same text, placeholder and other parameters)

And to make the thymeleaf template effective, don't forget to add this xmlns to the HTML parameter:

<! DOCTYPE html>

In this way, we can switch the language of the browser in the case of switching between Chinese and English, the setting method to Chrome example (need English to add the English top):

custom configuration to make the Chinese and English transitions in our page take effect

Patient classmates, let's take a look at the topic, how to make their own configuration effective:

1. At the top of the article, we see the HTML of the Chinese and English buttons, and we look at how to configure:

Note that the page I will visit here is actually; LOCALHOST:8080/INDEX.HTML?L=ZH_CN, which is the link we generated after clicking the Chinese button. In Thymeleaf's template syntax, the parameter is not "? ", instead of using parentheses, then the parameters are set in the form of key=value, note the single quotes;

2. In order for the custom configuration to take effect, all we have to do is overwrite or change the default configuration, then we create a new file Mylocaleresolver to implement the function of Localeresolver interface;

How to: (Because our region is configured with an underscore "_", so here is the delimiter to get the region and language, put it in the request)

 Packagecom.iceodin.common.component;Importorg.springframework.util.StringUtils;ImportOrg.springframework.web.servlet.LocaleResolver;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportJava.util.Locale;/*** Carry regional information on the link*/ Public classMylocaleresolverImplementsLocaleresolver {@Override PublicLocale Resolvelocale (httpservletrequest httpservletrequest) {String L= Httpservletrequest.getparameter ("L"); Locale Locale=Locale.getdefault (); if(!Stringutils.isempty (L)) {string[] Split= L.split ("_"); Locale=NewLocale (Split[0], split[1]); }        returnlocale; } @Override Public voidSetLocale (httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, locale locale) {}}
3. Let our configuration take effect, of course, to add the configuration to the Springboot container, so in the previous configuration file, add the bean:

4. We'd better look at the results: The default language display, because the browser default Chinese, so:

Then click 中文版 below to see the URL and page changes:

We will then click on Chinese:

Fix It ~

Springboot Diary--International Chapter

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.