doc: 增加UIResourceBundle注释

This commit is contained in:
songdragon 2023-08-27 17:00:34 +08:00
parent 6679d6e04d
commit 59c8b8f5ed

View File

@ -10,11 +10,22 @@ import org.jcnc.jnotepad.app.config.LocalizationConfig;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* UI资源绑定用于加载语言文件
*
* @author songdragon
*/
public class UIResourceBundle {
private static final UIResourceBundle INSTANCE = new UIResourceBundle();
/**
* resource目录下的i18n/i18nXXX.properties
*/
private static final String BASENAME = "i18n/i18n";
/**
* 当前语言
*/
private Locale currentLocale;
public static final UIResourceBundle getInstance() {
@ -25,8 +36,15 @@ public class UIResourceBundle {
this.resetLocal();
}
/**
* 资源文件的观察者绑定
*/
private ObjectProperty<ResourceBundle> resources = new SimpleObjectProperty<>();
/**
* 获取当前资源文件
* @return
*/
public ObjectProperty<ResourceBundle> resourcesProperty() {
return resources;
}
@ -39,6 +57,9 @@ public class UIResourceBundle {
resourcesProperty().set(resources);
}
/**
* 重置当前local
*/
public final void resetLocal() {
if (this.currentLocale == LocalizationConfig.getCurrentLocal()) {
return;
@ -49,6 +70,11 @@ public class UIResourceBundle {
}
/**
* 获取key对应的绑定属性内容
* @param key key
* @return key对应的内容
*/
public StringBinding getStringBinding(String key) {
return new StringBinding() {
{
@ -62,6 +88,11 @@ public class UIResourceBundle {
};
}
/**
* 工具方法绑定StringProperty和Key对应的内容
* @param stringProperty
* @param key
*/
public static void bindStringProperty(StringProperty stringProperty, String key) {
if (stringProperty == null) {
return;
@ -69,10 +100,19 @@ public class UIResourceBundle {
stringProperty.bind(getInstance().getStringBinding(key));
}
/**
* 获取当前资源中的key值
* @param key
* @return
*/
public static String getContent(String key) {
return getInstance().getResources().getString(key);
}
/**
* 注册资源变更监听器
* @param listener
*/
public void addListener(ChangeListener<? super ResourceBundle> listener) {
this.resources.addListener(listener);
}