如果locale无效,则Spring mvc从请求中获取默认语言环境

时间:2016-03-25 20:37:18

标签: java spring spring-mvc internationalization locale

我已经在我的Spring MVC应用程序(localeInterceptor等)中配置了i18n。

支持两种语言(en& pl)。 PL是默认值。 I18n当然是有效的。

我的情况是获取当前的语言环境并将其传递给模型。这很容易,但是当我传递无效的语言环境参数(比如 localhost:8080 /?language = asd )时,我会得到" asd"而不是" pl"这是默认的。

值得一提的是整个i18n工作得很好,我的意思是在这种情况下使用messages_pl.properties。 我尝试了以下方法但没有成功:

@RequestMapping(...)
public String home(Locale locale,..)

localeResolver.resolveLocale(request);
RequestContextUtils.getLocale(request);
LocaleContextHolder.getLocale();

每一个都给了我一个" asd"而不是" pl"。

Atm我的解决方法有点混乱:

if (!supportedLocaleService.isLocaleSupported(locale)) {//myservice
    locale = new Locale("pl");
}

2 个答案:

答案 0 :(得分:1)

您可以覆盖LocaleChangeInterceptor

public class SupportedLocaleAwareLocaleChangeInterceptor extends LocaleChangeInterceptor {

    private List<String>supportedLocales = Arrays.asList("pl",....);

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

        String newLocale = request.getParameter(this.paramName);
        //if (newLocale != null) {
        if (newLocale != null && supportedLocales.contains(newLocale )) {
            LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
            if (localeResolver == null) {
                throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
            }
            localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
        }
        // Proceed in any case.
        return true;
    } 
}

答案 1 :(得分:0)

再来一步。

public class CustomCookieLocaleResolver extends CookieLocaleResolver {

        private final List<String> permittedLanguages = Arrays.asList("sk", "en");

        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            Locale locale = (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
            if (locale != null && !permittedLanguages.contains(locale.getLanguage())) {
                locale = determineDefaultLocale(request);
            }
            return locale;
        }

        @Override
        public LocaleContext resolveLocaleContext(HttpServletRequest request) {

            try {
                LocaleContext context = super.resolveLocaleContext(request);
                return context;

            } catch (Exception e) {

            }

            final Locale defaultLocale = determineDefaultLocale(request);
            TimeZone defaultTimeZone2 = determineDefaultTimeZone(request);

            return new TimeZoneAwareLocaleContext() {
                @Override
                @Nullable
                public Locale getLocale() {
                    return defaultLocale;
                }

                @Override
                @Nullable
                public TimeZone getTimeZone() {
                    return defaultTimeZone2;
                }
            };
        }

        @Override
        public void addCookie(HttpServletResponse response, String cookieValue) {
            if (permittedLanguages.contains(cookieValue)) {
                super.addCookie(response, cookieValue);
            }
        }
    }