From b7fc0f8d4e0f935c8f1dcbd6365927938e2f883f Mon Sep 17 00:00:00 2001 From: zhouhao Date: Wed, 15 Jan 2025 17:17:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=9B=BD?= =?UTF-8?q?=E9=99=85=E5=8C=96=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hswebframework/web/i18n/LocaleUtils.java | 31 ++++++++++++++++++- .../web/i18n/LocaleUtilsTest.java | 9 ++++++ .../dictionary/entity/DictionaryEntity.java | 5 +++ .../entity/DictionaryItemEntity.java | 10 ++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/hsweb-core/src/main/java/org/hswebframework/web/i18n/LocaleUtils.java b/hsweb-core/src/main/java/org/hswebframework/web/i18n/LocaleUtils.java index 295a1326b..b7f555eeb 100644 --- a/hsweb-core/src/main/java/org/hswebframework/web/i18n/LocaleUtils.java +++ b/hsweb-core/src/main/java/org/hswebframework/web/i18n/LocaleUtils.java @@ -11,7 +11,7 @@ import reactor.core.publisher.*; import reactor.util.context.Context; import javax.annotation.Nonnull; -import java.util.Locale; +import java.util.*; import java.util.concurrent.Callable; import java.util.function.*; @@ -36,6 +36,35 @@ public final class LocaleUtils { static MessageSource messageSource = UnsupportedMessageSource.instance(); + static Set supportsLocales; + + static { + supportsLocales = new HashSet<>(); + supportsLocales.add(Locale.CHINESE); + supportsLocales.add(Locale.ENGLISH); + String prop = System.getProperty("hsweb.locale.supports"); + if (prop != null) { + try { + for (String locale : prop.split(",")) { + if (locale.isEmpty()) { + continue; + } + supportsLocales.add(Locale.forLanguageTag(locale)); + } + } catch (Throwable e) { + System.err.println("error parse hsweb.locale.supports :" + prop); + } + } + } + + /** + * 获取支持的语言地区,默认支持中文和英文,可通过jvm参数: -Dhsweb.locale.supports=zh,en 来指定支持的语言地区 + * + * @return 支持的语言地区 + */ + public static Set getSupportLocales() { + return Collections.unmodifiableSet(supportsLocales); + } /** * 从指定数据源中获取国际化消息 diff --git a/hsweb-core/src/test/java/org/hswebframework/web/i18n/LocaleUtilsTest.java b/hsweb-core/src/test/java/org/hswebframework/web/i18n/LocaleUtilsTest.java index 4bc2be7ff..4d663fbc7 100644 --- a/hsweb-core/src/test/java/org/hswebframework/web/i18n/LocaleUtilsTest.java +++ b/hsweb-core/src/test/java/org/hswebframework/web/i18n/LocaleUtilsTest.java @@ -11,6 +11,15 @@ import static org.junit.Assert.*; public class LocaleUtilsTest { + @Test + public void testSupports(){ + + assertNotNull(LocaleUtils.getSupportLocales()); + + System.out.println(LocaleUtils.getSupportLocales()); + + + } @Test public void testFlux() { diff --git a/hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/entity/DictionaryEntity.java b/hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/entity/DictionaryEntity.java index a6c575676..537092a40 100644 --- a/hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/entity/DictionaryEntity.java +++ b/hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/entity/DictionaryEntity.java @@ -30,6 +30,7 @@ import org.hswebframework.web.crud.generator.Generators; import org.hswebframework.web.dict.DictDefine; import org.hswebframework.web.dict.defaults.DefaultDictDefine; import org.hswebframework.web.i18n.I18nSupportUtils; +import org.hswebframework.web.i18n.LocaleUtils; import org.hswebframework.web.i18n.MultipleI18nSupportEntity; import org.hswebframework.web.validator.CreateGroup; @@ -96,6 +97,10 @@ public class DictionaryEntity extends GenericEntity implements RecordCre return getI18nMessage("describe", this.describe); } + public void putI18nName(String i18nKey) { + putI18nName(i18nKey, LocaleUtils.getSupportLocales()); + } + public void putI18nName(String i18nKey, Collection locales) { this.i18nMessages = I18nSupportUtils diff --git a/hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/entity/DictionaryItemEntity.java b/hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/entity/DictionaryItemEntity.java index 460518478..ac765cf9a 100644 --- a/hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/entity/DictionaryItemEntity.java +++ b/hsweb-system/hsweb-system-dictionary/src/main/java/org/hswebframework/web/dictionary/entity/DictionaryItemEntity.java @@ -27,6 +27,7 @@ import org.hswebframework.ezorm.rdb.mapping.annotation.JsonCodec; import org.hswebframework.web.api.crud.entity.GenericTreeSortSupportEntity; import org.hswebframework.web.dict.EnumDict; import org.hswebframework.web.i18n.I18nSupportUtils; +import org.hswebframework.web.i18n.LocaleUtils; import org.hswebframework.web.i18n.MultipleI18nSupportEntity; import org.hswebframework.web.utils.DigestUtils; import org.springframework.util.StringUtils; @@ -107,6 +108,15 @@ public class DictionaryItemEntity extends GenericTreeSortSupportEntity return getI18nMessage("text", this.text); } + /** + * 根据消息key生成默认的的语言并填充到i18nMessages中 + * + * @param i18nKey key,在国际化文件中定义. + */ + public void putI18nText(String i18nKey) { + putI18nText(i18nKey, LocaleUtils.getSupportLocales()); + } + /** * 根据消息key生成对应的语言并填充到i18nMessages中 *