springboot+thymeleaf+i18n
来源:互联网 发布:淘宝现金券 编辑:程序博客网 时间:2024/06/03 17:02
本人小码农一名,这是第一次写博客,写的不好请大家见谅!
由于工作项目需要中英文切换,所以本人查看了大量其他博主写的关于spring国际化的文章,加上自己的理解与尝试,终于实现了中英文切换的功能。项目是用springboot+
mybatis+springmvc+maven+thymeleaf搭建的,具体实现代码如下:
1.创建不同语言的字典(个人理解)
首先要在/java/main/resources/目录下创建一个目录i18n,然后创建3个properties,具体命名如下:
这里命名有要求,messages是前缀,是spring默认的前缀,如果要改成其他的需要指定。后面会讲到。先来说说这三个properties,messages.properties是默认语言,messages_en_US是英文,messages_zh_CN是中文,这三个properties里面主要是放需要中英文切换的内容。举个例子:
messages.properties:
index_info.addHospital = 添加医院
messages_en_US.properties:
index_info.addHospital = addHospital
messages_zn_CN.properties:
index_info.addHospital = 添加医院
三个properties中的key一般要一致,你可以在properties中加你需要翻译的内容。当然如果你需要更多的语言你可以创建更多的properties。
2.写配置文件
@Configurationpublic class I18nConfig { private static Logger logger = LoggerFactory.getLogger(I18nConfig.class); @Bean(name="localeResolver") public LocaleResolver localeResolverBean() { logger.info("#####sessionLocaleResolver---create"); SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); sessionLocaleResolver.setDefaultLocale(Locale.CHINA); logger.info("#####sessionLocaleResolver:"+sessionLocaleResolver); return sessionLocaleResolver; } @Bean(name="messageSource") public ResourceBundleMessageSource resourceBundleMessageSource(){ ResourceBundleMessageSource source=new ResourceBundleMessageSource(); source.setBasename("i18n/messages"); source.setDefaultEncoding("UTF-8"); source.setUseCodeAsDefaultMessage(true); return source; }}
spring采用的默认区域解析器是AcceptHeaderLocaleResolver,根据request header中的accept-language值来解析locale,并且是不可变的。
LocaleResolver 是一个处理语言的接口,SessionLocaleResolver和CookieLocaleResolver继承了AcceptHeaderLocaleResolver抽象类并实现了LocaleResolver接口,这里我用的SessionLocaleResolver来处理解析locale。由命名可知SessionLocaleResolver是把locale放入session中。
这里的ResourceBundleMessageSource 就是设置语言字典的参数,其中的setBasename(”i18n/messages”),resolver就会从i18n这个目录下找messages前缀的properties去读取其中的内容。所以这个设置与上面properties的所在目录紧密相关。其它的参数大家可以百度。当然我们用的是springboot框架,springboot也提供了相应的参数配置spring.messages.basename=i18n/message。
3.配置拦截器
@Configurationpublic class MyIntercepter extends WebMvcConfigurerAdapter { @Bean public LocaleChangeInterceptor localeChangeInterceptor() { LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); lci.setParamName("lang"); return lci; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); }}
这里配置了一个拦截器LocaleChangeInterceptor,拦截带有参数lang的url请求。
比如:http://localhost:8080/index?lang=en_US 它会拦截下来然后用resolver去解析。
4.前端页面显示
**index.html**<ul class="notification-menu"> <li> <a href="javascript:void(0)" class="btn btn-default dropdown-toggle" data-toggle="dropdown" onclick="swithChinese()"> <i class="fa fa-exchange"></i> <span style="margin-left: 5px;" class="language-state">中文</span> </a> </li> <li> <a href="javascript:void(0)" class="btn btn-default dropdown-toggle" data-toggle="dropdown" onclick="swithEnglish()"> <i class="fa fa-exchange"></i> <span style="margin-left: 5px;" class="language-state">英文</span> </a> </li> </ul> <div class="btn-group pull-right"> <button id="" class="btn btn-success" onclick="addHospital()"><span th:text="#{index_info.addHospital}"></span> <i class="fa fa-plus"></i> </button> </div>**js代码**<script type="text/javascript">function swithChinese(){ window.location.href = '?lang=zh_CN';}function swithEnglish() { window.location.href = '?lang=en_US';}</script>**controller**@GetMapping("/index")public String index() { return "index";}
前端用th:text=”#{index_info.addHospital}”就可以将properties中对应的内容显示出来啦。点击相应的按钮就会切换中英文了。
- springboot+thymeleaf+i18n
- SpringBoot -- thymeleaf
- springboot-thymeleaf
- SpringBoot-thymeleaf
- SpringBoot + thymeleaf
- springboot thymeleaf
- Springboot国际化i18n
- SpringBoot Thymeleaf 模板引擎
- SpringBoot-thymeleaf模板集成
- SpringBoot项目实战--thymeleaf
- springboot+jooq+thymeleaf
- springboot整合thymeleaf模板
- SpringBoot-mybatis- thymeleaf
- springboot-02thymeleaf注意
- SpringBoot整合thymeleaf模板
- SpringBoot集成Thymeleaf模板
- Springboot集成thymeleaf
- Springboot集成thymeleaf
- C语言指针变量的简单使用
- 多态
- lintcode---最大子数组差
- pymysql cursor传参报错TypeError: %d format: a number is required, not str
- Spring全家桶
- springboot+thymeleaf+i18n
- Pandas秘籍【第六章】
- nodejs koa web搭建
- 封装继承多态
- C语言·数组知识小结
- java JDBC 使用DBCP连接池
- python django框架的模型的创建
- C++primer薄片系列之特殊工具与技术
- Uva 11572 Unique snowflakes