1)、编写国际化配置文件;

2)、使用ResourceBundleMessageSource管理国际化资源文件
3)、在页面使用fmt:message取出国际化内容

步骤:

1)、编写国际化配置文件,抽取页面需要显示的国际化

文件内容:

2)、使用thymeleaf的语法去引用(jsp是fmt)

<form class="form-signin" action="dashboard.html">
<img class="mb-4" src="asserts/img/bootstrap-solid.svg" alt="" width="" height="">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{index_top}">Please sign in</h1>
<label class="sr-only">Username</label>
<input type="text" class="form-control" th:placeholder="#{index_Username}" placeholder="Username" required="" autofocus="">
<label class="sr-only">Password</label>
<input type="password" class="form-control" th:placeholder="#{index_password}" placeholder="Password" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> [[#{index_rember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted">© -</p>
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
</form>

效果是根据浏览器的语言设置进行国际化的显示

3)运行查看

系统为英文

系统语言选择中文:

 

SpringBoot自动配置好了管理国际化资源文件的组件

@Configuration
@ConditionalOnMissingBean(
value = {MessageSource.class},
search = SearchStrategy.CURRENT
)
@AutoConfigureOrder(-)
@Conditional({MessageSourceAutoConfiguration.ResourceBundleCondition.class})
@EnableConfigurationProperties
public class MessageSourceAutoConfiguration {
private static final Resource[] NO_RESOURCES = new Resource[]; public MessageSourceAutoConfiguration() {
} @Bean
@ConfigurationProperties(
prefix
= "spring.messages"
)
public MessageSourceProperties messageSourceProperties() {
return new MessageSourceProperties();
} @Bean
public MessageSource messageSource(MessageSourceProperties properties) {
//管理国际化的
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(properties.getBasename())) {
////设置国际化资源文件的基础名(去掉语言国家代码的)
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
}
if (properties.getEncoding() != null) {
messageSource.setDefaultEncoding(properties.getEncoding().name());
} messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
Duration cacheDuration = properties.getCacheDuration();
if (cacheDuration != null) {
messageSource.setCacheMillis(cacheDuration.toMillis());
} messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
return messageSource;
} protected static class ResourceBundleCondition extends SpringBootCondition {
private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap(); protected ResourceBundleCondition() {
} public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
////我们的配置文件可以直接放在类路径下叫messages.properties
String basename = context.getEnvironment().getProperty("spring.messages.basename", "messages"
);
ConditionOutcome outcome = (ConditionOutcome)cache.get(basename);
if (outcome == null) {
outcome = this.getMatchOutcomeForBasename(context, basename);
cache.put(basename, outcome);
} return outcome;
} private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context, String basename) {
Builder message = ConditionMessage.forCondition("ResourceBundle", new Object[]);
String[] var4 = StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename));
int var5 = var4.length; for(int var6 = ; var6 < var5; ++var6) {
String name = var4[var6];
Resource[] var8 = this.getResources(context.getClassLoader(), name);
int var9 = var8.length; for(int var10 = ; var10 < var9; ++var10) {
Resource resource = var8[var10];
if (resource.exists()) {
return ConditionOutcome.match(message.found("bundle").items(new Object[]{resource}));
}
}
}
return ConditionOutcome.noMatch(message.didNotFind("bundle with basename " + basename).atAll());
}
private Resource[] getResources(ClassLoader classLoader, String name) {
String target = name.replace('.', '/'); try {
return (new PathMatchingResourcePatternResolver(classLoader)).getResources("classpath*:" + target + ".properties");
} catch (Exception var5) {
return MessageSourceAutoConfiguration.NO_RESOURCES;
}
}
}
}

容器中没有就使用默认的

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(
prefix = "spring.mvc",
name = {"locale"}
)
public LocaleResolver localeResolver() {
if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
} else {
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
}

原理:

国际化的Local(区域信息对象);LocalResolver(获取区域信息对象)

public class MyLocaleResolver implements LocaleResolver {

    @Override
public Locale resolveLocale(HttpServletRequest request) {
String l = request.getParameter("l");
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(l)){
String[] split = l.split("_");
locale = new Locale(split[],split[]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
resolveLocale(httpServletRequest);
}

加入容器

@Bean(name="localeResolver")
public LocaleResolver MyLocaleResolver(){
return new MyLocaleResolver();
}

值得一说的是

@Bean()
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}

LocaleResolver 如果后面不跟localeResolver的话  就需在@Bean指定name

点击即可进行切换

9、SpringBoot-CRUD国际化的更多相关文章

  1. SpringBoot 国际化配置,SpringBoot Locale 国际化

    SpringBoot 国际化配置,SpringBoot Locale 国际化 ================================ ©Copyright 蕃薯耀 2018年3月27日 ht ...

  2. 配置和修改springboot默认国际化文件

    SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.mess ...

  3. SpringBoot的国际化使用

    在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中国际化的使用. 在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解 ...

  4. SpringBoot整合国际化功能

    (1).编写国际化配置文件 在resources下新建i18n文件夹,并新建以下文件 ①index.properties   username=username ②index_en_US.proper ...

  5. SpringBoot日记——国际化篇

    听起来高大上的国际化,起始就是在利用浏览器语言,或者页面中的中英文切换,将页面的文字在其他语言和中文进行切换,比如: 我们想让这个功能实现,点击中文,页面就是中文的,点击英文就是英文的. 国际化配置 ...

  6. SpringBoot资源国际化

    Springboot根据浏览器实现网站资源国际化 根据浏览器地区主动选择资源 1.创建资源化文件 resource目录下创建messages目录 创建messages_en_US.properties ...

  7. SpringBoot配置国际化

    1).国际化 1).编写国际化配置文件: 2).使用ResourceBundleMessageSource管理国际化资源文件 3).在页面使用fmt:message取出国际化内容 步骤: 1).编写国 ...

  8. SpringBoot 消息国际化配置

    一.目的 针对不同地区,设置不同的语言信息. SpringBoot国际化配置文件默认放在classpath:message.properties,如果自定义消息配置文件,需要application.p ...

  9. springboot实现国际化

    1.编写配置文件 2.在application.properties中添加 i18n指的是国际化所在包名 3.实现国际化的接口 4.在配置类中

  10. SpringBoot整合国际化I18n

    本文主要实现的功能: 从文件夹中直接加载多个国际化文件 后台设置前端页面显示国际化信息的文件 实现 国际化项目初始化,简单看下项目的目录和文件 在resource下创建国际化文件 messages.p ...

随机推荐

  1. 比较2个文件内容不同行的shell脚本

    第一种:grep命令法 命令如下:grep -vxFf file1 file2 > a.txt 其中file2是大文件,file1是小文件 第一种:comm命令法 命令如下:comm  file ...

  2. express组件学习

    一.express 可以做:web application.api... 特性: 适合写简单的路由系统 集成很多模板引擎 中间件系统 二.请求与响应 var express = require('ex ...

  3. 判断sql是否存在特定的对象

    1 判断数据库是否存在if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] 2 判断表是否存 ...

  4. linux cut: invalid byte, character or field list Try 'cut --help' for more information.

    1. 概述 centos执行简单shell 脚本 报错 cut: invalid byte, character or field listTry 'cut --help' for more info ...

  5. JS封装继承函数

    function extend(child,parent){ var F=function(){} F.prototype=parent.prototype; child.prototype=new ...

  6. vue.js与angular.js的区别(个人)

    刚进入实训 讲师就要发一些什么比较高大上的东西,本人才疏学浅  浅浅的分享一下angularjs 和vue.js的区别.只是简单的理解一下 大神勿喷. 生实训之前学习的angular.js 只是理解了 ...

  7. easyui grid 本地做分页

    背景: 有的数据不是很多,但是有分页的需求,这个时候后台往往没有做分页,我们是一次请求了所有的数据. 代码: dataSource 为 grid 里的数据源 html部分: <table id= ...

  8. Oracle中创建Job实现自动插入值操作

    在PL/SQL Developer中创建Job --临时测试表 create table test_job ( id ), c_date date ); --临时序列 create sequence ...

  9. c++开发ocx入门实践四

    总结一下参数传递和方法调用的方法,ocx和容器进行参数传递,包括ocx和js,ocx和c++,ocx和c#.

  10. maven项目怎么引入另一个maven项目

    yi      最近在做项目的时候,遇到多模块(mudul)开发,里面的maven包相互引用,刚开始不知道怎么导入,费了好大尽总算搞定了.把遇到的问题记录下. 1.怎么导入依赖的maven模块 选择I ...