5.2 page staff management system of international

A. Pages internationalization

1. Step:

  1. The IDEA set is UTF-8 character encoding

  2. Establish login.properties, login_en_US.properties, login_zh_CN.properties in the resources directory is used to configure the corresponding Chinese and English

  3. Message information is provided in a position application.properties

  4. Modified front page, using Thymeleaf acquisition message field, symbol # {...}, Note that the link setting handover parameters passed with English key-value pairs

  5. Write a custom parser locale

  6. Add to custom configuration file to take effect

  7. Start the test

2. Set the IDEA UTF-8 character encoding

3. Establish login.properties, login_en_US.properties, login_zh_CN.properties in the resources directory is used to configure the corresponding Chinese and English

(1) the final results:

(2) In the resources we create a resource file i18n directory, the establishment of a login.propetries file, there is a login_zh_CN.properties, found IDEA automatically recognizes we need to do international operations; folder changed

(3) We can go in it to create a new file login_en_US

 

(4) Next, we'll write the configuration, we can see below there is another view of the idea

This view we click on the + sign can be added directly to the property; we create a login.tip, you can see the edge of the box there are three files can be entered

We add about the contents of the home page

Then add another turn to page content

 

Then go see our configuration file

login.properties: Default

login.btn = Log 
login.password = password 
login.remember = Remember me 
login.tip = Please sign 
login.username = username

 login_en_US.properties English:

login.btn=Sign in
login.password=Password
login.remember=Remember me
login.tip=Please sign in
login.username=Username

login_zh_CN.properties中文:

login.btn=登录
login.password=密码
login.remember=记住我
login.tip=请登录
login.username=用户名

4.在application.properties中设置消息信息的位置

# 设置为i18n配置文件的位置
spring.messages.basename=i18n.login

5.修改前端页面,使用Thymeleaf获取消息字段,符号为#{...},设置切换中英文链接注意参数传递用键值对

参数传递: th:href="@{/index.html(l='zh_CN')}" ,多个参数中间用逗号隔开

例如: th:href="@{/index.html(l='zh_CN',f=#{xxx})}" 

 

 

 

6.编写自定义的语言环境解析器

  • 在config文件夹下添加MyLocaleResolver.java文件

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

//可以在链接上携带区域信息
public class MyLocaleResolver implements LocaleResolver {

    //解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest request) {

        // 获得请求中的需要的语言参数
        String language = request.getParameter("l");
        Locale locale = Locale.getDefault(); //如果没有获取到就使用系统默认的
        //如果请求链接不为空
        if (!StringUtils.isEmpty(language)){
            //分割请求参数
            String[] split = language.split("_");
            //国家,地区
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
View Code

7.添加到自定义配置文件中,使其生效

  • 在MyMvcConfig.java中配置

// 自定义的国际化组件就生效了
@Bean
public LocaleResolver localeResolver(){
    return new MyLocaleResolver();
}

 

 

 8.测试成功

 

Guess you like

Origin www.cnblogs.com/zhihaospace/p/12387409.html