-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Play integration module, but without tests yet.
- Loading branch information
makkarpov
committed
Apr 20, 2016
1 parent
c9da1f0
commit 8e2f059
Showing
7 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
play/src/main/scala/ru/makkarpov/scalingua/play/I18n.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/****************************************************************************** | ||
* 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.play | ||
|
||
import play.api.http.HeaderNames | ||
import play.api.mvc.RequestHeader | ||
import play.twirl.api.Html | ||
import ru.makkarpov.scalingua | ||
import ru.makkarpov.scalingua._ | ||
|
||
import scala.language.experimental.macros | ||
import scala.language.implicitConversions | ||
|
||
trait I18n extends scalingua.I18n { | ||
type LHtml = LValue[Html] | ||
|
||
implicit val htmlOutputFormat = new OutputFormat[Html] { | ||
override def convert(s: String): Html = Html(s) | ||
|
||
override def escape(s: String): String = { | ||
val ret = new StringBuilder | ||
var i = 0 | ||
ret.sizeHint(s.length) | ||
while (i < s.length) { | ||
s.charAt(i) match { | ||
case '<' => ret.append("<") | ||
case '>' => ret.append(">") | ||
case '"' => ret.append(""") | ||
case '\'' => ret.append("'") | ||
case '&' => ret.append("&") | ||
case c => ret += c | ||
} | ||
i += 1 | ||
} | ||
ret.result() | ||
} | ||
} | ||
|
||
// The conversion from `RequestHeader` to `LanguageId` will imply information loss: | ||
// Suppose browser sent the following language precedence list `xx_YY`, `zz_WW`, `en_US`. | ||
// This conversion will have no information about available languages, so it will result in | ||
// `LanguageId("xx", "YY")` even if this language is absent. By supplying implicit `Messages` | ||
// too, we will have enough information to skip unsupported languages and return supported | ||
// one with respect to priority. | ||
|
||
implicit def requestHeader2Language(implicit rq: RequestHeader, msg: Messages): Language = { | ||
val langs = (for { | ||
value0 <- rq.headers(HeaderNames.ACCEPT_LANGUAGE).split(',') | ||
value = value0.trim | ||
} yield { | ||
RequestHeader.qPattern.findFirstMatchIn(value) match { | ||
case Some(m) => (m.group(1).toDouble, m.before.toString) | ||
case None => (1.0, value) // “The default value is q=1.” | ||
} | ||
}).sortBy(_._1).iterator | ||
|
||
while (langs.hasNext) { | ||
val (_, id) = langs.next() | ||
val lng = LanguageId.get(id) | ||
|
||
if (lng.isDefined && msg.contains(lng.get)) | ||
return msg(lng.get) | ||
} | ||
|
||
Language.English | ||
} | ||
|
||
implicit def stringContext2Interpolator1(sc: StringContext): I18n.StringInterpolator = | ||
new I18n.StringInterpolator(sc) | ||
|
||
def th(msg: String, args: (String, Any)*)(implicit lang: Language, outputFormat: OutputFormat[Html]): Html = | ||
macro Macros.singular[Html] | ||
|
||
def lth(msg: String, args: (String, Any)*)(implicit outputFormat: OutputFormat[Html]): LHtml = | ||
macro Macros.lazySingular[Html] | ||
|
||
def tch(ctx: String, msg: String, args: (String, Any)*)(implicit lang: Language, outputFormat: OutputFormat[Html]): Html = | ||
macro Macros.singularCtx[Html] | ||
|
||
def ltch(ctx: String, msg: String, args: (String, Any)*)(implicit outputFormat: OutputFormat[Html]): LHtml = | ||
macro Macros.lazySingularCtx[Html] | ||
|
||
def p(msg: String, msgPlural: String, n: Long, args: (String, Any)*) | ||
(implicit lang: Language, outputFormat: OutputFormat[Html]): Html = | ||
macro Macros.plural[Html] | ||
|
||
def lp(msg: String, msgPlural: String, n: Long, args: (String, Any)*) | ||
(implicit outputFormat: OutputFormat[Html]): LHtml = | ||
macro Macros.lazyPlural[Html] | ||
|
||
def pc(ctx: String, msg: String, msgPlural: String, n: Long, args: (String, Any)*) | ||
(implicit lang: Language, outputFormat: OutputFormat[Html]): Html = | ||
macro Macros.pluralCtx[Html] | ||
|
||
def lpc(ctx: String, msg: String, msgPlural: String, n: Long, args: (String, Any)*) | ||
(implicit outputFormat: OutputFormat[Html]): LHtml = | ||
macro Macros.lazyPluralCtx[Html] | ||
} | ||
|
||
object I18n extends I18n { | ||
class StringInterpolator(val sc: StringContext) extends AnyVal { | ||
def h(args: Any*)(implicit lang: Language, outputFormat: OutputFormat[Html]): Html = | ||
macro Macros.interpolate[Html] | ||
|
||
def lh(args: Any*)(implicit outputFormat: OutputFormat[Html]): LHtml = | ||
macro Macros.lazyInterpolate[Html] | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
play/src/main/scala/ru/makkarpov/scalingua/play/MessagesProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/****************************************************************************** | ||
* 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.play | ||
|
||
import javax.inject.{Inject, Provider} | ||
|
||
import play.api.{Application, Environment} | ||
import ru.makkarpov.scalingua.Messages | ||
|
||
class MessagesProvider @Inject()(conf: ScalinguaConfig, env: Environment) extends Provider[Messages] { | ||
override def get(): Messages = Messages.compiledContext(env.classLoader, conf.localePackage) | ||
} |
23 changes: 23 additions & 0 deletions
23
play/src/main/scala/ru/makkarpov/scalingua/play/ScalinguaConfig.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/****************************************************************************** | ||
* 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.play | ||
|
||
import play.api.Configuration | ||
|
||
class ScalinguaConfig(cfg: Configuration) { | ||
val localePackage: String = cfg.getString("scalingua.package").getOrElse("locales") | ||
} |
25 changes: 25 additions & 0 deletions
25
play/src/main/scala/ru/makkarpov/scalingua/play/ScalinguaConfigProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/****************************************************************************** | ||
* 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.play | ||
|
||
import javax.inject.{Inject, Provider} | ||
|
||
import play.api.Configuration | ||
|
||
class ScalinguaConfigProvider @Inject() (cfg: Configuration) extends Provider[ScalinguaConfig] { | ||
override def get(): ScalinguaConfig = new ScalinguaConfig(cfg) | ||
} |
37 changes: 37 additions & 0 deletions
37
play/src/main/scala/ru/makkarpov/scalingua/play/ScalinguaPlugin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/****************************************************************************** | ||
* 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.play | ||
|
||
import javax.inject.Inject | ||
|
||
import play.api.{Application, BuiltInComponentsFromContext, Configuration, Environment} | ||
import play.api.inject.{Binding, Module} | ||
import ru.makkarpov.scalingua.Messages | ||
|
||
trait ScalinguaComponents { | ||
def configuration: Configuration | ||
def environment: Environment | ||
|
||
lazy val messages = new MessagesProvider(new ScalinguaConfigProvider(configuration).get(), environment).get() | ||
} | ||
|
||
class ScalinguaModule extends Module { | ||
override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = Seq( | ||
bind[ScalinguaConfig].toProvider[ScalinguaConfigProvider], | ||
bind[Messages].toProvider[MessagesProvider] | ||
) | ||
} |