1.配置部落格
國際化自動配置
basename:預設的掃描的國際化檔名為messages,即在resources建立messages_xx.properties檔案,可以通過逗號指定多個,如果不指定包名預設從classpath下尋找。
encoding:預設的編碼為UTF-8。
cacheSeconds:載入國際化檔案的快取時間,單位為秒,預設為永久快取。
fallbackToSystemLocale:當找不到當前語言的資原始檔時,如果為true預設找當前系統的語言對應的資原始檔如messages_zh_CN.properties,如果為false即載入系統預設的如messages.properties檔案。
複製程式碼

如index.properties,index_zh_CN.properties,index.properties作為找不到定義語言的資原始檔時的預設配置檔案。
複製程式碼
3、新增語言解析器,並設定預設語言為US英文部落格
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.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class I18nConfig extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
複製程式碼
4.controller層中語言屬性
LocaleContextHolder.getLocale()
複製程式碼

