Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在运行时设置springs固定语言环境解析器(用于i18n)的默认语言环境_Java_Spring_Spring Mvc_Internationalization_Locale - Fatal编程技术网

Java 在运行时设置springs固定语言环境解析器(用于i18n)的默认语言环境

Java 在运行时设置springs固定语言环境解析器(用于i18n)的默认语言环境,java,spring,spring-mvc,internationalization,locale,Java,Spring,Spring Mvc,Internationalization,Locale,我目前正在使用Spring框架创建一个java web应用程序。我的应用程序有一个管理员帐户和几个普通用户帐户。我希望我的管理员帐户能够通过从管理站点选择语言来选择应用程序应使用的语言 它是这样工作的: 1.管理员进入一个管理页面,在那里他可以选择应用程序应该用哪种语言呈现。 2.当他选择“保存设置”按钮时,他会更新web应用程序类路径中的属性文件 我的问题是,当使用新语言更新此属性文件时,我无法在运行时更改区域设置。它仍然使用古老的语言。我使用的是fixedLocaleResolver,因为我

我目前正在使用Spring框架创建一个java web应用程序。我的应用程序有一个管理员帐户和几个普通用户帐户。我希望我的管理员帐户能够通过从管理站点选择语言来选择应用程序应使用的语言

它是这样工作的: 1.管理员进入一个管理页面,在那里他可以选择应用程序应该用哪种语言呈现。 2.当他选择“保存设置”按钮时,他会更新web应用程序类路径中的属性文件

我的问题是,当使用新语言更新此属性文件时,我无法在运行时更改区域设置。它仍然使用古老的语言。我使用的是fixedLocaleResolver,因为我读到cookie和会话对于每个用户都是不同的

这是我的密码

配置文件

//Other code omitted

@PropertySource("classpath:/configuration/system.properties")
public class DefaultWebConfigurationContext extends WebMvcConfigurationSupport {

    @Autowired
    Environment env;

    @Bean
    public LocaleResolver localeResolver() {
        FixedLocaleResolver localeResolver = new FixedLocaleResolver();
        localeResolver.setDefaultLocale(new Locale(env.getProperty("system.default.language")));
        return localeResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:/languages/lang");
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
    }
//Other code omitted
系统属性

system.default.language=en
我希望你们中的任何人都能指导我如何使这个功能正常工作,这样管理员就可以更改区域设置,而无需停止tomcat服务器,手动更改system.properties,然后再次启动tomcat服务器


我的解决方案(23-05-2013) 我被告知要实现我自己的LocaleResolver,我已经做到了。到目前为止,它似乎是可行的,所以我将在这里选择自定义区域设置解析器的解决方案

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.LocaleResolver;
/**
 *
 * @author Martin Rohwedder
 * @since 23-05-2013
 * @version 1.0
 */
public class PropertyLocaleResolver implements LocaleResolver {

    private Properties prop = new Properties();
    private Locale defaultLocale = Locale.getDefault();

    public void setDefaultLocale(Locale locale) {
        this.defaultLocale = locale;
    }

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        return this.defaultLocale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale)     {
        try {
            this.prop.load(new FileInputStream("classpath:/configuration/system.properties"));
            this.defaultLocale = new Locale(this.prop.getProperty("system.default.language"));
        }
        catch (IOException e) {
            this.defaultLocale = (defaultLocale != null ? defaultLocale : Locale.getDefault());
        }
    }

}

实现您自己的LocaleResolver,该LocaleResolver保存当前区域设置并具有管理更新。

实现您自己的LocaleResolver,该LocaleResolver保存当前区域设置并具有管理更新。

您有任何资源(教程、文档等)吗,这说明了如何做到这一点?想想我发现了如何创建一个适用于我的自定义localeResolver,我已经添加了我的自定义Resolver作为我的问题的一部分,供其他人查看您是否有任何资源(教程、文档等)来解释如何做到这一点?想想我发现了如何创建适用于我的自定义localeResolver,我已将我的自定义解析器添加为我的问题的一部分,供其他人查看