Spring boot+beetl+i18n国际化处理的方法
作者:Start_liyi 时间:2023-07-22 00:36:31
国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式。它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素。换言之,应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产。开发这样的程序的过程,就称为国际化。
Spring boot 搭配慢慢开始火起来的 beetl 模板 配置国际化
首先需要添加WebMvcConfigurer配置
/**
* 设置 *
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
/**
* 国际化切换 *
*
* @return 国际化切换 *
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("lang");
return interceptor;
}
/**
* 国际化处理器
*
* @return 国际化处理器
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
//设置默认区域,
slr.setDefaultLocale(Locale.CHINA);
return slr;
}
然后自定义配置beetl
...
@Autowired
private WebApplicationContext wac;
@Bean
public BeetlTemplateCustomize beetlTemplateCustomize() {
return new BeetlTemplateCustomize() {
public void customize(GroupTemplate groupTemplate) {
// 注册全局共享变量
Map<String, Object> sharedVars = new HashMap<String, Object>();
groupTemplate.setSharedVars(sharedVars);
// 注册国家化函数
groupTemplate.registerFunction("i18n", new I18nFunction(wac));
}
};
}
然后配置i18n国际化函数
public class I18nFunction implements Function {
private WebApplicationContext wac;
public I18nFunction(WebApplicationContext wac) {
this.wac = wac;
}
@Override
public Object call(Object[] obj, Context context) {
HttpServletRequest request = (HttpServletRequest) context.getGlobal(WebVariable.REQUEST);
RequestContext requestContext = new RequestContext(request);
String message = requestContext.getMessage((String) obj[0]);
return message;
}
}
最后配置资源文件
这个资源文件路径也是配出来的,不多介绍了......
测试:
在模板中添加${i18n('messageCode')} , 在url参数中添加lang=en 或者 lang=zh-CN
来源:https://segmentfault.com/a/1190000018841007
标签:Spring,boot,beetl,i18n,国际化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java两个integer数据判断相等用==还是equals
2021-06-14 00:46:52
Spring集成Druid连接池及监控配置的全过程
2023-08-25 05:22:20
![](https://img.aspxhome.com/file/2023/8/63448_0s.png)
C#中static void Main(string[] args) 参数示例详解
2023-10-06 04:44:01
![](https://img.aspxhome.com/file/2023/3/102233_0s.png)
详解OAuth2 Token 一定要放在请求头中吗
2022-05-01 09:43:15
![](https://img.aspxhome.com/file/2023/3/76543_0s.jpg)
解决java main函数中的args数组传值问题
2021-06-24 12:46:13
![](https://img.aspxhome.com/file/2023/9/68579_0s.jpg)
android 引导界面的实现方法
2023-08-07 04:58:11
Android自定义控件实现圆形进度条
2023-02-02 05:01:59
![](https://img.aspxhome.com/file/2023/1/129151_0s.jpg)
spring事务之事务挂起和事务恢复源码解读
2023-11-26 18:50:42
![](https://img.aspxhome.com/file/2023/7/70357_0s.png)
Java从服务端下载Excel模板文件的两种方法
2021-08-29 05:44:40
![](https://img.aspxhome.com/file/2023/5/67115_0s.jpg)
C#中的委托和事件详解
2022-01-20 01:31:53
![](https://img.aspxhome.com/file/2023/2/113702_0s.png)
C#利用缓存分块读写大文件
2022-10-20 11:18:22
Mybatis 开发注解快速入门
2021-12-01 16:03:36
![](https://img.aspxhome.com/file/2023/8/128838_0s.png)
从Cocos2d-x2迁移到Cocos2d-x3的过程分享
2021-06-07 23:52:28
java ArrayList集合中的某个对象属性进行排序的实现代码
2022-05-27 19:54:24
![](https://img.aspxhome.com/file/2023/0/62530_0s.jpg)
解决Android Studio电脑不支持HAXM的问题
2022-05-28 04:48:51
![](https://img.aspxhome.com/file/2023/4/108314_0s.jpg)
浅谈Spring Boot 整合ActiveMQ的过程
2022-03-22 05:20:27
Java使用MulticastSocket实现群聊应用程序
2021-09-23 12:12:29
![](https://img.aspxhome.com/file/2023/9/75669_0s.jpg)
获取控件大小和设置调整控件的位置XY示例
2022-06-06 04:40:25
Unity的IPostBuildPlayerScriptDLLs实用案例深入解析
2022-12-23 00:12:38
MybatisPlus分页排序查询字段带有下划线的坑及解决
2022-08-16 22:26:28