UserScript Translation Engine

Translate strings by given translations

As of 2014-12-20. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/7081/28938/UserScript%20Translation%20Engine.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

Author
Jixun.Moe
Version
1.0
Created
2014-12-20
Updated
2014-12-20
Size
4.65 KB
License
N/A

方便使用的翻译函数库。

代码演示

var Translator = new Translation (Translation.getLang(lang));

addEventListener ('DOMContentLoaded', function () {
    Translator.run ();
}, false);

其中,lang 为一个包含有所有可选语言的对象,如 { 'zh-CN': { ... }, zh: { ... }, ... }

每个语言的格式如下 (中间可随意消失,如 node.tag.SPAN.text 节点写到 node.tag.text 也是合法的):

node: {
    str: {
        '翻译前': '翻译后',

        regex: [
            [ /abc(\d)/, '$1' ]
        ]
    },

    tag: {
        SPAN: {
            '翻译前': '翻译后',

            regex: [ ... ]
        },
    }

    regex: [ ... ]
},

attr: {
    str: {
        '翻译前': '翻译后',

        regex: [ ... ]
    },

    attr: {
        tag: {
            TD: {
                title: {
                    regex: [ ... ]
                }
            }
        }
    }

    regex: [
        [ ... ]
    ]
}

所以说如果语言文件没有归类好就会很乱啦…