public String i18n(String messageKey) { return messages.get(messageKey, language); }
return getWithDefault(key, defaultMessage, language, args);
/** * Gets the requested localized message. * <p/> * <p> * The current Request and Response are used to help determine the messages * resource to use. * <ol> * <li>Exact locale match, return the registered locale message * <li>Language match, but not a locale match, return the registered * language message * <li>Return supplied default message * </ol> * <p> * The message can be formatted with optional arguments using the * {@link java.text.MessageFormat} syntax. * </p> * * @param key * @param defaultMessage * @param args * @return the message or the key if the key does not exist */ public String getWithDefault(String key, String defaultMessage, String language, Object... args) { String value = get(key, language, args); if (value.equals(key)) { // key does not exist, format default message value = formatMessage(defaultMessage, language, args); } return value; }
Properties messages = getMessagesForLanguage(language); String value = messages.getProperty(key); if (value != null) { return formatMessage(value, language, args); } else { log.warn("Failed to find '{}' in Messages", key);
public Application(PippoSettings settings) { this.pippoSettings = settings; this.languages = new Languages(settings); this.messages = new Messages(languages); this.mimeTypes = new MimeTypes(settings); this.httpCacheToolkit = new HttpCacheToolkit(settings); this.engines = new ContentTypeEngines(); this.initializers = new ArrayList<>(); this.webSocketRouter = new WebSocketRouter(); registerContentTypeEngine(TextPlainEngine.class); }
Messages messages = new Messages(); messages.setMessage("Some message here"); //Add all the regn IDs to the regIDs List. ArrayList<String> regIDs = messages.getRegIDs() regIDs.add("regnID1"); regIDs.add("regnID2"); regIDs.add("regnID3"); regIDs.add("regnID4"); //Add as many IDs as you want. //then create a new GSON Object. Gson gson = new Gson(); String jsonString = gson.toJSON(messages);
/** * Returns all localized messages. * <p> * The current Request and Response are used to help determine the messages * resource to use. * <ol> * <li>Exact locale match, return the registered locale messages * <li>Language match but not a locale match, return the registered language * messages * <li>Return the default messages * </ol> * </p> * * @param routeContext * @return all localized messages */ public Map<String, String> getAll(RouteContext routeContext) { String language = languages.getLanguageOrDefault(routeContext); return getAll(language); }
@AfterMethod public void tearDown(ITestResult result) { new Messages().screenshot(result); }
public String i18n(String messageKey) { return messages.get(messageKey, language); }
/** * Attempts to provide a localized message. * * @param messageKey * @param defaultMessage * @return the default message or a localized message or null */ protected String translate(String messageKey, String defaultMessage) { if (messages == null || Strings.isNullOrEmpty(messageKey)) { return Strings.emptyToNull(defaultMessage); } return Strings.emptyToNull(messages.getWithDefault(messageKey, defaultMessage, defaultLanguage)); }
@Path("rest/heartbeat") public class HeartbeatResource { @GET @Produces(MediaType.APPLICATION_XML) public Response heartbeatGet() { return Response.status(Status.OK).type(MediaType.APPLICATION_XML) .entity(new Messages("I am alive!")).build(); } }
public String i18n(String messageKey) throws IOException { return messages.get(messageKey, language); }
/** * Attempts to provide a localized message. * * @param messageKey * @param defaultMessage * @return the default message or a localized message or null */ protected String translate(String messageKey, String defaultMessage) { if (messages == null || Strings.isNullOrEmpty(messageKey)) { return Strings.emptyToNull(defaultMessage); } return Strings.emptyToNull(messages.getWithDefault(messageKey, defaultMessage, defaultLanguage)); }
var messages = new Messages(); for(var i = 0; i < 3; ++i){
public String i18n(String messageKey, Object... args) throws IOException { return messages.get(messageKey, language, args); }
protected void handleDeclaredThrownException(Exception e, Method method, Context context) { Class<? extends Exception> exceptionClass = e.getClass(); for (Return declaredReturn : declaredReturns) { if (exceptionClass.isAssignableFrom(declaredReturn.onResult())) { context.status(declaredReturn.code()); // prefer declared message to exception message String message = Strings.isNullOrEmpty(declaredReturn.description()) ? e.getMessage() : declaredReturn.description(); if (!Strings.isNullOrEmpty(declaredReturn.descriptionKey())) { // retrieve localized message, fallback to declared message message = messages.getWithDefault(declaredReturn.descriptionKey(), message, context); } if (!Strings.isNullOrEmpty(message)) { context.setLocal("message", message); } validateResponseHeaders(declaredReturn, context); log.warn("Handling declared return exception '{}' for '{}'", e.getMessage(), Util.toString(method)); return; } } if (e instanceof RuntimeException) { // pass-through the thrown exception throw (RuntimeException) e; } // undeclared exception, wrap & throw throw new FathomException(e); }
public static void main(String[] args) { Messages m = new Messages(); String jsonMessage = getJsonMessage(); // This gets the JSON message to be run through ObjectMapper ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); /* this is the fix */ try { Message message = mapper.readValue(jsonMessage, Message.class); } catch (Exception e) { e.printStackTrace(); } } // end method main()
public String i18n(String messageKey, Object... args) { return messages.get(messageKey, language, args); }
protected void handleDeclaredThrownException(Exception e, Method method, Context context) { Class<? extends Exception> exceptionClass = e.getClass(); for (Return declaredReturn : declaredReturns) { if (exceptionClass.isAssignableFrom(declaredReturn.onResult())) { context.status(declaredReturn.code()); // prefer declared message to exception message String message = Strings.isNullOrEmpty(declaredReturn.description()) ? e.getMessage() : declaredReturn.description(); if (!Strings.isNullOrEmpty(declaredReturn.descriptionKey())) { // retrieve localized message, fallback to declared message message = messages.getWithDefault(declaredReturn.descriptionKey(), message, context); } if (!Strings.isNullOrEmpty(message)) { context.setLocal("message", message); } validateResponseHeaders(declaredReturn, context); log.warn("Handling declared return exception '{}' for '{}'", e.getMessage(), Util.toString(method)); return; } } if (e instanceof RuntimeException) { // pass-through the thrown exception throw (RuntimeException) e; } // undeclared exception, wrap & throw throw new FathomException(e); }