Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HMCL/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ tasks.checkstyleMain {

val addOpens = listOf(
"java.base/java.lang",
"java.base/java.util",
"java.base/java.lang.reflect",
"java.base/jdk.internal.loader",
"javafx.base/com.sun.javafx.binding",
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private Metadata() {
public static final String CHANGELOG_URL = DOCS_URL + "/changelog/";
public static final String EULA_URL = DOCS_URL + "/eula/hmcl.html";
public static final String GROUPS_URL = "https://www.bilibili.com/opus/905435541874409529";
public static final String LOCALIZATION_URL = "https://github.com/HMCL-dev/HMCL/blob/main/docs/Localization.md";

public static final String BUILD_CHANNEL = JarUtils.getAttribute("hmcl.version.type", "nightly");
public static final String GITHUB_SHA = JarUtils.getAttribute("hmcl.version.hash", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import com.jfoenix.controls.JFXPopup;
import javafx.beans.InvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.css.PseudoClass;
Expand Down Expand Up @@ -189,6 +192,10 @@ public void setDescriptionConverter(Function<T, String> value) {
descriptionConverterProperty().set(value);
}

public void setNullSafeDescriptionConverter(Function<@NotNull T, String> value) {
descriptionConverterProperty().set(it -> it != null ? value.apply(it) : "");
}

private final ListProperty<T> items = new SimpleListProperty<>(this, "items", FXCollections.emptyObservableList());

public ListProperty<T> itemsProperty() {
Expand Down
21 changes: 14 additions & 7 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/main/SettingsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,26 @@ protected int getTrailingTextIndex() {
chooseLanguagePane.setSubtitle(i18n("settings.take_effect_after_restart"));

SupportedLocale currentLocale = I18n.getLocale();
chooseLanguagePane.setNullSafeConverter(locale -> {
chooseLanguagePane.setNullSafeConverter(it -> it.getDisplayName(currentLocale));
chooseLanguagePane.setNullSafeDescriptionConverter(locale -> {
if (locale.isDefault())
return locale.getDisplayName(currentLocale);
else if (locale.isSameLanguage(currentLocale))
return locale.getDisplayName(locale);
else
return locale.getDisplayName(currentLocale) + " - " + locale.getDisplayName(locale);
return "";

String name = locale.getDisplayName(locale);

double completeness = locale.getTranslationCompleteness();
if (completeness != 0.0)
return String.format("%s - %.2f%%", name, completeness * 100);
else return name;
});
chooseLanguagePane.setItems(SupportedLocale.getSupportedLocales());
chooseLanguagePane.valueProperty().bindBidirectional(settings().languageProperty());

languagePaneList.getContent().add(chooseLanguagePane);
LineButton helpButton = LineButton.createExternalLinkButton(Metadata.LOCALIZATION_URL);
helpButton.setTitle(i18n("settings.launcher.language.contribution.title"));
helpButton.setSubtitle(i18n("settings.launcher.language.contribution.subtitle"));

languagePaneList.getContent().addAll(chooseLanguagePane, helpButton);
}

rootPane.getChildren().addAll(ComponentList.createComponentListTitle(i18n("settings.launcher.language")), languagePaneList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.i18n.translator.Translator;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.PropertyKey;

import java.io.IOException;
Expand Down Expand Up @@ -294,6 +295,49 @@ public Translator getTranslator() {
return this.translator = new Translator(this);
}

@Nullable
private static final MethodHandle METHOOD_LOOKUP_HANDLE_KEY_SET;

static {
MethodHandle methoodLookupHandleKeySet;
try {
methoodLookupHandleKeySet = MethodHandles.privateLookupIn(ResourceBundle.class, MethodHandles.lookup())
.findVirtual(ResourceBundle.class, "handleKeySet", MethodType.methodType(Set.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
LOG.warning("Failed to get private lookup for ResourceBundle", e);
methoodLookupHandleKeySet = null;
}
METHOOD_LOOKUP_HANDLE_KEY_SET = methoodLookupHandleKeySet;
}

public double getTranslationCompleteness() {
if (METHOOD_LOOKUP_HANDLE_KEY_SET == null) return 0.0;

ResourceBundle enBundle = SupportedLocale.getLocale(Locale.ENGLISH).getResourceBundle();
Set<String> enKeys = enBundle.keySet();
int totalKeys = enKeys.size();
if (totalKeys == 0) return 0.0;

ResourceBundle currentBundle = getResourceBundle();
Set<String> handleKeys;

try {
@SuppressWarnings("unchecked")
Set<String> keys = (Set<String>) METHOOD_LOOKUP_HANDLE_KEY_SET.invoke(currentBundle);
handleKeys = keys;
} catch (Throwable e) {
LOG.warning("Failed to get handle keys", e);
return 0.0;
}

long actualTranslatedCount = handleKeys.stream()
.filter(enKeys::contains)
.count();

double v = (double) actualTranslatedCount / totalKeys;
return Math.min(1.0, v);
}

public boolean isSameLanguage(SupportedLocale other) {
return LocaleUtils.getRootLanguage(this.getLocale())
.equals(LocaleUtils.getRootLanguage(other.getLocale()));
Expand Down
Loading