【问题标题】:Java, Spring internationalization: how to use values from .properties in simple String?Java,Spring 国际化:如何在简单字符串中使用 .properties 中的值?
【发布时间】:2017-05-07 12:36:54
【问题描述】:

我使用下一个代码

@Value("${app.user.root}")
    private String userRoot;

从我的 application.properties 文件中获取常量值。

在我的GetMapping method 中,我需要重定向到 error 页面并传递一个字符串作为参数。

@GetMapping("/user/activate")
    public String activate(String activation) {
        Users u = usersService.activate(activation);
        if (u != null) {
            usersService.autoLogin(u);
            return "redirect:/";
        }

        return "redirect:/error?message=Could not activate with this activation code, please contact support";

但我需要使用不同的语言有不同的字符串值。所以,我使用的是 Spring i18n,但是我怎样才能在运行时获得我需要的值呢?我需要这样的东西:

return "redirect:/error?message=${errorMessage}";

谢谢你,希望你能帮助我。

【问题讨论】:

    标签: java html spring spring-mvc


    【解决方案1】:

    首先您必须为多种语言创建多个属性文件

    messages_en.properties messages_fr.properties

    i18n的配置应该如下

    @Configuration
    public class LanguageConfig extends WebMvcConfigurerAdapter {
    
        @Bean
        public ReloadableResourceBundleMessageSource messageSource(){
            ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
            messageSource.setBasename("classpath:messages");
            messageSource.setDefaultEncoding("UTF-8");
            return messageSource;
        }
        @Bean
        public LocaleResolver localeResolver() {
            SmartLocaleResolver slr = new SmartLocaleResolver();
        Locale locale = new Locale("en", "us");
        slr.setDefaultLocale(locale); // Set default Locale as en_cos
            return slr;
        }
    
        @Bean
        public LocaleChangeInterceptor localeInterceptor() {
            LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
            interceptor.setParamName("lang");
            return interceptor;
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(localeInterceptor());
        }
    
        class SmartLocaleResolver extends CookieLocaleResolver {
    
            @Override
            public Locale resolveLocale(HttpServletRequest request) {
                String acceptLanguage = request.getHeader("Accept-Language");
                if (acceptLanguage == null || acceptLanguage.trim().isEmpty()) {
                    return super.determineDefaultLocale(request);
                }
                return request.getLocale();
            }
    
        }
    }
    

    现在更新您的控制器代码并自动连接org.springframework.context.MessageSource,然后使用它来获取本地化消息。

    @Autowired
    private MessageSource messageSource;
    

    然后您可以使用以下代码获取本地化消息。

    String errorMessage = messageSource.getMessage("project.errorMessage", new Object[]{"John Doe"},  LocaleContextHolder.getLocale());
    

    您也可以使用控制器方法参数中的 Locale 对象而不是 LocaleContextHolder.getLocale(),但它工作得很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 2014-12-29
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      相关资源
      最近更新 更多