List of usage examples for org.springframework.context.support ReloadableResourceBundleMessageSource getMessage
@Override public final String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException
From source file:it.f2informatica.webapp.test.context.WebApplicationContextTest.java
@Test public void messageSource() { ReloadableResourceBundleMessageSource messageSource = webApplicationConfig.messageSource(); String englishMessage = messageSource.getMessage("message.test", new Object[] {}, Locale.ENGLISH); assertThat(englishMessage).isEqualTo("Message in English"); String italianMessage = messageSource.getMessage("message.test", new Object[] {}, Locale.ITALIAN); assertThat(italianMessage).isEqualTo("Messaggio in Italiano"); }
From source file:com.utils.BundleMessageReader.java
public String getMessage(String key) { Locale locale = LocaleContextHolder.getLocale(); ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasenames("classpath:messages"); messageSource.setUseCodeAsDefaultMessage(true); messageSource.setDefaultEncoding("UTF-8"); messageSource.setCacheSeconds(0);// www . j ava2 s . c o m return messageSource.getMessage(key, new Object[0], locale); }
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//from ww w .j av a2 s.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 the correct encoding is used. * @throws Exception any error// w ww. j a va 2s. c o 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 and starts with a blank line. * @throws Exception any error// w w w. j a v a 2 s. c om */ @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: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./* w w 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: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 va2s . com // 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); }