Package ninja.i18n
Class MessagesImpl
- java.lang.Object
-
- ninja.i18n.MessagesImpl
-
-
Constructor Summary
Constructors Constructor Description MessagesImpl(NinjaProperties ninjaProperties, Lang lang)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Optional<String>get(String key, Optional<String> language, Object... params)Get a translated string.Optional<String>get(String key, Context context, Optional<Result> result, Object... parameter)Get a translated string.Map<Object,Object>getAll(Optional<String> language)Map<Object,Object>getAll(Context context, Optional<Result> result)Returns all messages for the default language in that context / result.StringgetWithDefault(String key, String defaultMessage, Optional<String> language, Object... params)Gets a message for a message key.StringgetWithDefault(String key, String defaultMessage, Context context, Optional<Result> result, Object... params)Gets a message for a message key.
-
-
-
Constructor Detail
-
MessagesImpl
@Inject public MessagesImpl(NinjaProperties ninjaProperties, Lang lang)
-
-
Method Detail
-
get
public Optional<String> get(String key, Context context, Optional<Result> result, Object... parameter)
Description copied from interface:MessagesGet a translated string. The language is determined by the provided locale or a suitable fallback. values of keys can use the MessageFormat. More here: http://docs.oracle.com/javase/6/docs/api/java/text/MessageFormat.html But in short you can use something like mymessage=my message with a placeholder {0}- Specified by:
getin interfaceMessages- Parameters:
key- The key used in your message file (conf/messages.properties)context- The context used to determine the language.result- The result used to determine the language.parameter- Parameters to use in formatting the message of the key (inMessageFormat).- Returns:
- The matching and formatted value or absent if not found.
-
get
public Optional<String> get(String key, Optional<String> language, Object... params)
Description copied from interface:MessagesGet a translated string. The language is determined by the provided locale or a suitable fallback. values of keys can use the MessageFormat. More here: http://docs.oracle.com/javase/6/docs/api/java/text/MessageFormat.html But in short you can use something like mymessage=my message with a placeholder {0} parameter will then be used to fill {0} with the content. Note: If you don't want to determine the language yourself please useMessages#get(String, Context, Result, Object...)- Specified by:
getin interfaceMessages- Parameters:
key- The key used in your message file (conf/messages.properties)language- The language to get. Can be null - then the default language is returned. It also looks for a fallback. Eg. A request for "en-US" will fallback to "en" if there is no matching language file.params- Parameters to use in formatting the message of the key (inMessageFormat).- Returns:
- The matching and formatted value or absent if not found.
-
getAll
public Map<Object,Object> getAll(Context context, Optional<Result> result)
Description copied from interface:MessagesReturns all messages for the default language in that context / result. Does NOT format any MessageFormat values. You have to do that yourself in the controller logic most likely.
-
getAll
public Map<Object,Object> getAll(Optional<String> language)
- Specified by:
getAllin interfaceMessages- Parameters:
language- The language to get. May be absent - then the default language is returned. It also looks for a fallback. Eg. A request for "en-US" will fallback to "en" if there is no matching language file. language can also be the complete Accept-Language header: en-US,en;q=0.8,de;q=0.6- Returns:
- A map with all messages as
-
getWithDefault
public String getWithDefault(String key, String defaultMessage, Context context, Optional<Result> result, Object... params)
Description copied from interface:MessagesGets a message for a message key. Returns a defaultValue if not found.- Specified by:
getWithDefaultin interfaceMessages- Parameters:
key- The key used in your message file (conf/messages.properties)defaultMessage- A default message that will be used when no matching message can be retrieved.context- The context used to determine the language.result- The result used to determine the language. May be absentparams- Parameters to use in formatting the message of the key (inMessageFormat).- Returns:
- The matching and formatted value (either from messages or the default one).
-
getWithDefault
public String getWithDefault(String key, String defaultMessage, Optional<String> language, Object... params)
Description copied from interface:MessagesGets a message for a message key. Returns a defaultValue if not found. Note: If you don't want to determine the language yourself please useMessages#getWithDefault(String, Context, Result, Object...)- Specified by:
getWithDefaultin interfaceMessages- Parameters:
key- The key used in your message file (conf/messages.properties)defaultMessage- A default message that will be used when no matching message can be retrieved.language- The language to get. May be absent - then the default language is returned. It also looks for a fallback. Eg. A request for "en-US" will fallback to "en" if there is no matching language file.- Returns:
- The matching and formatted value (either from messages or the default one).
-
-