trait ShowLocal[A] extends AnyRef
Displays locale specific string for any object.
ShowLocal
is a type class that extends classes with the showLocal
method. The method takes an implicit java.util.Locale
defining in what
language the returned string should be. In order to add the extension
method to an object it is necessary to define an implicit implementation
of the ShowLocal trait.
Example:
case class Person(name: String, age: Int) implicit val showLocalForPerson: ShowLocal[Person] = new ShowLocal[Person] { def showLocal(person: Person)(implicit locale: Locale): String = { locale.getLanguage match { case "en" => s"Name: ${person.name}, Age: ${person.age}" case "fr" => s"prénom: ${person.name}, âge: ${person.age}" } } } val person = Person("John Smith", 32) implicit val locale = Locale.ENGLISH person.showLocal person.showLocal(Locale.FRENCH)
Output:
Name: John Smith, Age: 32 prénom: John Smith, âge: 32
The showLocal
extension method can be defined for any class A
provided an implementation of ShowLocal[A]
exists implicitly and
is in scope. The implicit definitions are generally defined in separate
classes in a "instances" sub-directory enclosed within a trait. The
trait is then mixed into an Implicits object allowing user to only
import Implicits._
.
For the example above the trait class would look like:
trait PersonInstances { implicit val showLocalForPerson: ShowLocal[Person] = new ShowLocal[Person] { def showLocal(person: Person)(implicit locale: Locale): String = { locale.getLanguage match { case "en" => s"Name: ${person.name}, Age: ${person.age}" case "fr" => s"prénom: ${person.name}, âge: ${person.age}" } } } }
stored in the file instances/PersonInstances.scala. All implicits would be mixed in a file called Implicits.scala that looks like:
object Implicits extends instances.PersonInstances
- A
type for which a language specific string is generated
- Source
- ShowLocal.scala
- Alphabetic
- By Inheritance
- ShowLocal
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Abstract Value Members
- abstract def showLocal(a: A)(implicit locale: Locale): String
Produces a language specific string for an object
Produces a language specific string for an object
In order to support fully internationalisation it is necessary to encode strings in language independent structures. Some examples of these structures include Message and Error. At some point a language specific string of the structure is required. The language to display is determined by a
java.util.Locale
value.The
showLocal
method returns a string for a given object conforming to the given locale. Each class that wants to be able to use this method must implement this trait for the class type.The method is extended to classes that have a defined
ShowLocal
implementation and is invoked by:// implicit invocation implicit val locale = Locale.GERMAN object.showLocal // explicit invocation object.showLocal(Locale.GERMAN)
- a
object to show language specific string for
- locale
defining the language to use for the string
- returns
language specific string describing the object
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##(): Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- 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(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
Documentation for the Axiell Utilities Project
Overview
The Axiell Utilities Project attempts to provide a small library of classes and object applicable to most Axiell developments. The library is not geared towards any one project and so should be useful for new projects as well as existing ones.
The library is based on a few simple principles, namely:
Package structure
The
package consists of a number of utilities, where each utility provides a single piece of functionality.com.axiell.util
Notable utilities are:
Configure
generic configuration reader allowing application settings defined as resources to be interrogated.Error
generic error handling class defining a locale independent error message along with details relating to the context of the error. The context may include an exception and other relevant values.Formatter
retrieves and formats a locale specific string from a specified resource bundle. The formatting is specified in aC
printf style.Implicits
implements implicit methods and classes used to pimp methods onto existing classes (e.g.fold()
method toBoolean
class) and to provide implicit implementations for the JsonLocal and ShowLocal type classes.JsonLocal
type class used to output locale specific JSON structures. Implementations exist for Message and Error.Message
provides a locale independent mechanism for handling strings. The strings for a given locale are stored in resource bundles that are accessed when the message is to be displayed.ShowLocal
type class used to show locale specific strings. Allows Message and Error to be converted to a given language string.Dependencies
The list of dependencies are:
Typesafe Config
a generic configuration file reader. The library is used by theConfigure
class to access settings stored as resources within an application.