我的i18n分辨率在我的弹簧启动应用程序中有问题。我有这样的配置:Spring Boot i18n分辨率
LocaleConfiguration.java这里我将Locale.US设置为默认语言,将消息目录设置为“src/main/resources/messages”并使用locale param来更改语言。
package com.myproject.web.config;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
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.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class LocaleConfiguration extends WebMvcConfigurerAdapter {
/**
* Este bean se encargará de resolver que idioma (locale) esta siendo usado
* actualmente
*
* @return
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
/**
* Este bean se encargara de interceptar el locale que venga a través del
* parámetro locale de la url
*
* @return
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("locale");
return lci;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
/**
* Registramos los filtros
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
我的邮件文件:
/src/main/resources/
----/messages/
-------->messages_es_ES.properties (Spanish message file)
-------->messages.properties (Default messages file. US)
乳清我在我家进入,我的应用程序显示:
??home.welcome_en_US??
看起来无法解决消息文件(甚至不是默认文件!)。我应该错过一些东西,但我找不到。任何帮助?这里
解决方案
问题是,我使用,我必须定义在类路径的完整路径没有语言环境后缀的文件扩展名(我的使用,这一点)基名路径: >类路径 消息/消息 - - >基本名 然后添加所需的SUFF
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
这将在 /src目录/主/资源/搜索IX(“es_ES”,“EN_US” ..) 和文件扩展名“的.properties”
请阅读关于'baseName'的含义的文档。这并不意味着你的想法。 –
看起来像消息定义属性文件的名称,而不是路径:)感谢指向我正确的方向 – Genaut