栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot的国际化使用

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot的国际化使用

在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中国际化的使用。

在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解springboot和thymeleaf的使用,可以去看我的上一篇文章《SpringBoot集成Thymeleaf》。

新建一个springboot项目,pom文件代码如下:



    4.0.0

    com.dalaoyang
    springboot_internationalization
    0.0.1-SNAPSHOT
    jar

    springboot_internationalization
    springboot_internationalization

    
 org.springframework.boot
 spring-boot-starter-parent
 1.5.10.RELEASE
  
    

    
 UTF-8
 UTF-8
 1.8
    

    
 
     org.springframework.boot
     spring-boot-starter-web
 

 
     org.springframework.boot
     spring-boot-devtools
     runtime
 
 
     org.springframework.boot
     spring-boot-starter-test
     test
 
 
     org.springframework.boot
     spring-boot-starter-thymeleaf
 

 
     net.sourceforge.nekohtml
     nekohtml
     1.9.15
 
    

    
 
     
  org.springframework.boot
  spring-boot-maven-plugin
     
 
    

从上面可以看出,其实和之前结合thymeleaf的时候一样。接下来给大家看一下application.propertie配置:

##端口号
server.port=8888

##去除thymeleaf的html严格校验
spring.thymeleaf.mode=LEGACYHTML5

#设定thymeleaf文件路径 默认为src/main/resources/templates
spring.freemarker.template-loader-path=classpath:/templates

新建IndexController

@Controller
public class IndexController {

    @RequestMapping("/")
    public String hello(Model model){
 return "index";
    }
}

到这里可以看出来,其实和整合thymeleaf一样。

接下来我们要加入国际化的关键,在resources里面新建messages.properties(默认配置),messages_en_US.properties(英文),messages_zh_CN.properties(中文)

其中messages.properties里面加入:

message = 欢迎使用国际化(默认)

messages_en_US.properties里面加入:

message = Welcome to internationalization (English)

messages_zh_CN.properties里面加入

message = u6b22u8fceu4f7fu7528u56fdu9645u5316uff08u4e2du6587uff09

然后在templates下新建index.html,代码如下:




    
    Title



English(US)
简体中文

创建国际化配置文件,I18Config 代码如下:

package com.dalaoyang.config;

import java.util.Locale;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
@EnableAutoConfiguration
@ComponentScan

public class I18Config extends WebMvcConfigurerAdapter{
    @Bean
    public LocaleResolver localeResolver() {
 SessionLocaleResolver slr = new SessionLocaleResolver();
 // 默认语言
 slr.setDefaultLocale(Locale.US);
 return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
 LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
 // 参数名
 lci.setParamName("lang");
 return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
 registry.addInterceptor(localeChangeInterceptor());
    }
}

最后修改IndexController,修改成如下:

package com.dalaoyang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.Model;

@Controller
public class IndexController {

    @Autowired
    private MessageSource messageSource;

    @RequestMapping("/")
    public String hello(Model model){
 Locale locale = LocaleContextHolder.getLocale();
 model.addAttribute("message", messageSource.getMessage("message", null, locale));
 return "index";
    }
}

现在启动项目,访问http://localhost:8888/

然后点击中文或者English就可以自由切换语言了。

源码下载 :大老杨码云

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235307.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号