Initial project

This commit is contained in:
Timi
2025-07-14 14:27:01 +08:00
parent 0adde06b54
commit 49d88e13e6
15 changed files with 767 additions and 94 deletions

View File

@ -0,0 +1,31 @@
package com.imyeyu.lang.mapper;
import com.imyeyu.java.bean.Language;
import com.imyeyu.java.bean.LanguageMapping;
import java.text.MessageFormat;
/**
* @author 夜雨
* @version 2024-04-01 16:25
*/
public abstract class AbstractLanguageMapper implements LanguageMapping {
protected static final MessageFormat FORMAT = new MessageFormat("");
protected final Language language;
protected boolean isDebugging = false;
public AbstractLanguageMapper(Language language) {
this.language = language;
}
public Language getLanguage() {
return language;
}
public void setDebugging(boolean debugging) {
isDebugging = debugging;
}
}

View File

@ -0,0 +1,35 @@
package com.imyeyu.lang.mapper;
import com.imyeyu.io.IO;
import com.imyeyu.java.bean.Language;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* @author 夜雨
* @version 2024-04-09 01:00
*/
public class FileLanguageMap extends PropertiesLanguageMap {
/**
* 默认构造
*
* @param language 所属语言
*/
public FileLanguageMap(Language language) {
super(language);
}
public void load(String path) {
try {
InputStream is = IO.getInputStream(path.formatted(language.toString()));
Properties properties = new Properties();
properties.load(new InputStreamReader(is));
load(properties);
} catch (Exception e) {
throw new RuntimeException("load language properties file error", e);
}
}
}

View File

@ -0,0 +1,124 @@
package com.imyeyu.lang.mapper;
import com.imyeyu.java.TimiJava;
import com.imyeyu.java.bean.Language;
import com.imyeyu.utils.Text;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author 夜雨
* @version 2024-04-03 10:11
*/
public class LanguageMap extends AbstractLanguageMapper {
protected final Map<String, String> map;
/**
* 默认构造
*
* @param language 所属语言
*/
public LanguageMap(Language language) {
super(language);
this.map = new HashMap<>();
}
public void union(LanguageMap map) {
this.map.putAll(map.map);
}
@Override
public void add(String key, String value) {
map.put(key, value);
}
@Override
public boolean has(String key) {
return map.containsKey(key);
}
/**
* 获取文本,支持二次映射,没有找到映射时返回入参键
* <pre>
* test.msg=Hello world
* mapping=@test.msg
* no_mapping=\@test.msg
*
* Multilingual.text("mapping"); // Hello world
* Multilingual.text("no_mapping"); // @test.msg
* </pre>
*
* @param key 键
* @return 文本值
*/
@Override
public String text(String key) {
if (map.containsKey(key)) {
String result = map.get(key);
if (result.startsWith("@")) {
// 递归映射
return text(result.substring(1));
} else {
if (result.startsWith("\\@")) {
return result.substring(1);
} else {
return result;
}
}
} else {
if (TimiJava.isNotEmpty(key)) {
// 推测
String guessKey = key;
LinkedHashMap<String, Number> keySRL = Text.similarityRatioList(map.keySet(), key);
if (keySRL.keySet().iterator().hasNext()) {
String k = keySRL.keySet().iterator().next();
Number ratio = keySRL.get(k);
if (.7 < ratio.doubleValue()) {
guessKey = k;
}
}
System.err.printf("not found language mapping for key: [%s], it is [%s]?%n", key, guessKey);
if (isDebugging) {
throw new RuntimeException("not found language mapping key: " + key);
}
}
}
// 找不到映射,直接返回键
return key;
}
/**
* 获取文本
*
* @param key 键
* @param def 默认值(没有找到映射值时)
* @return 获取结果
*/
@Override
public String text(String key, String def) {
String result = text(key);
return key.equals(result) ? def : result;
}
/**
* 插入参数获取文本
*
* @param key 键
* @param args 参数
* @return 结果
*/
@Override
public String textArgs(String key, Object... args) {
String result = text(key);
if (map.containsKey(result)) {
// 没有映射值
return result + Arrays.toString(args);
}
FORMAT.applyPattern(result);
return FORMAT.format(args);
}
}

View File

@ -0,0 +1,27 @@
package com.imyeyu.lang.mapper;
import com.imyeyu.java.bean.Language;
import java.util.HashMap;
import java.util.Properties;
/**
* @author 夜雨
* @version 2024-04-09 00:50
*/
public class PropertiesLanguageMap extends LanguageMap {
/**
* 默认构造
*
* @param language 所属语言
*/
public PropertiesLanguageMap(Language language) {
super(language);
}
@SuppressWarnings({"unchecked", "rawtypes"})
public void load(Properties properties) {
map.putAll(new HashMap(properties));
}
}

View File

@ -0,0 +1,39 @@
package com.imyeyu.lang.mapper;
import com.imyeyu.io.IO;
import com.imyeyu.java.bean.Language;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* @author 夜雨
* @version 2024-04-09 00:55
*/
public class ResourcesLanguageMap extends PropertiesLanguageMap {
/**
* 默认构造
*
* @param language 所属语言
*/
public ResourcesLanguageMap(Language language) {
super(language);
}
public void load(String path) {
try {
InputStream is = IO.resourceToInputStream(getClass(), path.formatted(language.toString()));
if (is != null) {
Properties properties = new Properties();
properties.load(new InputStreamReader(is));
load(properties);
} else {
throw new FileNotFoundException("not found language properties file: " + path.formatted(language.toString()));
}
} catch (Exception e) {
throw new RuntimeException("load language properties file error", e);
}
}
}