2

我正在开发一个 Spring Boot 应用程序来本地化数据。我能够使用翻译文件进行本地化。

import java.util.Locale;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
public class LocaleConfiguration implements WebMvcConfigurer {

    /**
     * * @return default Locale set by the user
     */
    @Bean(name = "localeResolver")
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

    /**
     * an interceptor bean that will switch to a new locale based on the value of
     * the language parameter appended to a request:
     *
     * @param registry
     * @language should be the name of the request param
     *           <p>
     *           Note: All requests to the backend needing Internationalization
     *           should have the "lang" request param
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        registry.addInterceptor(localeChangeInterceptor);
    }


}

在我的 src/main/resource 文件夹中,我保留了我的翻译密钥。

messages_en.properties

message_fr.properties

现在使用消息源,我可以翻译数据

String translatedMessage = messageSource.getMessage(key, null, "default_message",
                    LocaleContextHolder.getLocale());

问题是什么?

我正在使用 PhraseApp 服务,每次收到翻译任何数据的请求时,我都必须同步翻译,即在运行时下载翻译文件并将其加载到 Spring Boot 应用程序中。

我可以在运行时更新 *messages_en.properties" 文件和其他属性文件,但无法将其加载回来。旧的翻译处于活动状态。如果我重新启动应用程序,新的翻译就会处于活动状态,

任何帮助表示赞赏。

谢谢!!

4

2 回答 2

1

将以下 bean 添加到 LocaleConfiguration 应在 60 秒后刷新翻译(可以通过传递所需的值来更改。如果需要,您还可以有一个以毫秒为单位的刷新间隔 - 请参阅注释行):

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
//      messageSource.setCacheMillis(1000);
        messageSource.setCacheSeconds(60);
        return messageSource;
    }
于 2019-08-01T10:43:05.453 回答
1

首先将您的 i18n 资源放在特定文件夹中,例如在i18n项目根目录中(但不在文件夹中src/main/resources)。

然后将以下 bean 添加到您的应用程序中:

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource commands = new ReloadableResourceBundleMessageSource();
        commands.setBasename("file:i18n/messages");
        commands.setCacheSeconds(60);
        return commands;
    }

Spring 每分钟都会清除MessageSource缓存,您的应用程序将能够从i18n文件夹中重新读取更新的数据。

于 2019-08-04T14:31:12.900 回答