use StringInterpolator
This commit is contained in:
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,7 @@ 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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 夜雨
|
* @author 夜雨
|
||||||
@ -11,7 +10,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 static final StringInterpolator INTERPOLATOR = StringInterpolator.createDollarInterpolator();
|
||||||
|
|
||||||
protected final Language language;
|
protected final Language language;
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import com.imyeyu.java.TimiJava;
|
|||||||
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;
|
||||||
@ -107,18 +106,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