i18n
Original i18n Support
Although Skinny Micro has i18n feature but this is our original implementation, Skinny Framework also has i18n feature.
The reason to do so is that we need to seamlessly support validator’s error messages and other things.
Default Locale
package controller
import skinny._
import skinny.filter._
trait ApplicationController extends SkinnyController with ErrorPageFilter {
override def defaultLocale = Some(new java.util.Locale("ja"))
}
Current Locale Value in Sessions
You can store users’ locale in Servlet sessions or Skinny sessions.
object SettingsController extends ApplicationController {
def updateLocale = {
val locale = params.getAs[String]("locale").orNull[String]
setCurrentLocale(locale) // can get via currentLocale: Option[Locale]
redirect("somewhere")
}
}
See the example project here.
https://github.com/skinny-framework/skinny-framework/tree/master/example
messages(_{locale}).conf
By default, defaultLocale is None. In this case, src/main/resources/messages.conf
will be used.
error {
required="{0} is required"
length="{0} length must be {1}"
.....
}
year="Year"
month="Month"
day="Day"
hour="Hour"
minute="Minute"
second="Second"
backToList="Back to list"
submit="Submit"
cancel="Cancel"
detail="Detail"
new="New"
edit="Edit"
delete="Delete"
When locale is set, src/main/resources/messages_{locale}.conf
instead of messages.conf
.
The following example is messages_ja.conf
for Japanese.
error {
required="{0} を入力してください"
length="{0} は {1} 文字で入力してください"
.....
}
year="年"
month="月"
day="日"
hour="時"
minute="分"
second="秒"
backToList="一覧へ戻る"
submit="送信"
cancel="キャンセル"
detail="詳細"
new="追加"
edit="変更"
delete="削除"
i18n in View Templates
Simply use skinny.I18n
instance. Current skinny.I18n
instance is always available as i18n
in view templates.
<%@val i18n: skinny.I18n %>
${i18n.get("submit")}
${i18n.get("project.name")}
The above example expects the following src/main/resources/messages_{locale}.conf
.
submit="Submit"
project {
name="Project Name"
}
Integration with Skinny Validator
Integration with Skinny Validator is much simple.
The error message which is defined with error
prefix and validator’s name
will be used.
For instance, this validator’s name is notNull
,
import skinny.validator._
object notNull extends ValidationRule {
def name = "notNull"
def isValid(v: Any) = v != null
}
So validation error message should be defined in messages.conf
like this:
error {
notNull="{0} must not be null"
}
That’s all!