spring-boot - 本地化 : Load translation file dynamically

标签 spring-boot localization internationalization locale translation

我正在开发一个 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 文件夹中,我保留了翻译 key 。

messages_en.properties

message_fr.properties

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

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

问题是什么?

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

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

感谢任何帮助。

谢谢!!

最佳答案

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

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
//      messageSource.setCacheMillis(1000);
        messageSource.setCacheSeconds(60);
        return messageSource;
    }

关于spring-boot - 本地化 : Load translation file dynamically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57208699/

相关文章:

spring - 将自定义 SecurityExpressionOperations 中的方法注册为 Spring SpEL 函数

javascript - 是否有任何 javascript 库实现了瑞典语等字母的排序方法?

Spring-Boot-Devtools 不想重新加载进程(没有选择正确的类路径?)

java - Whitelabel错误页面,Spring Boot中未执行剩余URL

java - 如何在 Spring Boot 应用程序启动时启动 H2 TCP 服务器?

Iphone 和从右到左的文本

xcode - 使用 Localized.String 在 Xcode 4 中进行本地化

.net - 使用自定义文化时,ResourceManager 未选择正确的资源集

.net - 从 .resx 文件组获取所有可用区域性

internationalization - xmodmap 和多种布局