translator.java

来自「piweurrrrq i o fhsadhfka fd dskajc zxkjc」· Java 代码 · 共 75 行

JAVA
75
字号
/* * Copyright (c) 2000 Lyrisoft Solutions, Inc. * Used by permission */package com.lyrisoft.util.i18n;import java.util.Properties;public class Translator {    private static Properties _messages;    public Translator() {        this(new Properties());    }    public Translator(Properties p) {        if (_messages == null) {            _messages = p;        }    }    public String getMessage(String key) {        String s = _messages.getProperty(key);        if (s == null) {            throw new RuntimeException("No such key: " + key);        }        return s;    }    public String getMessage(String key, String arg1) {        String[] args = { arg1 };        return getMessage(key, args);    }    public String getMessage(String key, String arg1, String arg2) {        String[] args = { arg1, arg2 };        return getMessage(key, args);    }    public String getMessage(String key, String arg1, String arg2, String arg3) {        String[] args = { arg1, arg2, arg3 };        return getMessage(key, args);    }    public String getMessage(String key, String[] args) {        String s = getMessage(key);        StringBuffer sb = new StringBuffer();        char[] raw = s.toCharArray();        for (int i=0; i < raw.length; i++) {            char c = raw[i];//            System.err.println(i + ": " + c);            if (c == '{') {                int j=i;                for (; j < raw.length; j++) {                    if (raw[j] == '}') {                        break;                    }                }                int idx = extractInt(raw, i, j);                sb.append(args[idx]);                i = j;            } else {                sb.append(c);            }        }        return sb.toString();    }    int extractInt(char[] buf, int start, int end) {        start++;//        System.err.println("extractInt: " + start + ", " + end);        return Integer.parseInt(new String(buf, start, end-start));    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?