Example usage for org.springframework.context.support ReloadableResourceBundleMessageSource setBasename

List of usage examples for org.springframework.context.support ReloadableResourceBundleMessageSource setBasename

Introduction

In this page you can find the example usage for org.springframework.context.support ReloadableResourceBundleMessageSource setBasename.

Prototype

public void setBasename(String basename) 

Source Link

Document

Set a single basename, following the basic ResourceBundle convention of not specifying file extension or language codes.

Usage

From source file:cz.jirutka.spring.exhandler.RestHandlerExceptionResolverBuilder.java

private MessageSource createDefaultMessageSource() {

    ReloadableResourceBundleMessageSource messages = new ReloadableResourceBundleMessageSource();
    messages.setBasename(DEFAULT_MESSAGES_BASENAME);
    messages.setDefaultEncoding("UTF-8");
    messages.setFallbackToSystemLocale(false);

    return messages;
}

From source file:com.acc.storefront.web.theme.StorefrontResourceBundleSource.java

protected AbstractMessageSource createMessageSource(final String basename) {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename(basename);
    messageSource.setCacheSeconds(getCacheSeconds());
    messageSource.setResourceLoader(getResourceLoader());
    messageSource.setFallbackToSystemLocale(fallbackToSystemLocale);
    messageSource.setDefaultEncoding(getDefaultEncoding());
    return messageSource;
}

From source file:net.chrissearle.flickrvote.service.TestMessageSourceChallengeMessageService.java

@Before
public void setup() throws Exception {
    ConstrettoConfiguration conf = new ConstrettoBuilder().addCurrentTag("development").createPropertiesStore()
            .addResource(new DefaultResourceLoader().getResource("classpath:flickrvote.properties")).done()
            .createPropertiesStore()//from   www .j  av a2 s.  co  m
            .addResource(new DefaultResourceLoader().getResource("file:/etc/flickrvote/flickrvote.properties"))
            .done().getConfiguration();

    ShortUrlService shortUrlService = new BitlyShortUrlService();
    ((BitlyShortUrlService) shortUrlService).configure(conf.evaluateToString("bitly.login"),
            conf.evaluateToString("bitly.key"));

    MessageSourceChallengeMessageService service = new MessageSourceChallengeMessageService(shortUrlService);

    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:messages");
    messageSource.setFallbackToSystemLocale(true);

    service.setMessageSource(messageSource);
    service.afterPropertiesSet();

    challengeMessageService = service;

    DateTime startDate = new DateTime(2009, DateTimeConstants.JANUARY, 1, 18, 0, 0, 0);

    DateTime voteDate = startDate.plusDays(5);

    DateTime endDate = startDate.plusDays(7).plusHours(3);

    TestChallengeSummary testChallenge = new TestChallengeSummary();
    testChallenge.setTag("TestTag");
    testChallenge.setTitle("Test Name");
    testChallenge.setStartDate(startDate.toDate());
    testChallenge.setEndDate(endDate.toDate());
    testChallenge.setVoteDate(voteDate.toDate());

    challenge = testChallenge;

    resultsUrl = challengeMessageService.getResultsUrl(challenge);
}

From source file:com.orange.mmp.i18n.helpers.DefaultInternationalizationManager.java

/**
 * Adds a MessageSource from a module/*w  w w.  j  a va  2 s . co m*/
 * 
 * @param module The module owning the MessageSource
 * @throws MMPException
 */
private void addModuleMessageSource(Module module) throws MMPException {
    try {
        MMPConfig moduleConfiguration = ModuleContainerFactory.getInstance().getModuleContainer()
                .getModuleConfiguration(module);
        if (moduleConfiguration != null && moduleConfiguration.getMessagesource() != null) {
            for (MMPConfig.Messagesource messageSourceConfig : moduleConfiguration.getMessagesource()) {
                if (messageSourceConfig.getOtherAttributes().get(I18N_CONFIG_MS_LOCATION) != null
                        && messageSourceConfig.getOtherAttributes().get(I18N_CONFIG_MS_BASENAME) != null) {
                    String messageSourceName = messageSourceConfig.getName();
                    String location = messageSourceConfig.getOtherAttributes().get(I18N_CONFIG_MS_LOCATION);
                    String basename = messageSourceConfig.getOtherAttributes().get(I18N_CONFIG_MS_BASENAME);
                    List<URL> locations = ModuleContainerFactory.getInstance().getModuleContainer()
                            .findModuleResource(module, location, basename + "*", false);

                    String messageSourceFolderBase = this.messageBasePath + "/" + messageSourceName;
                    FileUtils.forceMkdir(new File(messageSourceFolderBase));

                    //Copy I18N resources on shared repository (Only master server)
                    if (ApplicationController.getInstance().isMaster()) {
                        String fileName = null;
                        for (URL messageUrl : locations) {
                            InputStream in = null;
                            OutputStream out = null;
                            try {
                                in = messageUrl.openStream();
                                String filePath = messageUrl.getPath();
                                fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
                                out = new FileOutputStream(new File(messageSourceFolderBase, fileName));
                                IOUtils.copy(in, out);
                            } finally {
                                if (in != null) {
                                    in.close();
                                }
                                if (out != null) {
                                    out.close();
                                }
                            }

                        }
                    }

                    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
                    messageSource.setDefaultEncoding("UTF-8");
                    messageSource.setBasename("file://" + messageSourceFolderBase + "/" + basename);
                    this.messageSourceMap.put(messageSourceName, messageSource);

                    //Keep file path for the translation
                    i18nFilesPathByMessageSource.put(messageSourceName,
                            messageSourceFolderBase + "/" + basename);
                }
            }
        }
    } catch (IOException ioe) {
        throw new MMPI18NException("Failed to load " + module.getName() + " I18N configuration");
    }
}

From source file:net.jawr.web.resource.bundle.locale.message.GrailsMessageBundleScriptCreator.java

public Reader createScript(Charset charset) {

    // Determine wether this is run-app or run-war style of runtime. 
    boolean warDeployed = ((Boolean) this.servletContext.getAttribute(JawrConstant.GRAILS_WAR_DEPLOYED))
            .booleanValue();//from  ww  w.j a  va  2s  .co m

    // Spring message bundle object, the same used by grails. 
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setFallbackToSystemLocale(false);
    Set<String> allPropertyNames = null;

    // Read the properties files to find out the available message keys. It is done differently 
    // for run-app or run-war style of runtimes. 
    if (warDeployed) {
        allPropertyNames = getPropertyNamesFromWar();
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("found a total of " + allPropertyNames.size() + " distinct message keys.");
        messageSource.setResourceLoader(new ServletContextResourceLoader(this.servletContext));
        messageSource.setBasename(PROPERTIES_DIR + configParam.substring(configParam.lastIndexOf('.') + 1));
    } else {
        ResourceBundle bundle;
        if (null != locale)
            bundle = ResourceBundle.getBundle(configParam, locale);
        else
            bundle = ResourceBundle.getBundle(configParam);
        allPropertyNames = new HashSet<String>();
        Enumeration<String> keys = bundle.getKeys();
        while (keys.hasMoreElements())
            allPropertyNames.add(keys.nextElement());
        String basename = "file:./" + configParam.replaceAll("\\.", "/");
        messageSource.setBasename(basename);
    }

    // Pass all messages to a properties file. 
    Properties props = new Properties();
    for (Iterator<String> it = allPropertyNames.iterator(); it.hasNext();) {
        String key = it.next();
        if (matchesFilter(key)) {
            try {
                // Use the property encoding of the file
                String msg = new String(
                        messageSource.getMessage(key, new Object[0], locale).getBytes(CHARSET_ISO_8859_1),
                        charset.displayName());
                props.put(key, msg);
            } catch (NoSuchMessageException e) {
                // This is expected, so it's OK to have an empty catch block. 
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("Message key [" + key + "] not found.");
            } catch (UnsupportedEncodingException e) {
                LOGGER.warn("Unable to convert value of message bundle associated to key '" + key
                        + "' because the charset is unknown");
            }
        }
    }

    return doCreateScript(props);
}

From source file:org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.java

/**
 * The {@link MessageSourceAccessor} to provide messages for {@link ResourceDescription}s being rendered.
 * /*  w  ww  . j  av a  2 s  .  com*/
 * @return
 */
@Bean
public MessageSourceAccessor resourceDescriptionMessageSourceAccessor() {

    try {

        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("rest-default-messages.properties"));
        propertiesFactoryBean.afterPropertiesSet();

        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:rest-messages");
        messageSource.setCommonMessages(propertiesFactoryBean.getObject());
        messageSource.setDefaultEncoding("UTF-8");

        return new MessageSourceAccessor(messageSource);

    } catch (Exception o_O) {
        throw new BeanCreationException("resourceDescriptionMessageSourceAccessor", "", o_O);
    }
}