Skip to content
rodionmoiseev edited this page May 12, 2012 · 2 revisions

Scala Integration

C10N works nicely in Scala. Simply create a trait with annotated abstract methods, to define your translations.

If method's return type is another c10n trait it will be automatically populated with relevant translations, to allow you to chain several levels of traits, as shown in example below:

object Main {
  def main(args: Array[String]) {
    /*
     * Use default locale bindings:
     * @En -> Locale.ENGLISH
     * @Ru -> Locale("ru")
     */
    C10N.configure(new DefaultC10NAnnotations())

    val gui: Gui = C10N.get(classOf[Gui])

    // assume a certain locale for illustration purposes
    Locale.setDefault(Locale.ENGLISH)
    //Locale.setDefault(new Locale("ru"))

    println("Welcome, " + gui.hello + "!")
    println("Click " + gui.menu.file + " to start.")
    println("Click " + gui.menu.print.pageSettings + " to open page settings.")
    println("Click " + gui.menu.print.execute("Canon iP90v") + " to print")
    println("Push the " + gui.menu.print.redButton + " for something special")
  }

  @C10NMessages
  trait Gui {
    @En("C10N user")
    @Ru("Пользователь C10N")
    def hello: String
    def menu: Menu
  }

  @C10NMessages
  trait Menu {
    @En("File Menu")
    @Ru("Файл-меню")
    def file: String
    def print: PrintMenu
  }

  @C10NMessages
  trait PrintMenu extends ExtendedPrintMenu{
    @En("Page Settings ...")
    @Ru("Настройки страницы ...")
    def pageSettings: String = "default values are ignored, sorry."

    @En("Print with {0}")
    @Ru("Распечатать на {0}")
    def execute(printerName: String): String
  }

  trait ExtendedPrintMenu{
    @En("red button")
    @Ru("красная кнопка")
    def redButton: String
  }
}

The example also illustrates how you can extend traits (for example PrintMenu trait extends ExtendedPrintMenu) to structure your translations.

See also: Play Framework 2.0 Integration

Clone this wiki locally