@Override public final String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { String[] codes = resolvable.getCodes(); if (codes == null) { codes = new String[0]; } for (String code : codes) { String msg = getMessageInternal(code, resolvable.getArguments(), locale); if (msg != null) { return msg; } } String defaultMessage = resolvable.getDefaultMessage(); if (defaultMessage != null) { return renderDefaultMessage(defaultMessage, resolvable.getArguments(), locale); } if (codes.length > 0) { String fallback = getDefaultMessage(codes[0]); if (fallback != null) { return fallback; } } throw new NoSuchMessageException(codes.length > 0 ? codes[codes.length - 1] : null, locale); }
public String getMessage(String[] keys, Object[] vars, Locale locale) { String msg = "???" + (keys.length > 0 ? keys[keys.length-1] : "") + "???"; boolean found = false; int i = 0; while( ! found && i < keys.length) { try { msg = messageSource.getMessage( keys[i], vars, locale); found = true; } catch (NoSuchMessageException ex) { i++; } } if( ! found ) { String key = keys[keys.length-1]; if (key.startsWith("enum.")){ msg = key.substring(key.lastIndexOf(".") + 1); } } return msg; }
/** * Overridden in order to use MockPortletConfig * @see org.springframework.web.context.XmlWebApplicationContextTests#testWithoutMessageSource() */ @Test @SuppressWarnings("resource") public void withoutMessageSource() throws Exception { MockPortletContext portletContext = new MockPortletContext(""); MockPortletConfig portletConfig = new MockPortletConfig(portletContext); XmlPortletApplicationContext pac = new XmlPortletApplicationContext(); pac.setParent(root); pac.setPortletConfig(portletConfig); pac.setNamespace("testNamespace"); pac.setConfigLocations(new String[] {"/org/springframework/web/portlet/context/WEB-INF/test-portlet.xml"}); pac.refresh(); try { pac.getMessage("someMessage", null, Locale.getDefault()); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected; } String msg = pac.getMessage("someMessage", null, "default", Locale.getDefault()); assertEquals("Default message returned", "default", msg); }
@Test public void testReloadableResourceBundleMessageSourceWithInappropriateDefaultCharset() { ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource(); ms.setBasename("org/springframework/context/support/messages"); ms.setDefaultEncoding("unicode"); Properties fileCharsets = new Properties(); fileCharsets.setProperty("org/springframework/context/support/messages_de", "unicode"); ms.setFileEncodings(fileCharsets); ms.setFallbackToSystemLocale(false); try { ms.getMessage("code1", null, Locale.ENGLISH); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected } }
@Test public void testReloadableResourceBundleMessageSourceWithInappropriateEnglishCharset() { ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource(); ms.setBasename("org/springframework/context/support/messages"); ms.setFallbackToSystemLocale(false); Properties fileCharsets = new Properties(); fileCharsets.setProperty("org/springframework/context/support/messages", "unicode"); ms.setFileEncodings(fileCharsets); try { ms.getMessage("code1", null, Locale.ENGLISH); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected } }
@Test public void messageSourceResolvable() { // first code valid String[] codes1 = new String[] {"message.format.example3", "message.format.example2"}; MessageSourceResolvable resolvable1 = new DefaultMessageSourceResolvable(codes1, null, "default"); assertTrue("correct message retrieved", MSG_TXT3_US.equals(sac.getMessage(resolvable1, Locale.US))); // only second code valid String[] codes2 = new String[] {"message.format.example99", "message.format.example2"}; MessageSourceResolvable resolvable2 = new DefaultMessageSourceResolvable(codes2, null, "default"); assertTrue("correct message retrieved", MSG_TXT2_US.equals(sac.getMessage(resolvable2, Locale.US))); // no code valid, but default given String[] codes3 = new String[] {"message.format.example99", "message.format.example98"}; MessageSourceResolvable resolvable3 = new DefaultMessageSourceResolvable(codes3, null, "default"); assertTrue("correct message retrieved", "default".equals(sac.getMessage(resolvable3, Locale.US))); // no code valid, no default String[] codes4 = new String[] {"message.format.example99", "message.format.example98"}; MessageSourceResolvable resolvable4 = new DefaultMessageSourceResolvable(codes4); exception.expect(NoSuchMessageException.class); sac.getMessage(resolvable4, Locale.US); }
@Test @SuppressWarnings("resource") public void withoutMessageSource() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("testNamespace"); wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); wac.refresh(); try { wac.getMessage("someMessage", null, Locale.getDefault()); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected; } String msg = wac.getMessage("someMessage", null, "default", Locale.getDefault()); assertTrue("Default message returned", "default".equals(msg)); }
/** * 解析错误信息 * TODO 国际化错误信息应该异常中处理 * @param fieldError * @return */ private String resolveLocalizedErrorMessage(org.springframework.validation.FieldError fieldError) { Locale currentLocale = LocaleContextHolder.getLocale(); String localizedErrorMessage = null; if(messageSource != null){ try { localizedErrorMessage = messageSource.getMessage(fieldError.getCode(), fieldError.getArguments(), currentLocale); }catch (NoSuchMessageException noSuchMessageException){ logger.debug(noSuchMessageException); noSuchMessageException.printStackTrace(); } } //If the message was not found, return the most accurate field error code instead. //You can remove this check if you prefer to get the default error message. if (localizedErrorMessage.equals(fieldError.getDefaultMessage())) { String[] fieldErrorCodes = fieldError.getCodes(); localizedErrorMessage = fieldErrorCodes[0]; } return localizedErrorMessage; }
@Override public String interpolate(final String s, final Context context, final Locale locale) { try { return this.messageSource.getMessage(s, context.getConstraintDescriptor().getAttributes().values().toArray( new Object[context.getConstraintDescriptor().getAttributes().size()]), locale); } catch (final NoSuchMessageException e) { return this.defaultMessageInterpolator.interpolate(s, context, locale); } }
public String get(String key, Object... args) { try { if (messageSource != null) { return messageSource.getMessage(key, args, key, LocaleUtil.getLocale()); } return key; } catch (NoSuchMessageException e) { return key; } }
public String interpolate(final String s, final Context context, final Locale locale) { try { return this.messageSource.getMessage(s, context.getConstraintDescriptor().getAttributes().values().toArray( new Object[context.getConstraintDescriptor().getAttributes().size()]), locale); } catch (final NoSuchMessageException e) { return this.defaultMessageInterpolator.interpolate(s, context, locale); } }
protected String getMessage(String key, String defaultMessageKey, Object... parameters) { try { return this.messageSource.getMessage(key, parameters, LocaleContextHolder.getLocale()); } catch (NoSuchMessageException e) { this.log.warn("No message key <{}> found, defaulting to <{}> ", key, defaultMessageKey); return this.getMessage(defaultMessageKey); } }
public String getI18nMessage(String key, Object[] args) { String msg = ""; if (StringUtils.isBlank(key)) { log.warn("Missing i18n key, returning warning message"); msg = "{ERROR: MISSING KEY}"; } else { try { msg = messageSource.getMessage(key, args, null); } catch (NoSuchMessageException e) { log.warn("Invalid i18n key ("+key+") could not be found, returning warning message"); msg = "{ERROR: INVALID KEY: "+key+"}"; } } return msg; }
/** * This implementation resolves the code in the MessageSource. * Returns {@code null} if the message could not be resolved. */ @Override protected Object handleGetObject(String key) { try { return this.messageSource.getMessage(key, null, this.locale); } catch (NoSuchMessageException ex) { return null; } }
/** * This implementation checks whether the target MessageSource can resolve * a message for the given key, translating {@code NoSuchMessageException} * accordingly. In contrast to ResourceBundle's default implementation in * JDK 1.6, this does not rely on the capability to enumerate message keys. */ @Override public boolean containsKey(String key) { try { this.messageSource.getMessage(key, null, this.locale); return true; } catch (NoSuchMessageException ex) { return false; } }
@Override public String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException { if (this.parentMessageSource != null) { return this.parentMessageSource.getMessage(code, args, locale); } else { throw new NoSuchMessageException(code, locale); } }
@Override public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { if (this.parentMessageSource != null) { return this.parentMessageSource.getMessage(resolvable, locale); } else { if (resolvable.getDefaultMessage() != null) { return renderDefaultMessage(resolvable.getDefaultMessage(), resolvable.getArguments(), locale); } String[] codes = resolvable.getCodes(); String code = (codes != null && codes.length > 0 ? codes[0] : null); throw new NoSuchMessageException(code, locale); } }
@Override public final String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException { String msg = getMessageInternal(code, args, locale); if (msg != null) { return msg; } String fallback = getDefaultMessage(code); if (fallback != null) { return fallback; } throw new NoSuchMessageException(code, locale); }
/** * Get the I18N description for this key. If the tool has supplied a messageService, then this is used to look up * the key and hence get the text. Otherwise if the tool has supplied a I18N languageFilename then it is accessed * via the shared toolActMessageService. If neither are supplied or the key is not found, then any "." in the name * are converted to space and this is used as the return value. * * This is normally used to get the description for a definition, in whic case the key should be in the format * output.desc.[definition name], key = definition name and addPrefix = true. For example a definition name of * "learner.mark" becomes output.desc.learner.mark. * * If you want to use this to get an arbitrary string from the I18N files, then set addPrefix = false and the * output.desc will not be added to the beginning. */ protected String getI18NText(String key, boolean addPrefix) { String translatedText = null; MessageSource tmpMsgSource = getMsgSource(); if (tmpMsgSource != null) { if (addPrefix) { key = KEY_PREFIX + key; } Locale locale = LocaleContextHolder.getLocale(); try { translatedText = tmpMsgSource.getMessage(key, null, locale); } catch (NoSuchMessageException e) { log.warn("Unable to internationalise the text for key " + key + " as no matching key found in the msgSource"); } } else { log.warn("Unable to internationalise the text for key " + key + " as no matching key found in the msgSource. The tool's OutputDefinition factory needs to set either (a) messageSource or (b) loadedMessageSourceService and languageFilename."); } if (translatedText == null || translatedText.length() == 0) { translatedText = key.replace('.', ' '); } return translatedText; }
/** * @see org.springframework.context.support.MessageSourceAccessor#getMessage(java.lang.String) * @param key * @return */ public String getMessage(String key) { String message; try { message = messageSource.getMessage(key, null, LocaleContextHolder.getLocale()); } catch (NoSuchMessageException e) { message = "??" + key + "??"; } return message; }
/** * @see org.springframework.context.support.MessageSourceAccessor#getMessage(java.lang.String, java.lang.String) * @param key * @param defaultMessage * @return */ public String getMessage(String key, String defaultMessage) { String message = defaultMessage; try { message = messageSource.getMessage(key, null, defaultMessage, LocaleContextHolder.getLocale()); } catch (NoSuchMessageException e) { message = defaultMessage; } return message; }
/** * @see org.springframework.context.support.MessageSourceAccessor#getMessage(java.lang.String, java.lang.Object[]) * @param key * @param args * @return */ public String getMessage(String key, Object[] args) { String message; try { message = messageSource.getMessage(key, args, LocaleContextHolder.getLocale()); } catch (NoSuchMessageException e) { message = "??" + key + "??"; } return message; }
/** * @see org.springframework.context.support.MessageSourceAccessor#getMessage(java.lang.String, java.lang.Object[], * java.lang.String) * @param key * @param args * @param defaultMessage * @return */ public String getMessage(String key, Object[] args, String defaultMessage) { String message = defaultMessage; try { message = messageSource.getMessage(key, args, defaultMessage, LocaleContextHolder.getLocale()); } catch (NoSuchMessageException e) { message = defaultMessage; } return message; }
public String getTranslation(String translationKey, Object[] args) { String translation = translationKey; try { translation = context.getMessage(translationKey, args, locale); } catch (NoSuchMessageException e) { System.out.println(e); } return translation; }
/** * Instantiates a new graph node type. * * @param name the nodetype name */ public GraphNodeType(String name, MessageSource colors){ //just pass in name, the rest is constructed using configuration if (name!=null){ this.name=name; this.displayName=name.replace("_", " "); String nodeProps = null; try { nodeProps = colors.getMessage(name, null, Locale.ENGLISH); } catch (NoSuchMessageException e) { // null nodeProps handled below } if (nodeProps!=null && nodeProps.contains("|")) { String[] props = nodeProps.split("\\|"); this.color = props[0]; this.shape = props[1]; if (this.shape.equals(Constants.NODESHAPE_IMAGE) && props.length==4){ this.image = props[2]; this.description = props[3]; } else { this.description = props[2]; } } else { //set defaults this.color=Constants.DEFAULT_NODE_COLOR; this.shape=Constants.DEFAULT_NODE_SHAPE; } } }
public String getMessage(String key, Object[] vars) { try { return messageSource.getMessage( key, vars, null); } catch (NoSuchMessageException ex) { return "???" + key + "???"; } }
protected String getMessage(String key, Object[] vars) { try { return messageSource.getMessage( key, vars, null); } catch (NoSuchMessageException ex) { return "???" + key + "???"; } }
/** * {@inheritDoc} */ public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { for (String code : resolvable.getCodes()) { String message = getMessage(code, resolvable.getArguments(), locale); if (message != null && message.length() > 0) { return message; } } return resolvable.getDefaultMessage(); }
public String get( String id ){ try{ return messageSource.getMessage( id, null, LocaleContextHolder.getLocale() ); }catch( NoSuchMessageException e ){ return id; } }