简体   繁体   English

Spring Boot 国际化解析

[英]Spring Boot i18n resolution

I have problems with the resolution of i18n in my spring boot application .我的spring boot 应用程序中的i18n解析有问题。 I have this configuration:我有这个配置:

LocaleConfiguration.java Here I set Locale.US like default language, set the message directory to "src/main/resources/messages" and use locale param to change the language. LocaleConfiguration.java在这里,我将 Locale.US 设置为默认语言,将消息目录设置为“src/main/resources/messages”,并使用 locale 参数更改语言。

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());
    }

}

My messages files:我的消息文件:

/src/main/resources/
----/messages/
-------->messages_es_ES.properties (Spanish message file)
-------->messages.properties (Default messages file. US)

Whey I enter in my home, my app shows:乳清我进入我的家,我的应用程序显示:

??home.welcome_en_US??

Looks like can't resolve message files (not even default file.).看起来无法解析消息文件(甚至不是默认文件)。 I should missed something but I can't find?我应该错过什么但我找不到? Any help with this?有什么帮助吗?

Solution解决方案


Problem here was the Basename path that I use, I have to define the full path in the classpath without the locale suffix and the file extension (For my use, this):这里的问题是我使用的 Basename 路径,我必须在没有语言环境后缀和文件扩展名的类路径中定义完整路径(供我使用,这个):

@Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

This will search in /src/main/resources/ --> classpath messages/messages --> basename And then add the required suffix ("es_ES", "en_US"..) and the file extension ".properties"这将在 /src/main/resources/ 中搜索 --> classpath messages/messages --> basename 然后添加所需的后缀(“es_ES”、“en_US”..)和文件扩展名“.properties”

For any similar problem, This was the solution对于任何类似的问题,这就是解决方案

Problem here was the Basename path that I use, I have to define the full path in the classpath without the locale suffix and the file extension (For my use, this):这里的问题是我使用的 Basename 路径,我必须在没有语言环境后缀和文件扩展名的类路径中定义完整路径(供我使用,这个):

@Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

This will search in这将搜索

/src/main/resources/ --> classpath /src/main/resources/ --> 类路径

messages/messages --> basename消息/消息 --> basename

And then add the required suffix ("es_ES", "en_US"..)然后添加所需的后缀(“es_ES”、“en_US”..)

and the file extension ".properties"和文件扩展名“.properties”

Another way to have it working in a flexible manner is using the properties initialization file: application.properties with the following entry:另一种让它以灵活方式工作的方法是使用属性初始化文件:application.properties 和以下条目:

spring.messages.basename=messages/messages

Where the first messages is the name of the folder inside src\resources.其中第一条消息是 src\resources 中文件夹的名称。 Other example, for eventual clarification, is a directory called bundles inside src\resources, like this:其他示例,为了最终澄清,是 src\resources 中名为bundles的目录,如下所示:

spring.messages.basename=bundles/messages

Using that configuration you don't need the hard coded configuration below in your Springboot main class:使用该配置,您不需要在 Springboot 主类中使用下面的硬编码配置:

...
messageSource.setBasename("classpath:messages/messages");
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM