示例——Localization.ts

作者:追风剑情 发布于:2018-11-16 15:56 分类:TypeScript

示例

Localization.ts

/**
 * 本地化
 */
class Localization {

	private static _dic: {[key: string]: string} = {};

	private constructor() {
	}

	public static loadString(str: string)
	{
		if (Localization.isNullOrEmpty(str)) {
			return "";
		}
		var lineArr: Array<string> = str.split('\n');
		var line: string;
		var kv: Array<string>;
		var len: number = lineArr.length;
		for (var i: number = 0; i < len; i++) {
		    line = lineArr[i];
		    if (Localization.isNullOrEmpty(line)) {
				continue;
			}
			kv = line.split('\t');
			if (kv.length < 2) {
				continue;
		    }
		    Localization._dic[kv[0]] = kv[1];
		}
	}

	/**
	 * 获取字符串
	 */
	public static get(key: string, ...rest): string
	{
		//检查key
		if (Localization.isNullOrEmpty(key)) {
			return "";
		}
		var str: string = Localization._dic[key] as string
		if (Localization.isNullOrEmpty(key)) {
			return "";
		}
		//检查参数
		var p: string;
		var len: number = rest.length;
		var args: Array<string> = null;
		if (len == 1 && (rest[0] instanceof Array)) {
                     args = rest[0];
		     if (args != null) {
			len = args.length;
		     }else{
			len = 0
		     }
                }
                else {
                     args = rest;
                }
		//替换所有参数
		for (var i: number = 0; i < len; i++) {
			p = args[i];
			if (p == null) {
				p = "";
			}
			str = str.replace(new RegExp("\\{" + i + "\\}", "g"), p);
		}
		return str;
	}

	private static isNullOrEmpty(str: string): boolean
        {
                return (str == null || str.trim() == "");
        }
}


ts_test.ts

/// <reference path="Localization.ts" />
let content: string = `
key1	{0}只{1}
key2	{0}条{1}
key3	vvvvvvvvvv3
`;
Localization.loadString(content);
console.log("key1="+Localization.get("key1", 1, "鸡"));
console.log("key2="+Localization.get("key2", [2, "鱼"]));
console.log("key3=" + Localization.get("key3"));


输出后的test.js

/**
 * 本地化
 */
var Localization = /** @class */ (function () {
    function Localization() {
    }
    Localization.loadString = function (str) {
        if (Localization.isNullOrEmpty(str)) {
            return "";
        }
        var lineArr = str.split('\n');
        var line;
        var kv;
        var len = lineArr.length;
        for (var i = 0; i < len; i++) {
            line = lineArr[i];
            if (Localization.isNullOrEmpty(line)) {
                continue;
            }
            kv = line.split('\t');
            if (kv.length < 2) {
                continue;
            }
            Localization._dic[kv[0]] = kv[1];
        }
    };
    /**
     * 获取字符串
     */
    Localization.get = function (key) {
        var rest = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            rest[_i - 1] = arguments[_i];
        }
        //检查key
        if (Localization.isNullOrEmpty(key)) {
            return "";
        }
        var str = Localization._dic[key];
        if (Localization.isNullOrEmpty(key)) {
            return "";
        }
        //检查参数
        var p;
        var len = rest.length;
        var args = null;
        if (len == 1 && (rest[0] instanceof Array)) {
            args = rest[0];
            if (args != null) {
                len = args.length;
            }
            else {
                len = 0;
            }
        }
        else {
            args = rest;
        }
        //替换所有参数
        for (var i = 0; i < len; i++) {
            p = args[i];
            if (p == null) {
                p = "";
            }
            str = str.replace(new RegExp("\\{" + i + "\\}", "g"), p);
        }
        return str;
    };
    Localization.isNullOrEmpty = function (str) {
        return (str == null || str.trim() == "");
    };
    Localization._dic = {};
    return Localization;
}());
/// <reference path="Localization.ts" />
var content = "\nkey1\t{0}\u53EA{1}\nkey2\t{0}\u6761{1}\nkey3\tvvvvvvvvvv3\n";
Localization.loadString(content);
console.log("key1=" + Localization.get("key1", 1, "鸡"));
console.log("key2=" + Localization.get("key2", [2, "鱼"]));
console.log("key3=" + Localization.get("key3"));


运行测试

1111.png

标签: TypeScript

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号