A most lightweight library for translating you application to the local languages.
<?xml version="1.0" encoding="UTF-8" ?>
<translator>
<message>
<from>This is the original message!</from>
<to>
<!-- the translation for chinese -->
<zh>这是原始文本!</zh>
<!-- the translation for traditional chinese(Hong Kong) -->
<zh_HK>這是原始文本!</zh_HK>
</to>
</message>
</translator>- Add
Stranslatorto your project
libraryDependencies += "com.lingcreative" %% "stranslator" % "1.1.0"
- Code sample
import translator._
val translator = Translator()
val locale = new Locale("zh", "CN")
implicit val context = SimpleTranslatorContext(translator, Seq(locale))
val welcome = ${"Hello, Sauntor! Welcome to China!"}If you create a translation file which is located in l10n/translator.xml(within the class path) with the flowing content:
<?xml version="1.0" encoding="UTF-8" ?>
<translator>
<message>
<from>Hello, Sauntor! Welcome to China!</from>
<to>
<zh_CN>适然,你好!欢迎来到中国!</zh_CN>
</to>
</message>
</translator>The welcome would be:
适然,你好!欢迎来到中国!
- the line feed(
\n) after the beginning and before the ending tag offrom,locales (intotag, i.e.localestands for<zh_CN>and</zh_CN>), will be ignored. - you can break one line into multiple lines by putting an
\to the end of the line(and the leading space is ignored and the space after\will be preserved). For example:
<translator>
<message>
<from>
Hello, \
Jack! I'm waiting \
for you!
</from>
<to>
<zh>捷克,你来了。\
我已经等你好久了!
</zh>
</to>
</message>
</translator>The code above is equal to:
<translator>
<message>
<from>Hello, Jack! I'm waiting for you!</from>
<to><zh>捷克,你来了。我已经等你好久了!</zh></to>
</message>
</translator>- The default location to load translations from is
l10n/translator.xml, i.e.
val translator = Translator()Is equal to:
val translator = Translator("cp://l10n/translator.xml")
// Or to this:
//val translator = Translator("l10n/translator.xml")- You can
includeanother xml for translations by<include>tag :
<translator>
<include>http://example.com:9000/some/app/l10n/translations.xml</include>
</translator>The
<include>tag does not support relative path, i.e. you can't include a resource like../some/other/module.xml.
It's the core API for translating. You can initialize it with an URL. An class path resource will start with "cp://" (no prefix is identical to it too), or an external resource on a Web Server with the corresponding uri pattern(i.e. http://example.com/l10n/demo-app.xml).