mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-18 08:16:58 -04:00
Move DATE_TIME_FORMATTER to SupportedLocale (#2662)
* Move dateTimeFormatter to SupportedLocale * update
This commit is contained in:
parent
59a9b3a926
commit
b9a6401ef3
@ -55,7 +55,6 @@ import org.jackhuang.hmcl.ui.wizard.Refreshable;
|
|||||||
import org.jackhuang.hmcl.ui.wizard.WizardPage;
|
import org.jackhuang.hmcl.ui.wizard.WizardPage;
|
||||||
import org.jackhuang.hmcl.util.HMCLService;
|
import org.jackhuang.hmcl.util.HMCLService;
|
||||||
import org.jackhuang.hmcl.util.Holder;
|
import org.jackhuang.hmcl.util.Holder;
|
||||||
import org.jackhuang.hmcl.util.i18n.Locales;
|
|
||||||
|
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -66,6 +65,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import static org.jackhuang.hmcl.ui.ToolbarListPageSkin.wrap;
|
import static org.jackhuang.hmcl.ui.ToolbarListPageSkin.wrap;
|
||||||
import static org.jackhuang.hmcl.util.Logging.LOG;
|
import static org.jackhuang.hmcl.util.Logging.LOG;
|
||||||
|
import static org.jackhuang.hmcl.util.i18n.I18n.formatDateTime;
|
||||||
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
||||||
|
|
||||||
public final class VersionsPage extends BorderPane implements WizardPage, Refreshable {
|
public final class VersionsPage extends BorderPane implements WizardPage, Refreshable {
|
||||||
@ -308,7 +308,7 @@ public final class VersionsPage extends BorderPane implements WizardPage, Refres
|
|||||||
|
|
||||||
content.setTitle(remoteVersion.getSelfVersion());
|
content.setTitle(remoteVersion.getSelfVersion());
|
||||||
if (remoteVersion.getReleaseDate() != null) {
|
if (remoteVersion.getReleaseDate() != null) {
|
||||||
content.setSubtitle(Locales.DATE_TIME_FORMATTER.get().format(remoteVersion.getReleaseDate().toInstant()));
|
content.setSubtitle(formatDateTime(remoteVersion.getReleaseDate().toInstant()));
|
||||||
} else {
|
} else {
|
||||||
content.setSubtitle(null);
|
content.setSubtitle(null);
|
||||||
}
|
}
|
||||||
|
@ -21,16 +21,16 @@ import org.jackhuang.hmcl.ui.construct.DoubleValidator;
|
|||||||
import org.jackhuang.hmcl.ui.construct.NumberValidator;
|
import org.jackhuang.hmcl.ui.construct.NumberValidator;
|
||||||
import org.jackhuang.hmcl.ui.construct.OptionToggleButton;
|
import org.jackhuang.hmcl.ui.construct.OptionToggleButton;
|
||||||
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
|
import org.jackhuang.hmcl.ui.decorator.DecoratorPage;
|
||||||
import org.jackhuang.hmcl.util.i18n.Locales;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.Logging.LOG;
|
import static org.jackhuang.hmcl.util.Logging.LOG;
|
||||||
|
import static org.jackhuang.hmcl.util.i18n.I18n.formatDateTime;
|
||||||
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
||||||
|
|
||||||
public final class WorldInfoPage extends StackPane implements DecoratorPage {
|
public final class WorldInfoPage extends StackPane implements DecoratorPage {
|
||||||
@ -110,7 +110,7 @@ public final class WorldInfoPage extends StackPane implements DecoratorPage {
|
|||||||
lastPlayedPane.setLeft(label);
|
lastPlayedPane.setLeft(label);
|
||||||
|
|
||||||
Label lastPlayedLabel = new Label();
|
Label lastPlayedLabel = new Label();
|
||||||
lastPlayedLabel.setText(Locales.SIMPLE_DATE_FORMAT.get().format(new Date(world.getLastPlayed())));
|
lastPlayedLabel.setText(formatDateTime(Instant.ofEpochMilli(world.getLastPlayed())));
|
||||||
BorderPane.setAlignment(lastPlayedLabel, Pos.CENTER_RIGHT);
|
BorderPane.setAlignment(lastPlayedLabel, Pos.CENTER_RIGHT);
|
||||||
lastPlayedPane.setRight(lastPlayedLabel);
|
lastPlayedPane.setRight(lastPlayedLabel);
|
||||||
}
|
}
|
||||||
|
@ -29,13 +29,13 @@ import org.jackhuang.hmcl.game.World;
|
|||||||
import org.jackhuang.hmcl.ui.Controllers;
|
import org.jackhuang.hmcl.ui.Controllers;
|
||||||
import org.jackhuang.hmcl.ui.FXUtils;
|
import org.jackhuang.hmcl.ui.FXUtils;
|
||||||
import org.jackhuang.hmcl.ui.wizard.SinglePageWizardProvider;
|
import org.jackhuang.hmcl.ui.wizard.SinglePageWizardProvider;
|
||||||
import org.jackhuang.hmcl.util.i18n.Locales;
|
|
||||||
import org.jackhuang.hmcl.util.versioning.VersionNumber;
|
import org.jackhuang.hmcl.util.versioning.VersionNumber;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Date;
|
import java.time.Instant;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.StringUtils.parseColorEscapes;
|
import static org.jackhuang.hmcl.util.StringUtils.parseColorEscapes;
|
||||||
|
import static org.jackhuang.hmcl.util.i18n.I18n.formatDateTime;
|
||||||
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
||||||
|
|
||||||
public class WorldListItem extends Control {
|
public class WorldListItem extends Control {
|
||||||
@ -49,7 +49,7 @@ public class WorldListItem extends Control {
|
|||||||
|
|
||||||
title.set(parseColorEscapes(world.getWorldName()));
|
title.set(parseColorEscapes(world.getWorldName()));
|
||||||
|
|
||||||
subtitle.set(i18n("world.description", world.getFileName(), Locales.SIMPLE_DATE_FORMAT.get().format(new Date(world.getLastPlayed())), world.getGameVersion() == null ? i18n("message.unknown") : world.getGameVersion()));
|
subtitle.set(i18n("world.description", world.getFileName(), formatDateTime(Instant.ofEpochMilli(world.getLastPlayed())), world.getGameVersion() == null ? i18n("message.unknown") : world.getGameVersion()));
|
||||||
|
|
||||||
setOnMouseClicked(event -> showInfo());
|
setOnMouseClicked(event -> showInfo());
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ package org.jackhuang.hmcl.util.i18n;
|
|||||||
import org.jackhuang.hmcl.setting.ConfigHolder;
|
import org.jackhuang.hmcl.setting.ConfigHolder;
|
||||||
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
|
import org.jackhuang.hmcl.util.i18n.Locales.SupportedLocale;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.IllegalFormatException;
|
import java.util.IllegalFormatException;
|
||||||
import java.util.MissingResourceException;
|
import java.util.MissingResourceException;
|
||||||
@ -68,6 +69,10 @@ public final class I18n {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String formatDateTime(Instant instant) {
|
||||||
|
return getCurrentLocale().getDateTimeFormatter().format(instant);
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean hasKey(String key) {
|
public static boolean hasKey(String key) {
|
||||||
return getResourceBundle().containsKey(key);
|
return getResourceBundle().containsKey(key);
|
||||||
}
|
}
|
||||||
|
@ -21,19 +21,15 @@ import com.google.gson.annotations.JsonAdapter;
|
|||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
import org.jackhuang.hmcl.util.Lang;
|
import org.jackhuang.hmcl.util.Lang;
|
||||||
import org.jackhuang.hmcl.util.Lazy;
|
|
||||||
import org.jackhuang.hmcl.util.platform.JavaVersion;
|
import org.jackhuang.hmcl.util.platform.JavaVersion;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
|
||||||
|
|
||||||
public final class Locales {
|
public final class Locales {
|
||||||
private Locales() {
|
private Locales() {
|
||||||
}
|
}
|
||||||
@ -104,10 +100,11 @@ public final class Locales {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JsonAdapter(SupportedLocale.TypeAdapter.class)
|
@JsonAdapter(SupportedLocale.TypeAdapter.class)
|
||||||
public static class SupportedLocale {
|
public static final class SupportedLocale {
|
||||||
private final Locale locale;
|
private final Locale locale;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final ResourceBundle resourceBundle;
|
private final ResourceBundle resourceBundle;
|
||||||
|
private DateTimeFormatter dateTimeFormatter;
|
||||||
|
|
||||||
SupportedLocale(Locale locale) {
|
SupportedLocale(Locale locale) {
|
||||||
this(locale, null);
|
this(locale, null);
|
||||||
@ -132,12 +129,20 @@ public final class Locales {
|
|||||||
return resourceBundle;
|
return resourceBundle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DateTimeFormatter getDateTimeFormatter() {
|
||||||
|
if (dateTimeFormatter == null) {
|
||||||
|
dateTimeFormatter = DateTimeFormatter.ofPattern(resourceBundle.getString("world.time")).withZone(ZoneId.systemDefault());
|
||||||
|
}
|
||||||
|
|
||||||
|
return dateTimeFormatter;
|
||||||
|
}
|
||||||
|
|
||||||
public String getName(ResourceBundle newResourceBundle) {
|
public String getName(ResourceBundle newResourceBundle) {
|
||||||
if (name == null) return resourceBundle.getString("lang");
|
if (name == null) return resourceBundle.getString("lang");
|
||||||
else return newResourceBundle.getString(name);
|
else return newResourceBundle.getString(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class TypeAdapter extends com.google.gson.TypeAdapter<SupportedLocale> {
|
public static final class TypeAdapter extends com.google.gson.TypeAdapter<SupportedLocale> {
|
||||||
@Override
|
@Override
|
||||||
public void write(JsonWriter out, SupportedLocale value) throws IOException {
|
public void write(JsonWriter out, SupportedLocale value) throws IOException {
|
||||||
out.value(getNameByLocale(value));
|
out.value(getNameByLocale(value));
|
||||||
@ -149,7 +154,4 @@ public final class Locales {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Lazy<SimpleDateFormat> SIMPLE_DATE_FORMAT = new Lazy<>(() -> new SimpleDateFormat(i18n("world.time")));
|
|
||||||
public static final Lazy<DateTimeFormatter> DATE_TIME_FORMATTER = new Lazy<>(() -> DateTimeFormatter.ofPattern(i18n("world.time")).withZone(ZoneId.systemDefault()));
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user