我如何在多种语言支持下包括百里香



我想显示带有不同语言的页面。我知道将消息属性用于不同语言,但这并不能解决我的问题。因为我的HTML页面具有很多动态变量。我想在语言选项中包括页面。我如何在胸腺中轻松解决这个问题?

为了支持多种语言,我个人将使用春季框架的国际化:http://www.baeldung.com/spring-boot-internationsization-轻松使用Thymeleaf模板

所以这里有一个解决方案:

@Controller

公共类mvcwebconfig实现webmvcconfigurer {

/*THis locates all message files for html pages */
@Bean("messageSource")
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource =
            new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:messages");
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setUseCodeAsDefaultMessage(true);
    return messageSource;
}

/*Local Resolver is to set language preference */
@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver LR = new SessionLocaleResolver();
    LR.setDefaultLocale(Locale.UK);
    return LR;
}

/*overides language default with preference */
@Override
public void addInterceptors(InterceptorRegistry registry) {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("lang");
    registry.addInterceptor(localeChangeInterceptor);
}

}

然后在胸腺中实现此目标

Language : <a href="?lang=uk">English</a> | <a href="?lang=gd">Gaelic</a>

最新更新