我正在开发一个 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" 文件和其他属性文件,但无法将其加载回来。旧的翻译处于活动状态。如果我重新启动应用程序,新的翻译就会处于活动状态,
任何帮助表示赞赏。
谢谢!!