语言更改在Heroku中不起作用

时间:2019-07-11 19:03:37

标签: spring-boot heroku internationalization thymeleaf

我有一个在SpringBoot 2.2.0.M4中开发的,具有i18n支持的应用程序。这在本地没有问题,但是当在Heroku中部署它不起作用时,有人发生了类似的事情吗?

在一起的图像

@Configuration
public class GoSportConfig implements WebMvcConfigurer
{
    @Value("${app.language.param-name}")
    private String paramName;

    @Bean
    public LocaleResolver localeResolver(Message message)
    {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(message.getDefaultLocale());
        return slr;
    }

    @Bean
    public GoSportLocaleChangeInterceptor localeChangeInterceptor()
    {
        GoSportLocaleChangeInterceptor interceptor = new GoSportLocaleChangeInterceptor();
        interceptor.setParamName(paramName);
        return interceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(localeChangeInterceptor());
    }
}
public class GoSportLocaleChangeInterceptor extends LocaleChangeInterceptor
{
    @Autowired
    private Message message;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws ServletException
    {

        super.preHandle(request, response, handler);

        String newLocale = request.getParameter(getParamName());

        if (newLocale != null && checkHttpMethod(request.getMethod()))
        {
            message.setLocale(parseLocaleValue(newLocale));
        }

        return true;
    }

    private boolean checkHttpMethod(String currentMethod)
    {
        String[] configuredMethods = getHttpMethods();

        if (ObjectUtils.isEmpty(configuredMethods))
        {
            return true;
        }

        for (String configuredMethod : configuredMethods)
        {
            if (configuredMethod.equalsIgnoreCase(currentMethod))
            {
                return true;
            }
        }

        return false;
    }
}
@Component
public class Message
{

    @Value("${app.language.default}")
    private String language;

    private Locale defaultLocale = null;

    @Autowired
    private MessageSource messageSource;

    private MessageSourceAccessor accessor;

    @PostConstruct
    private void init()
    {
        defaultLocale = new Locale(language);
        setLocale(defaultLocale);
    }

    public String get(String code)
    {
        return accessor.getMessage(code);
    }

    public void setLocale(Locale localeCode)
    {
        accessor = new MessageSourceAccessor(messageSource, localeCode);
    }

    public Locale getDefaultLocale()
    {
        return defaultLocale;
    }
}
# App configuration
app:
  language:
    supports: es,en
    default: es
    param-name: lang

0 个答案:

没有答案