1
0
mirror of https://github.com/nitu2003/T18n synced 2024-11-21 23:28:49 +00:00

修改方法位置

1. 将操作方法移动到T18n中
2. 添加 README
This commit is contained in:
Taskeren 2019-10-01 19:17:50 +08:00
parent 1625253fca
commit bf55d38a7b
5 changed files with 90 additions and 66 deletions

82
README.md Normal file
View File

@ -0,0 +1,82 @@
<h1 align="center">
Taskeren's Internationalization
</h1>
<h4 align="center">
A lightweight, powerful I18n tool for Java.
</h4>
## Get T18n
You can get this project in Jitpack with Maven, Gradle and so on.
### Maven
1. Import the Jitpack source.
```
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
```
2. Add the denpendency.
```
<dependency>
<groupId>com.github.nitu2003</groupId>
<artifactId>T18n</artifactId>
<version>1.5</version>
</dependency>
```
### Gradle
1. Import the Jitpack source.
```
repositories {
...
maven { url 'https://jitpack.io' }
}
```
2. Add the dependency.
```
dependencies {
implementation 'com.github.nitu2003:T18n:1.5'
}
```
### For else
[![](https://jitpack.io/v/nitu2003/T18n.svg)](https://jitpack.io/#nitu2003/T18n)
## Dev with T18n
1. Get the Language Map.
```java
// from file
LanguageMapBuilder.fromFile(new File("res/i18n/en_us.lang"));
// from jar resource
LanguageMapBuilder.fromJarResource("res/i18n/en_us.lang");
// from online content (Deprecated)
LanguageMapBuilder.fromURL("https://example.com/i18n/en.lang");
```
2. Push it to the I18n.
```java
// reset the I18n and push it
T18n.set(theMap);
// add the new to the old
T18n.add(theMap);
```
3. Use it in the project
_en.lang_
```
HelloToUser=Hello %s.
```
_app.java_
```java
System.out.println(I18n.format("HelloToUser", "Taskeren")); // It should be "Hello Taskeren"
```

View File

@ -17,22 +17,6 @@ public class I18n {
charset = Charset.forName(System.getProperty("file.encoding")); charset = Charset.forName(System.getProperty("file.encoding"));
} }
/* *******************************************************
*
* Settings
*
* *******************************************************/
@Deprecated
public static void setLanguageMap(LanguageMap map) {
T18n.set(map);
}
@Deprecated
public static void setEncoding(String charset) {
I18n.charset = Charset.forName(charset);
}
/* ******************************************************* /* *******************************************************
* *
* Localization functions * Localization functions
@ -40,8 +24,7 @@ public class I18n {
* *******************************************************/ * *******************************************************/
/** 自动从语言文件中提取翻译,空翻译时返回原键值 */ /** 自动从语言文件中提取翻译,空翻译时返回原键值 */
@Deprecated protected static String translate(String key) {
public static String translate(String key) {
return reEncode(map.get(key), charset); return reEncode(map.get(key), charset);
} }
@ -56,8 +39,7 @@ public class I18n {
* 连续读取翻译translate * 连续读取翻译translate
* @param keyRegular 翻译键值表达式需要填入一个%s用于替换为行数行数从0开始例如welcome.%s则程序会依次向列表中添加welcome.0welcome.1...的翻译直到获取到空值 * @param keyRegular 翻译键值表达式需要填入一个%s用于替换为行数行数从0开始例如welcome.%s则程序会依次向列表中添加welcome.0welcome.1...的翻译直到获取到空值
*/ */
@Deprecated protected static List<String> translateList(String keyRegular) {
public static List<String> translateList(String keyRegular) {
List<String> vlist = new ArrayList<String>(); List<String> vlist = new ArrayList<String>();
@ -111,15 +93,4 @@ public class I18n {
} }
} }
/* *******************************************************
*
* For Devlopers
*
* *******************************************************/
@Deprecated
public static LanguageMap getLangMap() {
return map;
}
} }

View File

@ -1,8 +1,6 @@
package cn.glycol.t18n; package cn.glycol.t18n;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set;
public class LanguageMap extends HashMap<String, String> { public class LanguageMap extends HashMap<String, String> {
@ -12,35 +10,4 @@ public class LanguageMap extends HashMap<String, String> {
return super.getOrDefault(key, key.toString()); return super.getOrDefault(key, key.toString());
} }
/** 返回所有键值对
* @see #entrySet()
*/
public Set<Entry<String, String>> getAllKeyValuePairs() {
return this.entrySet();
}
/** 获取所有键值对文字 */
public ArrayList<String> getAllKeyValuePairsString() {
ArrayList<String> list = new ArrayList<String>();
for(Entry<String, String> set : this.entrySet()) {
String key = set.getKey();
String val = set.getValue();
String line;
if(key != null && !key.equals("")) {
line = key + "=" + val;
list.add(line);
}
}
return list;
}
/** 输出键值对 */
public static void printKVPair(LanguageMap langMap) {
ArrayList<String> lines = langMap.getAllKeyValuePairsString();
lines.forEach(s->System.out.println(s));
}
} }

View File

@ -3,7 +3,7 @@ package cn.glycol.t18n;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Objects; import java.util.Objects;
public class T18n { public class T18n extends I18n {
/** /**
* 设置本地化对照表 * 设置本地化对照表

View File

@ -14,6 +14,7 @@ import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import java.util.Objects; import java.util.Objects;
public class T18nUtils { public class T18nUtils {
@ -68,7 +69,10 @@ public class T18nUtils {
try { try {
String content = flattenList(map.getAllKeyValuePairsString()); String content = "";
for(Entry<String, String> entries : map.entrySet()) {
content += entries.getKey() + "=" + entries.getValue() + "\n";
}
if(!file.exists()) { if(!file.exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();