SpringBoot-thymeleaf 基于数据库国际化

简介: SpringBoot-thymeleaf 基于数据库国际化

java配置文件

  • 配置拦截器及语言环境解析
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Bean
    public LocaleResolver localeResolver() {
        //设置cookie模式处理国际化
        CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
        cookieLocaleResolver.setDefaultLocale(Locale.CHINESE);
        return cookieLocaleResolver;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //指定国际化标识
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        registry.addInterceptor(localeChangeInterceptor);
    }
}
  • 配置国际化数据源
// 1.需指定名称替换默认bean
@Component("messageSource")
public class DBMessageSource extends AbstractMessageSource {
    // 自定义service
    @Autowired
    private LanguageRepository languageRepository;
    // 2.国际化处理方法依据key在数据库中查找对应国际化内容
    @Override
    protected MessageFormat resolveCode(String key, Locale locale) {
        LanguageEntity message = languageRepository.findByKeyAndLocale(key,locale.getLanguage());
        if (message == null) {
            message = languageRepository.findByKeyAndLocale(key,Locale.getDefault().getLanguage());
        }
        return new MessageFormat(message.getContent(), locale);
    }
    //3.新增方法,用于后端传参国际化
    public final String getMessage(String code, @Nullable Object[] args) throws NoSuchMessageException {
        Locale locale = LocaleContextHolder.getLocale();
        String msg = getMessageInternal(code, args, locale);
        if (msg != null) {
            return msg;
        }
        String fallback = getDefaultMessage(code);
        if (fallback != null) {
            return fallback;
        }
        throw new NoSuchMessageException(code, locale);
    }
}

其中3为自己新增的方法用于java代码中获取国际化,使用时在java代码中注入messageSource。

页面

<h2 th:text="#{home.welcome('xxx')}"></h2>
<p th:text="#{home.info}"></p>
<p th:text="#{home.changelanguage}"></p>
<ul>
    <li><a href="?lang=en" th:text="#{home.lang.en}"></a></li>
    <li><a href="?lang=de" th:text="#{home.lang.de}"></a></li>
    <li><a href="?lang=zh" th:text="#{home.lang.zh}"></a></li>
</ul>

参数传递为#{key(参数……)}

问题:

1.Thymeleaf中[[]]为转义符导致在js代码中使用国际化并不方便

完整代码

https://gitee.com/MeiJM/springboot-i18n

参考资料

http://zhangjiaheng.cn/blog/20190320/%E4%BD%BF%E7%94%A8springboot%E8%BF%9B%E8%A1%8C%E5%9B%BD%E9%99%85%E5%8C%96%E6%97%B6%E8%87%AA%E5%AE%9A%E4%B9%89%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE/

https://github.com/PhraseApp-Blog/spring-boot-db-messageresource

https://medium.com/techcret/database-aware-i18n-messages-springboot-5715063094ef

目录
打赏
0
0
0
0
891
分享
相关文章
SpringBoot入门 - 添加内存数据库H2
SpringBoot入门 - 添加内存数据库H2
158 3
SpringBoot入门 - 添加内存数据库H2
基于SpringBoot+Vue实现的大学生就业服务平台设计与实现(系统源码+文档+数据库+部署等)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
94 6
基于SpringBoot+Vue的班级综合测评管理系统设计与实现(系统源码+文档+数据库+部署等)
✌免费选题、功能需求设计、任务书、开题报告、中期检查、程序功能实现、论文辅导、论文降重、答辩PPT辅导、会议视频一对一讲解代码等✌
57 4
基于SpringBoot+Vue实现的大学生体质测试管理系统设计与实现(系统源码+文档+数据库+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
53 2
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
83 2
基于SpringBoot+Vue实现的冬奥会科普平台设计与实现(系统源码+文档+数据库+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
59 0
使用 Spring Boot 执行数据库操作:全面指南
使用 Spring Boot 执行数据库操作:全面指南
375 1
SpringBoot入门(4) - 添加内存数据库H2
SpringBoot入门(4) - 添加内存数据库H2
85 13
SpringBoot入门(4) - 添加内存数据库H2
SpringBoot入门(4) - 添加内存数据库H2
83 4
|
4月前
|
springboot当中ConfigurationProperties注解作用跟数据库存入有啥区别
`@ConfigurationProperties`注解和数据库存储配置信息各有优劣,适用于不同的应用场景。`@ConfigurationProperties`提供了类型安全和模块化的配置管理方式,适合静态和简单配置。而数据库存储配置信息提供了动态更新和集中管理的能力,适合需要频繁变化和集中管理的配置需求。在实际项目中,可以根据具体需求选择合适的配置管理方式,或者结合使用这两种方式,实现灵活高效的配置管理。
49 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等