Package play.i18n

Class MessagesApi

java.lang.Object
play.i18n.MessagesApi

@Singleton public class MessagesApi extends Object
The messages API.
  • Constructor Details

    • MessagesApi

      @Inject public MessagesApi(play.api.i18n.MessagesApi messages)
  • Method Details

    • asScala

      public play.api.i18n.MessagesApi asScala()
      Returns:
      the Scala versions of the Messages API.
    • get

      public String get(play.api.i18n.Lang lang, String key, Object... args)
      Translates a message.

      Uses `java.text.MessageFormat` internally to format the message.

      Parameters:
      lang - the message lang
      key - the message key
      args - the message arguments
      Returns:
      the formatted message or a default rendering if the key wasn't defined
    • get

      public String get(play.api.i18n.Lang lang, List<String> keys, Object... args)
      Translates the first defined message.

      Uses `java.text.MessageFormat` internally to format the message.

      Parameters:
      lang - the message lang
      keys - the messages keys
      args - the message arguments
      Returns:
      the formatted message or a default rendering if the key wasn't defined
    • isDefinedAt

      public Boolean isDefinedAt(play.api.i18n.Lang lang, String key)
      Check if a message key is defined.
      Parameters:
      lang - the message lang
      key - the message key
      Returns:
      a Boolean
    • preferred

      public Messages preferred(Collection<Lang> candidates)
      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.

      Parameters:
      candidates - the candidate languages
      Returns:
      the most appropriate Messages instance given the candidate languages
    • preferred

      public Messages preferred(Http.RequestHeader request)
      Get a messages context appropriate for the given request.

      Will select a language from the request, based on the languages available, and fallback to the default language if none of the candidates are available.

      Parameters:
      request - the incoming request
      Returns:
      the preferred messages context for the request
    • setLang

      public Result setLang(Result result, Lang lang)
      Given a Result and a Lang, return a new Result with the lang cookie set to the given Lang.
      Parameters:
      result - the result where the lang will be set.
      lang - the lang to set on the result
      Returns:
      a new result with the lang.
    • clearLang

      public Result clearLang(Result result)
      Given a Result, return a new Result with the lang cookie discarded.
      Parameters:
      result - the result to clear the lang.
      Returns:
      a new result with a cleared lang.
    • langCookieName

      public String langCookieName()
      Name for the language Cookie.
    • langCookieMaxAge

      public OptionalInt langCookieMaxAge()
      An optional max age in seconds for the language Cookie.
    • langCookieSecure

      public boolean langCookieSecure()
      Whether the secure attribute of the cookie is true or not.
    • langCookieHttpOnly

      public boolean langCookieHttpOnly()
      Whether the HTTP only attribute of the cookie should be set to true or not.
    • langCookieSameSite

      public Optional<Http.Cookie.SameSite> langCookieSameSite()
      The value of the [[SameSite]] attribute of the cookie. If None, then no SameSite attribute is set.