Skip to content

Instantly share code, notes, and snippets.

@mcku
Created November 27, 2018 18:57
Show Gist options
  • Save mcku/dd71065a93bf409e8688e75734fde6ce to your computer and use it in GitHub Desktop.
Save mcku/dd71065a93bf409e8688e75734fde6ce to your computer and use it in GitHub Desktop.
a sample t function for translation
import com.thoughtworks.binding.{Binding, FutureBinding, dom}
import io.udash.i18n.{Bundle, Lang, LocalTranslationProvider, Translated, TranslationKey, TranslationKey0, TranslationKey1, TranslationKey2, TranslationProvider}
import scala.concurrent.Future
import scala.util.{Failure, Success, Try}
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
class NovaFrontendTranslationProvider(bundles: Map[Lang,Bundle], val strToKeyMapNoArgs: Map[String, TranslationKey0]) extends LocalTranslationProvider(bundles)
object TranslationHelper {
// https://guide.udash.io/ext/i18n
implicit def makeIntelliJHappy(x: String): Binding[String] = ???
// this one assumes all the strings are in the Raw Strings Map. See NovaTranslations
@dom def t(sourceStr: String)(implicit translationProvider: NovaFrontendTranslationProvider , lang: Lang): Binding[String] = {
import translationProvider.strToKeyMapNoArgs
val xb: Option[Try[Translated]] = FutureBinding(strToKeyMapNoArgs(sourceStr)()).bind
xb match {
case None | Some(Failure(_)) => ""
case Some(Success(trx)) => trx.string
}
}
@dom def t(source: TranslationKey0 )(implicit translationProvider: TranslationProvider, lang: Lang): Binding[String] = {
val xb: Option[Try[Translated]] = FutureBinding(source()).bind
xb match {
case None | Some(Failure(_)) => ""
case Some(Success(trx)) => trx.string
}
}
@dom def t[T <: TranslationKey](source: T, args: Any* )(implicit translationProvider: TranslationProvider, lang: Lang): Binding[String] = {
val tFut: Future[Translated] = args.length match {
case 0 =>
translationProvider.translate(source.key)
case _ =>
translationProvider.translate(source.key,args:_*)
}
val xb: Option[Try[Translated]] = FutureBinding(tFut).bind
xb match {
case None | Some(Failure(_)) => ""
case Some(Success(trx)) => trx.string
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment