Skip to content
Merged
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
23 changes: 13 additions & 10 deletions HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.jackhuang.hmcl.auth.AuthInfo;
import org.jackhuang.hmcl.launch.DefaultLauncher;
import org.jackhuang.hmcl.launch.ProcessListener;
import org.jackhuang.hmcl.util.i18n.Locales;
import org.jackhuang.hmcl.util.i18n.LocaleUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import org.jackhuang.hmcl.util.platform.ManagedProcess;
import org.jackhuang.hmcl.util.versioning.GameVersionNumber;
Expand Down Expand Up @@ -81,7 +81,7 @@ private void generateOptionsTxt() {
}

Locale locale = Locale.getDefault();
if (Locales.isEnglish(locale))
if (LocaleUtils.isEnglish(locale))
return;

/*
Expand Down Expand Up @@ -115,14 +115,6 @@ private static String normalizedLanguageTag(Locale locale, GameVersionNumber gam
String region = locale.getCountry();

switch (language) {
case "zh":
case "cmn":
if (Locales.isSimplifiedChinese(locale))
return "zh_CN";
if (gameVersion.compareTo("1.16") >= 0
&& (region.equals("HK") || region.equals("MO")))
return "zh_HK";
return "zh_TW";
case "ru":
return "ru_RU";
case "uk":
Expand All @@ -135,7 +127,18 @@ private static String normalizedLanguageTag(Locale locale, GameVersionNumber gam
return gameVersion.compareTo("1.16") >= 0
? "lzh"
: "";
case "zh":
default:
if (LocaleUtils.isChinese(locale)) {
String script = LocaleUtils.getScript(locale);
if ("Hant".equals(script)) {
if ((region.equals("HK") || region.equals("MO") && gameVersion.compareTo("1.16") >= 0))
return "zh_HK";
return "zh_TW";
}
return "zh_CN";
}

return "";
}
}
Expand Down
73 changes: 71 additions & 2 deletions HMCL/src/main/java/org/jackhuang/hmcl/setting/FontManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@
import javafx.scene.text.Font;
import org.jackhuang.hmcl.Metadata;
import org.jackhuang.hmcl.util.Lazy;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.i18n.DefaultResourceBundleControl;
import org.jackhuang.hmcl.util.i18n.I18n;
import org.jackhuang.hmcl.util.io.JarUtils;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jackhuang.hmcl.util.platform.SystemUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.*;
import java.util.stream.Stream;

import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.util.logging.Logger.LOG;
Expand All @@ -50,7 +54,21 @@ public final class FontManager {
public static final double DEFAULT_FONT_SIZE = 12.0f;

private static final Lazy<Font> DEFAULT_FONT = new Lazy<>(() -> {
Font font = tryLoadDefaultFont(Metadata.HMCL_CURRENT_DIRECTORY);
Font font;

// Recommended

font = tryLoadLocalizedFont(Metadata.HMCL_CURRENT_DIRECTORY.resolve("font"));
if (font != null)
return font;

font = tryLoadLocalizedFont(Metadata.HMCL_GLOBAL_DIRECTORY.resolve("font"));
if (font != null)
return font;

// Legacy

font = tryLoadDefaultFont(Metadata.HMCL_CURRENT_DIRECTORY);
if (font != null)
return font;

Expand All @@ -69,6 +87,8 @@ public final class FontManager {
return font;
}

// Default

String fcMatchPattern;
if (OperatingSystem.CURRENT_OS.isLinuxOrBSD()
&& !(fcMatchPattern = I18n.getLocale().getFcMatchPattern()).isEmpty())
Expand Down Expand Up @@ -108,6 +128,7 @@ private static Font tryLoadDefaultFont(Path dir) {
for (String extension : FONT_EXTENSIONS) {
Path path = dir.resolve("font." + extension);
if (Files.isRegularFile(path)) {
LOG.info("Load font file: " + path);
try {
Font font = Font.loadFont(path.toUri().toURL().toExternalForm(), DEFAULT_FONT_SIZE);
if (font != null) {
Expand All @@ -123,6 +144,53 @@ private static Font tryLoadDefaultFont(Path dir) {
return null;
}

private static Font tryLoadLocalizedFont(Path dir) {
if (!Files.isDirectory(dir))
return null;

try (Stream<Path> list = Files.list(dir)) {
Map<String, Path> map = new HashMap<>();

Set<String> extensions = Set.of(FONT_EXTENSIONS);
list.forEach(file -> {
if (Files.isRegularFile(file)) {
String fileName = file.getFileName().toString();
String extension = StringUtils.substringAfterLast(fileName, '.');

if (fileName.startsWith("font") && extensions.contains(extension)) {
map.put(fileName.substring(0, fileName.length() - extension.length() - 1), file);
}
}
});

List<Locale> candidateLocales = I18n.getLocale().getCandidateLocales();

for (Locale locale : candidateLocales) {
String key = DefaultResourceBundleControl.INSTANCE.toBundleName("font", locale);

Path path = map.get(key);
if (path != null) {
LOG.info("Load font file: " + path);
try {
Font font = Font.loadFont(
path.toAbsolutePath().normalize().toUri().toURL().toExternalForm(),
DEFAULT_FONT_SIZE);
if (font != null)
return font;
} catch (MalformedURLException ignored) {
}

LOG.warning("Failed to load font " + path);
}
}

} catch (IOException e) {
LOG.warning("Failed to load font " + dir, e);
}

return null;
}

public static Font findByFcMatch(String pattern) {
Path fcMatch = SystemUtils.which("fc-match");
if (fcMatch == null)
Expand All @@ -147,6 +215,7 @@ public static Font findByFcMatch(String pattern) {
return null;
}

LOG.info("Load font file: " + path);
Font[] fonts = Font.loadFonts(file.toUri().toURL().toExternalForm(), DEFAULT_FONT_SIZE);
if (fonts == null) {
LOG.warning("Failed to load font from " + path);
Expand Down
6 changes: 2 additions & 4 deletions HMCL/src/main/java/org/jackhuang/hmcl/util/i18n/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,21 @@ private I18n() {
}

private static volatile SupportedLocale locale = Locales.DEFAULT;
private static volatile ResourceBundle resourceBundle = locale.getResourceBundle();

public static void setLocale(SupportedLocale locale) {
I18n.locale = locale;
resourceBundle = locale.getResourceBundle();
}

public static SupportedLocale getLocale() {
return locale;
}

public static boolean isUseChinese() {
return Locales.isChinese(locale.getLocale());
return LocaleUtils.isChinese(locale.getLocale());
}

public static ResourceBundle getResourceBundle() {
return resourceBundle;
return locale.getResourceBundle();
}

public static String i18n(String key, Object... formatArgs) {
Expand Down
Loading