Giter Site home page Giter Site logo

stranslator's Introduction

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! 🍵

stranslator's People

Contributors

sauntor avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.