Skip to content

Instantly share code, notes, and snippets.

@propensive
Last active August 29, 2015 14:19
Show Gist options
  • Save propensive/bb67dc946b481fdc585a to your computer and use it in GitHub Desktop.
Save propensive/bb67dc946b481fdc585a to your computer and use it in GitHub Desktop.
scala> import rapture.i18n._
import rapture.i18n._
scala> import languages._
import languages._
// This would typically be a runtime value, but we'll just use "FR" as a fixed example
scala> val localeString = "FR"
localeString: String = FR
// Note that our Locale implicit is typed on the possible languages
scala> implicit val locale = (en | fr | la).parse(localeString)
locale: rapture.i18n.Locale[rapture.i18n.En with rapture.i18n.Fr with rapture.i18n.La] = fr
// That was the single point in the program that a runtime error could occur. From now on, we're safe!
// We have provided a message in a superset of these languages
scala> val msg = en"Hello" | fr"Bonjour" | de"Hallo" | la"Salve"
msg: rapture.i18n.I18n[String,rapture.i18n.En with rapture.i18n.Fr with rapture.i18n.De with rapture.i18n.La] = en|fr|de|la:Hello
// This will automatically convert to a String, using the implicit Locale
scala> msg: String
res0: String = Bonjour
// But if we do not provide all the languages, like this...
scala> val msg2 = fr"Au revoir" | it"Ciao"
msg2: rapture.i18n.I18n[String,rapture.i18n.Fr with rapture.i18n.It] = fr|it:Au revoir
// ...then we get a type error
scala> msg2: String
<console>:19: error: type mismatch;
found : rapture.i18n.I18n[String,rapture.i18n.Fr with rapture.i18n.It]
required: String
msg2: String
^
// This doesn't work because we would not be not be able to handle the cases where localeString was "EN" or "DE".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment