diff --git a/.classpath b/.classpath deleted file mode 100644 index 6b41508..0000000 --- a/.classpath +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/.gitignore b/.gitignore index c75f3c2..718aff4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,9 @@ # Ignore Gradle build output directory build + +# eclipse +bin/ +.settings/ +.classpath +.project \ No newline at end of file diff --git a/.project b/.project deleted file mode 100644 index f3f62de..0000000 --- a/.project +++ /dev/null @@ -1,23 +0,0 @@ - - - T18n - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.buildship.core.gradleprojectbuilder - - - - - - org.eclipse.jdt.core.javanature - org.eclipse.buildship.core.gradleprojectnature - - diff --git a/.settings/org.eclipse.buildship.core.prefs b/.settings/org.eclipse.buildship.core.prefs deleted file mode 100644 index 408b507..0000000 --- a/.settings/org.eclipse.buildship.core.prefs +++ /dev/null @@ -1,2 +0,0 @@ -connection.project.dir= -eclipse.preferences.version=1 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index b141b85..0000000 --- a/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,13 +0,0 @@ -# -#Tue Jun 25 20:09:41 CST 2019 -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.compliance=1.8 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.source=1.8 -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error diff --git a/bin/.gitignore b/bin/.gitignore deleted file mode 100644 index 7eed456..0000000 --- a/bin/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/main/ -/test/ diff --git a/src/main/java/cn/glycol/t18n/I18n.java b/src/main/java/cn/glycol/t18n/I18n.java index d4d0df7..4cdd842 100644 --- a/src/main/java/cn/glycol/t18n/I18n.java +++ b/src/main/java/cn/glycol/t18n/I18n.java @@ -4,15 +4,16 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; -@SuppressWarnings("unused") + public class I18n { - private static LanguageMap map; - private static Charset charset; + protected static LanguageMap map; + protected static Charset charset; - public static final int LOOP_MAX_COUNT = 1000; + protected static final int LOOP_MAX_COUNT = 32767; static { + map = new LanguageMap(); charset = Charset.forName(System.getProperty("file.encoding")); } @@ -22,10 +23,12 @@ public class I18n { * * *******************************************************/ + @Deprecated public static void setLanguageMap(LanguageMap map) { - I18n.map = map; + T18n.set(map); } + @Deprecated public static void setEncoding(String charset) { I18n.charset = Charset.forName(charset); } @@ -37,8 +40,9 @@ public class I18n { * *******************************************************/ /** 自动从语言文件中提取翻译,空翻译时返回原键值 */ + @Deprecated public static String translate(String key) { - return reEncode(getLanguageMapSafe().get(key), charset); + return reEncode(map.get(key), charset); } /** 自动翻译(translate)后再执行格式化(format) */ @@ -52,6 +56,7 @@ public class I18n { * 连续读取翻译(translate)。 * @param keyRegular 翻译键值表达式,需要填入一个“%s”用于替换为行数。行数从0开始。例如“welcome.%s”,则程序会依次向列表中添加“welcome.0”,“welcome.1”...的翻译,直到获取到空值。 */ + @Deprecated public static List translateList(String keyRegular) { List vlist = new ArrayList(); @@ -78,13 +83,13 @@ public class I18n { * @see #translateList(String) */ public static String formatList(String keyRegular, Object...format) { - + return tryFormat(T18nUtils.flattenList(translateList(keyRegular)), format); } public static boolean hasKey(String key) { - return getLanguageMapSafe().containsKey(key); + return map.containsKey(key); } /* ******************************************************* @@ -93,13 +98,6 @@ public class I18n { * * *******************************************************/ - /** @see #hasKey(String) */ - private static boolean canTranslate(String key) { - return hasKey(key); - } - - private static final Charset DEFAULT_CHARSET = Charset.forName("unicode"); - private static String reEncode(String bef, Charset charset) { byte[] bytes = bef.getBytes(charset); return new String(bytes, charset); @@ -113,16 +111,13 @@ public class I18n { } } - private static LanguageMap getLanguageMapSafe() { - return map == null ? new LanguageMap() : map; - } - /* ******************************************************* * * For Devlopers * * *******************************************************/ + @Deprecated public static LanguageMap getLangMap() { return map; } diff --git a/src/main/java/cn/glycol/t18n/T18n.java b/src/main/java/cn/glycol/t18n/T18n.java new file mode 100644 index 0000000..319d4c1 --- /dev/null +++ b/src/main/java/cn/glycol/t18n/T18n.java @@ -0,0 +1,42 @@ +package cn.glycol.t18n; + +import java.nio.charset.Charset; +import java.util.Objects; + +public class T18n { + + /** + * 设置本地化对照表 + * @param map 本地化对照表 + */ + public static void set(LanguageMap map) { + Objects.requireNonNull(map, "map cannot be null"); + I18n.map = map; + } + + /** + * 添加额外的本地化对照表 + * @param map 本地化对照表 + */ + public static void add(LanguageMap map) { + Objects.requireNonNull(map, "map cannot be null"); + I18n.map.putAll(map); + } + + /** + * 设置输出的编码 + * @param charset 编码 + */ + public static void charset(Charset charset) { + Objects.requireNonNull(charset, "charset cannot be null"); + I18n.charset = charset; + } + + /** + * 获取本地化对照表 + */ + public static LanguageMap map() { + return I18n.map; + } + +}