国际化在Spring Boot中的问题在配置文件中无法正常工作。

huangapple go评论125阅读模式
英文:

The problem with Internationalization in Spring Boot do not work in Configuration file

问题

以下是翻译好的部分:

我目前正在使用Spring Boot包括Spring Security开发Web应用程序目前我正在尝试集成国际化支持首先是默认语言俄语
这是一个配置文件我在其中编写代码以获取俄语语言的消息如果用户输入了错误的数据等但是这段代码却返回英语

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.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Configuration
public class ConfigForAuth {
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(new Locale("ru"));
        return slr;
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource rs = new ReloadableResourceBundleMessageSource();
        rs.addBasenames("classpath:i18n/messages");
        rs.addBasenames("classpath:/org/springframework/security/messages_ru.properties:1");
        rs.setDefaultEncoding("UTF-8");
        rs.setUseCodeAsDefaultMessage(true);
        return rs;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("locale");

        return localeChangeInterceptor;
    }
}

但是,如果我将相同的代码写在应用程序文件中,这段代码可以正常工作,并以俄语返回给我。
应用程序文件:

@SpringBootApplication
@ComponentScan("--.--.--") //conf
@EnableCaching
@PropertySources({
        @PropertySource("classpath:application.properties"),
        @PropertySource("classpath:clients.properties")
})
public class OneBpmAuthApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(OneBpmAuthApiApplication.class, args);
    }
}
英文:

I'm currently working on a Web-App using Spring boot (including spring security). At the moment i'm trying to integrate internationalization support for the defult language russian as a start.
This is a configuration file where I write to get messages in the Russian language, if the user writed wrong data or e.t but this code returns me In English

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.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Configuration

public class ConfigForAuth {
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(new Locale("ru"));
        return slr;
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource rs = new ReloadableResourceBundleMessageSource();
        rs.addBasenames("classpath:i18n/messages");
        rs.addBasenames("classpath:/org/springframework/security/messages_ru.properties:1");
        rs.setDefaultEncoding("UTF-8");
        rs.setUseCodeAsDefaultMessage(true);
        return rs;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("locale");

        return localeChangeInterceptor;
    }
}

But if I will write this same code in Application file this code works fine and returns to me in RU
Application file:

@SpringBootApplication
@ComponentScan("--.--.--")//conf
@EnableCaching
@PropertySources({
        @PropertySource("classpath:application.properties"),
        @PropertySource("classpath:clients.properties")
})
public class OneBpmAuthApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(OneBpmAuthApiApplication.class, args);
    }

答案1

得分: 0

尝试从 WebMvcConfigurerAdapter 进行扩展,并添加拦截器。


@Configuration
public class ConfigForAuth extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	registry.addInterceptor(localeChangeInterceptor());
    }

}
英文:

Try extending from WebMvcConfigurerAdapter and adding the interceptors.


@Configuration
public class ConfigForAuth extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	registry.addInterceptor(localeChangeInterceptor());
    }

}

答案2

得分: 0

我认为之前的建议基本上是正确的,只是你需要将 ParamName 设置为 "lang",像这样:

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("lang");
    return localeChangeInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}

同时,请记得扩展 WebMvcConfigurer。

英文:

I think previous advice was more or less right just you need to set ParamName to "lang" like so:

   @Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor localeChangeInterceptor = new
            LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("lang");
    return localeChangeInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}

also please do remember to extend WebMvcConfigurer.

huangapple
  • 本文由 发表于 2020年8月18日 18:47:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63467012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定