Compare commits

...

7 Commits
1.4 ... master

Author SHA1 Message Date
Taskeren bf55d38a7b 修改方法位置
1. 将操作方法移动到T18n中
2. 添加 README
2019-10-01 19:17:50 +08:00
Taskeren 1625253fca 给予Gradlew执行权限 2019-08-30 12:19:25 +08:00
Taskeren b9d5915cda 调整部分方法
将设置和开发相关方法移动到T18n类中
2019-08-30 12:17:48 +08:00
倪钍 7e91fa3d4c
添加CI支持 2019-08-30 11:35:35 +08:00
Taskeren d87eb146d4 [+] 很多注解 2019-07-12 13:55:18 +08:00
Taskeren 240bc0cd0b [+] 修复LanguageMapBuilder#fromJarResource编码问题 2019-07-11 14:26:17 +08:00
Taskeren 355f64b475 [-] Clean up 2019-07-11 02:48:00 +08:00
15 changed files with 213 additions and 142 deletions

View File

@ -1,18 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin/main" path="src/main/java">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/main" path="src/main/resources">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>

17
.github/workflows/gradle.yml vendored Normal file
View File

@ -0,0 +1,17 @@
name: Java CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Gradle
run: ./gradlew build

6
.gitignore vendored
View File

@ -3,3 +3,9 @@
# Ignore Gradle build output directory
build
# eclipse
bin/
.settings/
.classpath
.project

View File

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>T18n</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>

View File

@ -1,2 +0,0 @@
connection.project.dir=
eclipse.preferences.version=1

View File

@ -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

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"
```

2
bin/.gitignore vendored
View File

@ -1,2 +0,0 @@
/main/
/test/

0
gradlew vendored Normal file → Executable file
View File

View File

@ -1,38 +1,22 @@
package cn.glycol.t18n;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
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"));
}
/* *******************************************************
*
* Settings
*
* *******************************************************/
public static void setLanguageMap(LanguageMap map) {
I18n.map = map;
}
public static void setEncoding(String charset) {
I18n.charset = Charset.forName(charset);
}
/* *******************************************************
*
* Localization functions
@ -40,8 +24,8 @@ public class I18n {
* *******************************************************/
/** 自动从语言文件中提取翻译,空翻译时返回原键值 */
public static String translate(String key) {
return reEncode(getLanguageMapSafe().get(key), charset);
protected static String translate(String key) {
return reEncode(map.get(key), charset);
}
/** 自动翻译translate后再执行格式化format */
@ -55,7 +39,7 @@ public class I18n {
* translate
* @param keyRegular %s0welcome.%swelcome.0welcome.1...
*/
public static List<String> translateList(String keyRegular) {
protected static List<String> translateList(String keyRegular) {
List<String> vlist = new ArrayList<String>();
@ -81,13 +65,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);
}
/* *******************************************************
@ -96,13 +80,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);
@ -116,18 +93,4 @@ public class I18n {
}
}
private static LanguageMap getLanguageMapSafe() {
return map == null ? new LanguageMap() : map;
}
/* *******************************************************
*
* For Devlopers
*
* *******************************************************/
public static LanguageMap getLangMap() {
return map;
}
}

View File

@ -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));
}
}

View File

@ -5,33 +5,57 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class LanguageMapBuilder {
/**
*
* @param langFile
* @return
*/
public static LanguageMap fromFile(File langFile) {
return fromFile(langFile, LanguageMapConfiguration.DEFAULT);
}
/**
*
* @param langFile
* @param config
* @return
*/
public static LanguageMap fromFile(File langFile, LanguageMapConfiguration config) {
List<String> lines = T18nUtils.getLocalContent(langFile);
return processFinal(config, lines);
}
/**
* UTF8 Jar
* @param path
* @return
*/
public static LanguageMap fromJarResource(String path) {
return fromJarResource(path, LanguageMapConfiguration.DEFAULT);
return fromJarResource(path, "UTF8", LanguageMapConfiguration.DEFAULT);
}
public static LanguageMap fromJarResource(String path, LanguageMapConfiguration config) {
/**
* Jar
* @param path
* @param encode <code>UTF-8</code><code>GBK</code><code>Unicode</code>
* @param config
* @return
*/
public static LanguageMap fromJarResource(String path, String encode, LanguageMapConfiguration config) {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
if (is == null) {
return new LanguageMap();
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName(encode)));
String cache;
List<String> strs = new ArrayList<>();
@ -52,10 +76,21 @@ public class LanguageMapBuilder {
}
/**
* 线
* @param url
* @return
*/
public static LanguageMap fromURL(String url) {
return fromURL(url, LanguageMapConfiguration.DEFAULT);
}
/**
* 线
* @param url
* @param config
* @return
*/
public static LanguageMap fromURL(String url, LanguageMapConfiguration config) {
return processFinal(config, T18nUtils.getOnlineContent(url));
}

View File

@ -2,7 +2,20 @@ package cn.glycol.t18n;
public interface LanguageMapConfiguration {
/**
* <br>
*
* @param s
* @return
*/
public String getSplitter(String s);
/**
* <br>
*
* @param s
* @return <code>true</code>
*/
public boolean isAnnotation(String s);
public static final LanguageMapConfiguration DEFAULT = new LanguageMapConfiguration() {

View File

@ -0,0 +1,42 @@
package cn.glycol.t18n;
import java.nio.charset.Charset;
import java.util.Objects;
public class T18n extends I18n {
/**
*
* @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;
}
}

View File

@ -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();