mirror of
https://github.com/nitu2003/T18n
synced 2024-11-21 15:18:47 +00:00
修改方法位置
1. 将操作方法移动到T18n中 2. 添加 README
This commit is contained in:
parent
1625253fca
commit
bf55d38a7b
82
README.md
Normal file
82
README.md
Normal 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"
|
||||
```
|
@ -17,22 +17,6 @@ public class I18n {
|
||||
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
|
||||
@ -40,8 +24,7 @@ public class I18n {
|
||||
* *******************************************************/
|
||||
|
||||
/** 自动从语言文件中提取翻译,空翻译时返回原键值 */
|
||||
@Deprecated
|
||||
public static String translate(String key) {
|
||||
protected static String translate(String key) {
|
||||
return reEncode(map.get(key), charset);
|
||||
}
|
||||
|
||||
@ -56,8 +39,7 @@ public class I18n {
|
||||
* 连续读取翻译(translate)。
|
||||
* @param keyRegular 翻译键值表达式,需要填入一个“%s”用于替换为行数。行数从0开始。例如“welcome.%s”,则程序会依次向列表中添加“welcome.0”,“welcome.1”...的翻译,直到获取到空值。
|
||||
*/
|
||||
@Deprecated
|
||||
public static List<String> translateList(String keyRegular) {
|
||||
protected static List<String> translateList(String keyRegular) {
|
||||
|
||||
List<String> vlist = new ArrayList<String>();
|
||||
|
||||
@ -111,15 +93,4 @@ public class I18n {
|
||||
}
|
||||
}
|
||||
|
||||
/* *******************************************************
|
||||
*
|
||||
* For Devlopers
|
||||
*
|
||||
* *******************************************************/
|
||||
|
||||
@Deprecated
|
||||
public static LanguageMap getLangMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package cn.glycol.t18n;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
public class LanguageMap extends HashMap<String, String> {
|
||||
|
||||
@ -12,35 +10,4 @@ public class LanguageMap extends HashMap<String, String> {
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package cn.glycol.t18n;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Objects;
|
||||
|
||||
public class T18n {
|
||||
public class T18n extends I18n {
|
||||
|
||||
/**
|
||||
* 设置本地化对照表
|
||||
|
@ -14,6 +14,7 @@ import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
|
||||
public class T18nUtils {
|
||||
@ -68,7 +69,10 @@ public class T18nUtils {
|
||||
|
||||
try {
|
||||
|
||||
String content = flattenList(map.getAllKeyValuePairsString());
|
||||
String content = "";
|
||||
for(Entry<String, String> entries : map.entrySet()) {
|
||||
content += entries.getKey() + "=" + entries.getValue() + "\n";
|
||||
}
|
||||
|
||||
if(!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
|
Loading…
Reference in New Issue
Block a user