/** * Get the message at the given key. * * Uses `java.text.MessageFormat` internally to format the message. * * @param key the message key * @param args the message arguments * @return the formatted message or a default rendering if the key wasn't defined */ default String apply(String key, Object... args) { return at(key, args); }
/** * @return the current lang */ public Lang lang() { if (lang != null) { return lang; } else { return messages().lang(); } }
@Override default play.api.i18n.Messages messages() { return this.asScala(); } }
/** * @param key Alternate field name (default to java variable name) * @return The translated message */ public String message(String key) { key = Messages.get(key); Object[] args = new Object[variables.length + 1]; System.arraycopy(variables, 0, args, 1, variables.length); args[0] = key; return Messages.get(message, args); }
public static String formatString(Locale locale, String value, Object... args) { String message = String.format(locale, value, coolStuff(value, args)); Matcher matcher = recursive.matcher(message); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, get(matcher.group(1))); } matcher.appendTail(sb); return sb.toString(); }
private List<ValidationError> getFieldErrorsAsValidationErrors(Lang lang, BindingResult result) { return result.getFieldErrors().stream().map(error -> { String key = error.getObjectName() + "." + error.getField(); if (key.startsWith("target.") && rootName == null) { key = key.substring(7); } if (error.isBindingFailure()) { ImmutableList.Builder<String> builder = ImmutableList.builder(); final Messages msgs = lang != null ? new MessagesImpl(lang, this.messagesApi) : null; for (String code: error.getCodes()) { code = REPLACE_TYPEMISMATCH.matcher(code).replaceAll(Matcher.quoteReplacement(INVALID_MSG_KEY)); if (msgs == null || msgs.isDefinedAt(code)) { builder.add(code); } } final ImmutableList<String> messages = builder.build(); return new ValidationError(key, messages.isEmpty() ? Arrays.asList(INVALID_MSG_KEY) : messages.reverse(), convertErrorArguments(error.getArguments())); } else { return new ValidationError(key, error.getDefaultMessage(), convertErrorArguments(error.getArguments())); } }).collect(Collectors.toList()); }
/** * Get a messages context appropriate for the given candidates. * * Will select a language from the candidates, based on the languages available, and fallback to the default language * if none of the candidates are available. */ public Messages preferred(Http.RequestHeader request) { play.api.i18n.Messages msgs = messages.preferred(request); return new Messages(new Lang(msgs.lang()), this); }
public String __getMessage(Object[] val) { if (val == null) { throw new NullPointerException("You are trying to resolve a message with an expression " + "that is resolved to null - " + "have you forgotten quotes around the message-key?"); } if (val.length == 1) { return Messages.get(val[0]); } else { // extract args from val Object[] args = new Object[val.length - 1]; for (int i = 1; i < val.length; i++) { args[i - 1] = val[i]; } return Messages.get(val[0], args); } }
private List<ValidationError> getFieldErrorsAsValidationErrors(Lang lang, BindingResult result) { return result.getFieldErrors().stream().map(error -> { String key = error.getObjectName() + "." + error.getField(); if (key.startsWith("target.") && rootName == null) { key = key.substring(7); } if (error.isBindingFailure()) { ImmutableList.Builder<String> builder = ImmutableList.builder(); final Messages msgs = lang != null ? new MessagesImpl(lang, this.messagesApi) : null; for (String code: error.getCodes()) { code = REPLACE_TYPEMISMATCH.matcher(code).replaceAll(Matcher.quoteReplacement(INVALID_MSG_KEY)); if (msgs == null || msgs.isDefinedAt(code)) { builder.add(code); } } final ImmutableList<String> messages = builder.build(); return new ValidationError(key, messages.isEmpty() ? Arrays.asList(INVALID_MSG_KEY) : messages.reverse(), convertErrorArguments(error.getArguments())); } else { return new ValidationError(key, error.getDefaultMessage(), convertErrorArguments(error.getArguments())); } }).collect(Collectors.toList()); }
/** * Get a messages context appropriate for the given candidates. * * Will select a language from the candidates, based on the languages available, and fallback to the default language * if none of the candidates are available. */ public Messages preferred(Collection<Lang> candidates) { Seq<Lang> cs = JavaConversions.collectionAsScalaIterable(candidates).toSeq(); play.api.i18n.Messages msgs = messages.preferred((Seq) cs); return new Messages(new Lang(msgs.lang()), this); }
/** * The current lang * * @return the current lang * * @deprecated Deprecated as of 2.7.0. <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">See migration guide.</a>. */ @Deprecated public Lang lang() { if (lang != null) { return lang; } else { return messages().lang(); } }
/** * Get the message at the first defined key. * * Uses `java.text.MessageFormat` internally to format the message. * * @param keys the messages keys * @param args the message arguments * @return the formatted message or a default rendering if the key wasn't defined */ default String apply(List<String> keys, Object... args) { return at(keys, args); }
public void success(String value, Object... args) { put("success", Messages.get(value, args)); }
@Override default play.api.i18n.Messages messages() { return this.asScala(); } }
private List<ValidationError> getFieldErrorsAsValidationErrors(Lang lang, BindingResult result) { return result.getFieldErrors().stream().map(error -> { String key = error.getObjectName() + "." + error.getField(); if (key.startsWith("target.") && rootName == null) { key = key.substring(7); } if (error.isBindingFailure()) { ImmutableList.Builder<String> builder = ImmutableList.builder(); final Messages msgs = lang != null ? new MessagesImpl(lang, this.messagesApi) : null; for (String code: error.getCodes()) { code = REPLACE_TYPEMISMATCH.matcher(code).replaceAll(Matcher.quoteReplacement(INVALID_MSG_KEY)); if (msgs == null || msgs.isDefinedAt(code)) { builder.add(code); } } final ImmutableList<String> messages = builder.build(); return new ValidationError(key, messages.isEmpty() ? Arrays.asList(INVALID_MSG_KEY) : messages.reverse(), convertErrorArguments(error.getArguments())); } else { return new ValidationError(key, error.getDefaultMessage(), convertErrorArguments(error.getArguments())); } }).collect(Collectors.toList()); }
protected Binding setUpBindingVariables(Map<String, Object> args) { Binding binding = new Binding(args); binding.setVariable("play", new Play()); binding.setVariable("messages", new Messages()); binding.setVariable("lang", Lang.get()); return binding; }
/** * The current lang * * @return the current lang * * @deprecated Deprecated as of 2.7.0. <a href="https://www.playframework.com/documentation/latest/JavaHttpContextMigration27">See migration guide.</a>. */ @Deprecated public Lang lang() { if (lang != null) { return lang; } else { return messages().lang(); } }
/** * Get the message at the given key. * * Uses `java.text.MessageFormat` internally to format the message. * * @param key the message key * @param args the message arguments * @return the formatted message or a default rendering if the key wasn't defined */ default String apply(String key, Object... args) { return at(key, args); }
public void error(String value, Object... args) { put("error", Messages.get(value, args)); }
@Override default play.api.i18n.Messages messages() { return this.asScala(); } }