相关疑难解决方法(0)

有没有办法控制默认使用哪个隐式转换?

假设我有这个:

class String2(val x:String) {
    def *(times:Int) : String = {
        val builder = new StringBuilder()
        for( i <- 0 until times) {
            builder.append(x)
        }
        builder.toString()
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我添加这个隐含:

implicit def gimmeString2(y:String) = new String2(y)
Run Code Online (Sandbox Code Playgroud)

我会得到一个编译错误,因为stringWrapper也添加了这个隐式.有没有办法说编译器"忽略其他含义,使用它",这样我就不必实例化一个String2对象并对其进行处理?

我承认示例代码可能不是最合适的(对于这个问题),但我认为它会做.

scala implicit

20
推荐指数
1
解决办法
4644
查看次数

play framework 2.0 - 国际化 - 如何翻译消息

第一个问题:如何检索控制器中文本的翻译?

第二个问题:如何检索模板中文本的翻译?

api说有一个.get方法可以翻译一条消息:

http://www.playframework.org/documentation/api/2.0/java/play/i18n/Messages.html

但是我的应用程序无法识别此方法.在eclipse中打开Message.class显示其中有一个.apply方法,用Scala和Java编写!

object Messages {

  /**
   * Translates a message.
   *
   * Uses `java.text.MessageFormat` internally to format the message.
   *
   * @param key the message key
   * @param args the message arguments
   * @return the formatted message or a default rendering if the key wasn’t defined
   */
  def apply(key: String, args: Any*)(implicit lang: Lang): String = {
    Play.maybeApplication.flatMap { app =>
      app.plugin[MessagesPlugin].map(_.api.translate(key, args)).getOrElse(throw new Exception("this plugin was not registered or disabled"))
    }.getOrElse(noMatch(key, args))
  }
Run Code Online (Sandbox Code Playgroud)

现在eclipse告诉我,我可以像这样调用这个方法:

> String …
Run Code Online (Sandbox Code Playgroud)

internationalization playframework-2.0

6
推荐指数
1
解决办法
8245
查看次数