Skip to content

Commit

Permalink
Rewritten macros, added lazily resolved strings (when language is not…
Browse files Browse the repository at this point in the history
… known in advance).
  • Loading branch information
makkarpov committed Apr 14, 2016
1 parent c1c7b73 commit 94e4a85
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 219 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ val common = Seq(

licenses := Seq("Apache 2" -> url("http://www.apache.org/licenses/LICENSE-2.0.txt")),
homepage := Some(url("https://github.com/makkarpov/scalingua")),
organizationHomepage := Some(url("https://github.com/makkarpov")),
scmInfo := Some(ScmInfo(
browseUrl = new URL("https://github.com/makkarpov/scalingua"),
browseUrl = url("https://github.com/makkarpov/scalingua"),
connection = "scm:git://github.com/makkarpov/scalingua.git"
)),

Expand Down
31 changes: 31 additions & 0 deletions core/src/main/scala/ru/makkarpov/scalingua/LValue.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/******************************************************************************
* Copyright © 2016 Maxim Karpov *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
******************************************************************************/

package ru.makkarpov.scalingua

import scala.language.implicitConversions

object LValue {
implicit def unwrapLvalue[T](lValue: LValue[T])(implicit lang: Language): T = lValue.resolve
}

/**
* Value that can be translated lazily (e.g. for definitions whose target language is not known a priori)
*/
class LValue[T](func: Language => T) {
def resolve(implicit lang: Language) = func(lang)
override def toString: String = s"LValue(${func(Language.English)})"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ package ru.makkarpov.scalingua
object Compat {
type Context = scala.reflect.macros.Context
def prettyPrint(c: Context)(e: c.Tree): String = c.universe.show(e)
def termName(c: Context)(s: String): c.TermName = c.universe.newTermName(c.fresh(s))
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ import scala.reflect.macros.whitebox
object Compat {
type Context = whitebox.Context
def prettyPrint(c: Context)(e: c.Tree): String = c.universe.showCode(e)
def termName(c: Context)(s: String): c.TermName = c.universe.TermName(c.freshName(s))
}
26 changes: 21 additions & 5 deletions scalingua/src/main/scala/ru/makkarpov/scalingua/I18n.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,40 @@ package ru.makkarpov.scalingua

import scala.language.experimental.macros

/**
* Dynamic translation functions and the basic set of macro-based interpolators and translation functions.
*/
object I18n {
implicit class StringInterpolator(val sc: StringContext) extends AnyVal {
def t(args: Any*)(implicit lang: Language, outputFormat: OutputFormat[String]): String =
macro Macros.interpolate[String]

def lt(args: Any*)(implicit outputFormat: OutputFormat[String]): LValue[String] =
macro Macros.lazyInterpolate[String]
}

def t(msg: String, args: (String, Any)*)(implicit lang: Language, outputFormat: OutputFormat[String]): String =
macro Macros.singular[String]

def lt(msg: String, args: (String, Any)*)(implicit outputFormat: OutputFormat[String]): LValue[String] =
macro Macros.lazySingular[String]

def tc(ctx: String, msg: String, args: (String, Any)*)(implicit lang: Language, outputFormat: OutputFormat[String]): String =
macro Macros.singularCtx[String]

def p(msg: String, msgPlural: String, n: Long, args: (String, Any)*)(implicit lang: Language, outputFormat: OutputFormat[String]): String =
def ltc(ctx: String, msg: String, args: (String, Any)*)(implicit outputFormat: OutputFormat[String]): LValue[String] =
macro Macros.lazySingularCtx[String]

def p(msg: String, msgPlural: String, n: Long, args: (String, Any)*)
(implicit lang: Language, outputFormat: OutputFormat[String]): String =
macro Macros.plural[String]

def pc(ctx: String, msg: String, msgPlural: String, n: Long, args: (String, Any)*)(implicit lang: Language, outputFormat: OutputFormat[String]): String =
def lp(msg: String, msgPlural: String, n: Long, args: (String, Any)*)
(implicit outputFormat: OutputFormat[String]): LValue[String] =
macro Macros.lazyPlural[String]

def pc(ctx: String, msg: String, msgPlural: String, n: Long, args: (String, Any)*)
(implicit lang: Language, outputFormat: OutputFormat[String]): String =
macro Macros.pluralCtx[String]

def lpc(ctx: String, msg: String, msgPlural: String, n: Long, args: (String, Any)*)
(implicit outputFormat: OutputFormat[String]): LValue[String] =
macro Macros.lazyPluralCtx[String]
}
112 changes: 0 additions & 112 deletions scalingua/src/main/scala/ru/makkarpov/scalingua/MacroUtils.scala

This file was deleted.

Loading

0 comments on commit 94e4a85

Please sign in to comment.