UserScript Translation Engine

Translate strings by given translations

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

نووسەر
Jixun.Moe
وەشان
1.0
Created
2014-12-20
Updated
2014-12-20
مۆڵەت
نییە

方便使用的翻译函数库。

代码演示

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: [
        [ ... ]
    ]
}

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