class Messages extends Logging
Internationalization (i18n) and localization (l10n) helper class.
Message files (located in resources/i18n/
, by default) are written using
Typesafe Config "HOCON" format.
HOCON fully
supports UTF-8 Unicode and is more flexible and powerful than Java .properties
.
For instance, it is possible to have a locale such as "en-CA" to automatically
default to another locale, say "en", if a message is missing:
just add the include "messages.en.conf"
line to the messages.en-CA.conf
file.
Messages are rendered by ICU4J, which provides
comprehensive support for Unicode, globalization, and internationalization.
Compared to Java MessageFormat
, ICU4J supports named and numbered arguments,
enhanced gender and plurals, user-friendly apostrophe quoting syntax,
cardinal (one, two) and ordinal numbers (1st, 2nd, 3rd),
and much more.
- Source
- Messages.scala
@i18n object messages implicit val defaultLocale = Locale.ENGLISH val ptBr = Locale.forLanguageTag("pt-BR") assert(messages.helloWorld == "Hello World!") assert(messages.helloWorld(ptBr) == "Oi Mundo!")
Formatted messages are also supported:
, assert(messages.hello(name = "Scail") == "Hello Scail!") assert(messages.hello(name = "Scail")(ptBr) == "Oi Scail!")
One can pass custom parameters to the annotation by extending the
Messages
class: , @i18n object messages extends Messages(filename = "sample")
Please notice that parameter order in the method follows the same order as they were specified in the messages file. Therefore, it is highly recommended to use named parameters exclusively, as changes in the messages file can easily change parameter names or their relative order. Parameter types are inferred as
java.util.Date
if a date format is used in the messages file, asjava.lang.Number
if a number format is used, andAny
otherwise.- See also
SelectFormat Optionally, one may use the
@i18n
macro annotation for classes and objects. The@i18n
macro reads the messages file at compile time and generates typesafe methods for every localized message.
- Alphabetic
- By Inheritance
- Messages
- Logging
- LazyLogging
- AnyRef
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
- new Messages(folder: String = "i18n", filename: String = "messages", extension: String = "conf", cache: Cache[Locale, Config] = SimpleCache.empty, formatCache: Cache[KeyLocale, MessageFormat] = NoCache.empty)
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
- def +(other: String): String
- def ->[B](y: B): (Messages, B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
apply(key: Key, args: (String, Any)*)(implicit locale: Locale): String
Formats the localized message pattern identified by
key
for the givenlocale
using the given argumentsargs
.Formats the localized message pattern identified by
key
for the givenlocale
using the given argumentsargs
.- key
the key identifying the message pattern
- args
a map of arguments to be replaced into the pattern
- locale
the locale
- returns
the formatted localized message if it exists, a default placeholder otherwise
- Annotations
- @SuppressWarnings()
-
def
apply(key: Key)(implicit locale: Locale): String
Returns the localized message identified by
key
for the givenlocale
.Returns the localized message identified by
key
for the givenlocale
.- key
the key identifying the message
- locale
the locale
- returns
the localized message if it exists, a default placeholder otherwise
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
- def ensuring(cond: (Messages) ⇒ Boolean, msg: ⇒ Any): Messages
- def ensuring(cond: (Messages) ⇒ Boolean): Messages
- def ensuring(cond: Boolean, msg: ⇒ Any): Messages
- def ensuring(cond: Boolean): Messages
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
- def formatted(fmtstr: String): String
-
def
get(key: Key, args: (String, Any)*)(implicit locale: Locale): Option[String]
Optionally formats the localized message pattern identified by
key
for the givenlocale
using the given argumentsargs
.Optionally formats the localized message pattern identified by
key
for the givenlocale
using the given argumentsargs
.- key
the key identifying the message pattern
- args
a map of arguments to be replaced into the pattern
- locale
the locale
- returns
the optional value containing the formatted localized message if it exists,
None
otherwise
- Annotations
- @SuppressWarnings()
-
def
get(key: Key)(implicit locale: Locale): Option[String]
Optionally returns the localized message identified by
key
for the givenlocale
.Optionally returns the localized message identified by
key
for the givenlocale
.- key
the key identifying the message
- locale
the locale
- returns
the optional value containing the localized message if it exists,
None
otherwise
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
lazy val
logger: Logger
- Attributes
- protected
- Definition Classes
- LazyLogging
- Annotations
- @transient()
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
- def →[B](y: B): (Messages, B)