Compare commits
3 Commits
cf2f20052a
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| eedc9ff7d9 | |||
| 1ef52c7963 | |||
| 4f10cf3c20 |
10
pom.xml
10
pom.xml
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>com.imyeyu.lang</groupId>
|
<groupId>com.imyeyu.lang</groupId>
|
||||||
<artifactId>timi-lang</artifactId>
|
<artifactId>timi-lang</artifactId>
|
||||||
<version>0.0.1</version>
|
<version>0.0.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@ -78,12 +78,12 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.imyeyu.io</groupId>
|
<groupId>com.imyeyu.io</groupId>
|
||||||
<artifactId>timi-io</artifactId>
|
<artifactId>timi-io</artifactId>
|
||||||
<version>0.0.1</version>
|
<version>0.0.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mariadb.jdbc</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>mariadb-java-client</artifactId>
|
<artifactId>junit-jupiter</artifactId>
|
||||||
<version>3.5.3</version>
|
<version>5.10.2</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@ -2,8 +2,9 @@ package com.imyeyu.lang.mapper;
|
|||||||
|
|
||||||
import com.imyeyu.java.bean.Language;
|
import com.imyeyu.java.bean.Language;
|
||||||
import com.imyeyu.java.bean.LanguageMapping;
|
import com.imyeyu.java.bean.LanguageMapping;
|
||||||
|
import com.imyeyu.utils.StringInterpolator;
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
@ -11,7 +12,7 @@ import java.text.MessageFormat;
|
|||||||
*/
|
*/
|
||||||
public abstract class AbstractLanguageMapper implements LanguageMapping {
|
public abstract class AbstractLanguageMapper implements LanguageMapping {
|
||||||
|
|
||||||
protected static final MessageFormat FORMAT = new MessageFormat("");
|
protected final StringInterpolator INTERPOLATOR = StringInterpolator.createDollarInterpolator();
|
||||||
|
|
||||||
protected final Language language;
|
protected final Language language;
|
||||||
|
|
||||||
@ -28,4 +29,15 @@ public abstract class AbstractLanguageMapper implements LanguageMapping {
|
|||||||
public void setDebugging(boolean debugging) {
|
public void setDebugging(boolean debugging) {
|
||||||
isDebugging = debugging;
|
isDebugging = debugging;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String text(String key, String def) {
|
||||||
|
String result = text(key);
|
||||||
|
return result.equals(key) ? def : result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String textArgs(String key, Map<String, Object> args) {
|
||||||
|
return INTERPOLATOR.inject(text(key), args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
package com.imyeyu.lang.mapper;
|
package com.imyeyu.lang.mapper;
|
||||||
|
|
||||||
import com.imyeyu.java.TimiJava;
|
import com.imyeyu.java.TimiJava;
|
||||||
|
import com.imyeyu.java.bean.CallbackArgReturn;
|
||||||
import com.imyeyu.java.bean.Language;
|
import com.imyeyu.java.bean.Language;
|
||||||
import com.imyeyu.utils.Text;
|
import com.imyeyu.utils.Text;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -17,6 +17,15 @@ public class LanguageMap extends AbstractLanguageMapper {
|
|||||||
|
|
||||||
protected final Map<String, String> map;
|
protected final Map<String, String> map;
|
||||||
|
|
||||||
|
{
|
||||||
|
INTERPOLATOR.putFilter("lang", value -> {
|
||||||
|
if (value.startsWith("@")) {
|
||||||
|
value = text(value.substring(1));
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认构造
|
* 默认构造
|
||||||
*
|
*
|
||||||
@ -107,18 +116,12 @@ public class LanguageMap extends AbstractLanguageMapper {
|
|||||||
/**
|
/**
|
||||||
* 插入参数获取文本
|
* 插入参数获取文本
|
||||||
*
|
*
|
||||||
* @param key 键
|
* @param key 键
|
||||||
* @param args 参数
|
* @param argsMap 参数
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String textArgs(String key, Object... args) {
|
public String textArgs(String key, Map<String, Object> argsMap) {
|
||||||
String result = text(key);
|
return INTERPOLATOR.inject(text(key), argsMap);
|
||||||
if (map.containsKey(result)) {
|
|
||||||
// 没有映射值
|
|
||||||
return result + Arrays.toString(args);
|
|
||||||
}
|
|
||||||
FORMAT.applyPattern(result);
|
|
||||||
return FORMAT.format(args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -204,15 +204,15 @@ public class Multilingual implements LanguageMapping {
|
|||||||
/**
|
/**
|
||||||
* 从当前激活语言获取插入参数文本
|
* 从当前激活语言获取插入参数文本
|
||||||
*
|
*
|
||||||
* @param key 键
|
* @param key 键
|
||||||
* @param params 参数
|
* @param argsMap 参数
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String textArgs(String key, Object... params) {
|
public String textArgs(String key, Map<String, Object> argsMap) {
|
||||||
if (multilingualMap.get(getActivated()) == null) {
|
if (multilingualMap.get(getActivated()) == null) {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
return multilingualMap.get(getActivated()).textArgs(key, params);
|
return multilingualMap.get(getActivated()).textArgs(key, argsMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/test/java/test/TestLang.java
Normal file
21
src/test/java/test/TestLang.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import com.imyeyu.java.bean.Language;
|
||||||
|
import com.imyeyu.lang.mapper.ResourcesLanguageMap;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 夜雨
|
||||||
|
* @since 2025-11-06 14:02
|
||||||
|
*/
|
||||||
|
public class TestLang {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testArgs() {
|
||||||
|
ResourcesLanguageMap map = new ResourcesLanguageMap(Language.zh_CN);
|
||||||
|
map.load("zh_CN.lang");
|
||||||
|
System.out.println(map.textArgs("test", Map.of("msg", "hello world")));
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/test/resources/zh_CN.lang
Normal file
1
src/test/resources/zh_CN.lang
Normal file
@ -0,0 +1 @@
|
|||||||
|
test=say ${msg}
|
||||||
Reference in New Issue
Block a user