1

I am currently creating a java web application using Spring Framework. My application haves one administrator account and several normal user accounts. I want my administrator account to be the one who can choose which language the application should be resolved in, by selecting the language from an adminsitration site.

It works this way: 1. Administrator goes to a admin page, where he can select which language the application should be rendered in. 2. When he selects the save settings button he updates a properties file which I am having in my web applications classpath.

My problem is that I can't change the locale during runtime, when this properties file is updated with the new language. It still uses the old language. I am using a fixedLocaleResolver, since I have read that both cookie and session is different for each user.

Here is my code

Configuration File

//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.properties

system.default.language=en

I hope any of you can guide me in the right direction of how I can make this functionality work, so an admin can change the locale without having to stop the tomcat server, change the system.properties by hand, and the start the tomcat server again.


My Solution (23-05-2013) I was told to implement my own LocaleResolver, which I have done. So far it seems to work, so therefore I will choose my solution of the custom Locale Resolver here

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());
        }
    }

}
4

1 回答 1

1

实现你自己的 LocaleResolver 来保存当前的语言环境并让管理员更新它。

于 2013-05-22T19:07:38.017 回答