stranslator

A most lightweight library for translating you application to the local languages.

License

License

GroupId

GroupId

com.lingcreative
ArtifactId

ArtifactId

stranslator_2.11
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

stranslator
A most lightweight library for translating you application to the local languages.
Project URL

Project URL

https://github.com/sauntor/stranslator
Project Organization

Project Organization

LingCreative Studio
Source Code Management

Source Code Management

https://github.com/sauntor/stranslator

Download stranslator_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.lingcreative/stranslator_2.11/ -->
<dependency>
    <groupId>com.lingcreative</groupId>
    <artifactId>stranslator_2.11</artifactId>
    <version>1.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.lingcreative/stranslator_2.11/
implementation 'com.lingcreative:stranslator_2.11:1.2.0'
// https://jarcasting.com/artifacts/com.lingcreative/stranslator_2.11/
implementation ("com.lingcreative:stranslator_2.11:1.2.0")
'com.lingcreative:stranslator_2.11:jar:1.2.0'
<dependency org="com.lingcreative" name="stranslator_2.11" rev="1.2.0">
  <artifact name="stranslator_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.lingcreative', module='stranslator_2.11', version='1.2.0')
)
libraryDependencies += "com.lingcreative" % "stranslator_2.11" % "1.2.0"
[com.lingcreative/stranslator_2.11 "1.2.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.8
org.scala-lang.modules : scala-xml_2.11 jar 1.0.6

test (1)

Group / Artifact Type Version
org.specs2 : specs2-core_2.11 jar 3.8.8

Project Modules

There are no modules declared in this project.

Stranslator

A most lightweight library for translating you application to the local languages.

The XML format for translation file

<?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>

Usage

  1. Add Stranslator to your project
libraryDependencies += "com.lingcreative" %% "stranslator" % "1.1.0"
  1. 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: 适然,你好!欢迎来到中国!

Notice
  1. the line feed(\n) after the beginning and before the ending tag of from, locales (in to tag, i.e. locale stands for <zh_CN> and </zh_CN>), will be ignored.
  2. 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>
  1. 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")
  1. You can include another 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.

About the stranslator.Translator

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).

Enjoy it! 🍵

Versions

Version
1.2.0
1.1.0
1.0.1