JavaWeb - Thymeleaf - i18n

<div class="contact-title pb-3" data-aos="fade-up" data-aos-delay="100">
    <h2 class="title">[[#{contact.title}]]</h2>
</div>

案例

在Thymeleaf中,国际化(i18n)是通过属性文件(通常是.properties文件)来实现的。以下是确定引用哪个属性文件数据的步骤:

  1. 资源文件命名约定

    • 国际化资源文件通常遵循一定的命名约定,例如 messages.properties 作为默认文件,其他语言版本的文件则按照 messages_语言代码_国家代码.properties 的格式命名。例如,针对中文简体,文件名可能是 messages_zh_CN.properties
  2. 配置文件

    • 在Spring Boot应用中,通常在 application.properties 或 application.yml 配置文件中设置国际化资源的基础名称。例如:
      spring.messages.basename=messages
      
      这一行配置指定了基础资源文件名为 messages,所以Thymeleaf会查找 messages.properties 作为默认语言资源文件。
  3. 语言解析器

    • 应用程序通常会配置一个 LocaleResolver,用于确定当前用户的语言环境。这通常基于HTTP请求中的某些参数,如 Accept-Language 头部或特定的请求参数。
  4. 使用属性

    • 当你在Thymeleaf模板中使用 [[#{contact.title}]] 时,Thymeleaf会根据当前的 Locale(由 LocaleResolver 提供)查找相应的属性文件。如果当前的语言环境是中文简体,Thymeleaf将尝试从 messages_zh_CN.properties 文件中获取 contact.title 的值。

以下是详细的步骤:

  • 步骤1:确定当前用户的语言环境。

    • 这通常是通过用户的浏览器设置或者用户在应用程序中的偏好设置来确定的。
  • 步骤2:根据语言环境查找相应的属性文件。

    • 例如,如果当前语言环境是 zh_CN,则Thymeleaf会查找 messages_zh_CN.properties
  • 步骤3:在找到的属性文件中查找键 contact.title 对应的值。

    • 如果 contact.title 在 messages_zh_CN.properties 文件中定义了,那么对应的值将被用来替换 [[#{contact.title}]] 表达式。
  • 步骤4:如果没有找到对应语言的属性文件或键,则回退到默认属性文件。

    • 如果 messages_zh_CN.properties 文件不存在或其中没有 contact.title 键,Thymeleaf会回退到默认的 messages.properties 文件。

通过以上步骤,Thymeleaf能够根据当前用户的语言环境动态地引用正确的属性文件中的数据,从而实现国际化。

国际化配置:

一、在application.yml配置文件中添加配置信息


  spring:    
    messages:
        basename: static/i18n/message
        encoding: UTF-8

二、在src/main/resources/static目录中创建i18n文件夹、并创建 message_en_US.properties,message_zh_CN.properties,message.properties等几个文件

其中
message.properties 表示默认加载信息
message_zh_CN.properties 表示中文信息
message_en_US.properties 表示英文信息

三、添加控制类:

import java.util.Locale;
 
import org.springframework.context.annotation.Bean;
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.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
 
/**
 * 国际化配置
 * 
 * @author YqZhilan
 *
 */
@Configuration
public class I18nConfig extends WebMvcConfigurerAdapter {
    @Bean
    public LocaleResolver localeResolver(){
    	
    	CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    	localeResolver.setCookieName("localeCookie"); // 将语言信息添加到Cookie中
        //设置默认区域
        localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE); // 默认简化汉语
        localeResolver.setCookieMaxAge(86400);//设置cookie有效期.24小时
        return localeResolver;
    }
    
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名
        lci.setParamName("l");
        return lci;
    }
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值