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

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

Introduction

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

Prototype

public void setDefaultEncoding(@Nullable String defaultEncoding) 

Source Link

Document

Set the default charset to use for parsing properties files.

Usage

From source file:com.castlemock.war.config.MvcConfig.java

/**
 * Creates a Reloadable resource bundle message source. The resource is responsible for keeping track of the basename and
 * which encoding will be used on the application.
 * @return Returns a new Reloadable resource bundle message source
 *//*ww w  .  j a va 2s .c o m*/
@Bean
public MessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:language/messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

From source file:com.tkmtwo.rest.config.RestConfig.java

@Bean
public MessageSource httpErrorMessageSource() {
    ReloadableResourceBundleMessageSource m = new ReloadableResourceBundleMessageSource();
    //m.setBasename("classpath:/aisp/exhandler/messages");
    m.setBasename(env.getProperty("messagesource.basename"));
    m.setDefaultEncoding("UTF-8");
    return m;//from w  ww.j  a va  2  s.c o m
}

From source file:net.sf.yal10n.analyzer.ExploratoryEncodingTest.java

/**
 * Test to load a resource bundle via Spring's message source when the file
 * is encoded with a BOM and the correct encoding is used.
 * @throws Exception any error//w ww.  ja  va  2 s .  co  m
 */
@Test
public void testSpringMessageSourceBOM() throws Exception {
    ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
    source.setBasename("UTF8BOM");
    source.setDefaultEncoding("UTF-8-BOM");
    source.setFallbackToSystemLocale(false);

    Assert.assertEquals("first key", source.getMessage("testkey1", null, Locale.ROOT));
    Assert.assertEquals("second key", source.getMessage("testkey2", null, Locale.ROOT));
    Assert.assertEquals("This file is encoded as UTF-8 with BOM .",
            source.getMessage("description", null, Locale.ROOT));
}

From source file:net.sf.yal10n.analyzer.ExploratoryEncodingTest.java

/**
 * Test to load a resource bundle via Spring's message source when the file is
 * encoded with a BOM.//ww w  .  j a  v a2 s . c o  m
 * @throws Exception any error
 */
@Test
public void testSpringMessageSourceBOMDefault() throws Exception {
    ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
    source.setBasename("UTF8BOM");
    source.setDefaultEncoding("UTF-8");
    source.setFallbackToSystemLocale(false);

    // Assert.assertEquals("first key", source.getMessage("testkey1", null, Locale.ROOT));
    // note: utf-8 bom read as first character
    Assert.assertEquals("first key", source.getMessage("\ufefftestkey1", null, Locale.ROOT));
    Assert.assertEquals("second key", source.getMessage("testkey2", null, Locale.ROOT));
    Assert.assertEquals("This file is encoded as UTF-8 with BOM .",
            source.getMessage("description", null, Locale.ROOT));
}

From source file:nu.yona.server.CoreConfiguration.java

/**
 * This bean tells the application which message bundle to use.
 * //from w w  w . j a  v  a2  s. com
 * @return The message bundle source
 */
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageBundle = new ReloadableResourceBundleMessageSource();

    messageBundle.setFallbackToSystemLocale(false);
    messageBundle.setBasename("classpath:messages/messages");
    messageBundle.setDefaultEncoding("UTF-8");

    return messageBundle;
}

From source file:net.sf.yal10n.analyzer.ExploratoryEncodingTest.java

/**
 * Test to load a resource bundle via Spring's message source when the file
 * is encoded with a BOM and starts with a comment. 
 * @throws Exception any error//  w w  w  . j a v a 2s .  c  om
 */
@Test
public void testSpringMessageSourceBOMandComment() throws Exception {
    ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
    source.setBasename("UTF8BOMwithComment");
    source.setDefaultEncoding("UTF-8");
    source.setFallbackToSystemLocale(false);

    Assert.assertEquals("", source.getMessage("\ufeff#abc", null, Locale.ROOT));
    Assert.assertEquals("first key", source.getMessage("testkey1", null, Locale.ROOT));
    Assert.assertEquals("second key", source.getMessage("testkey2", null, Locale.ROOT));
    Assert.assertEquals("This file is encoded as UTF-8 with BOM .",
            source.getMessage("description", null, Locale.ROOT));
}

From source file:net.sf.yal10n.analyzer.ExploratoryEncodingTest.java

/**
 * Test to load a resource bundle via Spring's message source when the file is
 * encoded with a BOM and starts with a blank line.
 * @throws Exception any error//from  ww  w .ja va 2s  .com
 */
@Test
public void testSpringMessageSourceBOMandBlankLine() throws Exception {
    ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
    source.setBasename("UTF8BOMwithBlankLine");
    source.setDefaultEncoding("UTF-8");
    source.setFallbackToSystemLocale(false);

    Assert.assertEquals("first key", source.getMessage("testkey1", null, Locale.ROOT));
    Assert.assertEquals("second key", source.getMessage("testkey2", null, Locale.ROOT));
    Assert.assertEquals("This file is encoded as UTF-8 with BOM .",
            source.getMessage("description", null, Locale.ROOT));
}

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.orange.mmp.i18n.helpers.DefaultInternationalizationManager.java

/**
 * Adds a MessageSource from a module/*from   w w w .jav a  2s.  c  o 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:com.redhat.rhtracking.config.WebConfig.java

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:messages/messages", "classpath:messages/validation");
    // if true, the key of the message will be displayed if the key is not
    // found, instead of throwing a NoSuchMessageException
    messageSource.setUseCodeAsDefaultMessage(true);
    messageSource.setDefaultEncoding("UTF-8");
    // # -1 : never reload, 0 always reload
    messageSource.setCacheSeconds(0);//from  ww w. ja v a  2 s.  co m
    return messageSource;
}