mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-26 14:53:14 +00:00
Fix/use game locale for numbers (#266)
Co-authored-by: Nikolay S. <[email protected]> Co-authored-by: Martin Robertz <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
6 files changed
+149
-5
No files matched your search
@@ -22,12 +22,14 @@ import com.gtnewhorizon.gtnhlib.compat.Mods;
|
||||
import com.gtnewhorizon.gtnhlib.compat.NotEnoughItemsVersionChecker;
|
||||
import com.gtnewhorizon.gtnhlib.config.ConfigurationManager;
|
||||
import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber;
|
||||
import com.gtnewhorizon.gtnhlib.eventhandlers.ConfigEventHandler;
|
||||
import com.gtnewhorizon.gtnhlib.itemrendering.TexturedItemRenderer;
|
||||
import com.gtnewhorizon.gtnhlib.test.item.TestItem;
|
||||
import com.gtnewhorizon.gtnhlib.util.AboveHotbarHUD;
|
||||
|
||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.event.FMLConstructionEvent;
|
||||
import cpw.mods.fml.common.event.FMLInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
@@ -44,6 +46,14 @@ public class ClientProxy extends CommonProxy {
|
||||
public static int currentServerViewDistance = 12;
|
||||
public static final Minecraft mc = Minecraft.getMinecraft();
|
||||
|
||||
@Override
|
||||
public void construct(FMLConstructionEvent event) {
|
||||
super.construct(event);
|
||||
|
||||
// ConfigChangedEvent is client-only; register handler only on client.
|
||||
FMLCommonHandler.instance().bus().register(new ConfigEventHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
super.preInit(event);
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.gtnewhorizon.gtnhlib.eventhandlers;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.GTNHLib;
|
||||
import com.gtnewhorizon.gtnhlib.util.numberformatting.NumberFormatConfig;
|
||||
|
||||
import cpw.mods.fml.client.event.ConfigChangedEvent;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class ConfigEventHandler {
|
||||
|
||||
@SubscribeEvent
|
||||
public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) {
|
||||
if (event.modID.equals(GTNHLib.MODID)) {
|
||||
// Reload number formatting settings immediately when config changes
|
||||
NumberFormatConfig.syncNumberFormatting();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public enum ExponentialFormat {
|
||||
|
||||
int fractionalDigits = Math.max(1, options.getDecimalPlaces());
|
||||
|
||||
Locale locale = Locale.getDefault(Locale.Category.FORMAT);
|
||||
Locale locale = NumberFormatConfig.getActiveLocale();
|
||||
DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
|
||||
symbols.setExponentSeparator("e");
|
||||
|
||||
@@ -99,7 +99,7 @@ public enum ExponentialFormat {
|
||||
int exponent = rounded.precision() - 1 - rounded.scale();
|
||||
BigDecimal mantissa = rounded.movePointLeft(exponent);
|
||||
|
||||
Locale locale = Locale.getDefault(Locale.Category.FORMAT);
|
||||
Locale locale = NumberFormatConfig.getActiveLocale();
|
||||
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
|
||||
|
||||
DecimalFormat df = new DecimalFormat();
|
||||
|
||||
+108
-1
@@ -1,5 +1,8 @@
|
||||
package com.gtnewhorizon.gtnhlib.util.numberformatting;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.GTNHLib;
|
||||
import com.gtnewhorizon.gtnhlib.config.Config;
|
||||
|
||||
@Config(modid = "gtnhlib", category = "number_formatting", filename = "gtnhlib-number-formatting")
|
||||
@@ -22,10 +25,114 @@ public final class NumberFormatConfig {
|
||||
" SCIENTIFIC – Standard scientific notation (e.g. 1.23e12)",
|
||||
" ENGINEERING – Engineering notation with exponent in multiples of 3 (e.g. 123e9)",
|
||||
" POWER_OF_TEN – Power-of-ten notation using explicit multiplication (e.g. 1.23*10^12)",
|
||||
"Invalid values will cause the game to fail fast with an error." })
|
||||
"Invalid values will be logged and fall back to SCIENTIFIC." })
|
||||
@Config.DefaultString("SCIENTIFIC")
|
||||
public static String formatPattern = "SCIENTIFIC";
|
||||
|
||||
@Config.Comment({ "Select the number formatting locale. Choose SYSTEM_DEFAULT to use your operating system locale.",
|
||||
"Each option shows example formatting:", " SYSTEM_DEFAULT – Use your system language settings",
|
||||
" ENGLISH_US – 1,234.56 (comma grouping, period decimal)",
|
||||
" RUSSIAN – 1 234,56 (space grouping, comma decimal)",
|
||||
" GERMAN – 1.234,56 (period grouping, comma decimal)",
|
||||
" FRENCH – 1 234,56 (space grouping, comma decimal)",
|
||||
" SPANISH – 1.234,56 (period grouping, comma decimal)",
|
||||
" ITALIAN – 1.234,56 (period grouping, comma decimal)",
|
||||
" JAPANESE – 1,234.56 (comma grouping, period decimal)",
|
||||
" CHINESE – 1,234.56 (comma grouping, period decimal)",
|
||||
" POLISH – 1 234,56 (space grouping, comma decimal)",
|
||||
" PORTUGUESE_BR – 1.234,56 (period grouping, comma decimal)",
|
||||
" DUTCH – 1.234,56 (period grouping, comma decimal)",
|
||||
" SWEDISH – 1 234,56 (space grouping, comma decimal)",
|
||||
" NORWEGIAN – 1 234,56 (space grouping, comma decimal)",
|
||||
" FINNISH – 1 234,56 (space grouping, comma decimal)",
|
||||
" CZECH – 1 234,56 (space grouping, comma decimal)",
|
||||
" HUNGARIAN – 1 234,56 (space grouping, comma decimal)" })
|
||||
@Config.DefaultEnum("SYSTEM_DEFAULT")
|
||||
public static LocaleOption numberFormatLocale = LocaleOption.SYSTEM_DEFAULT;
|
||||
|
||||
@Config.Ignore
|
||||
public static ExponentialFormat EXPONENTIAL_FORMAT = ExponentialFormat.SCIENTIFIC;
|
||||
|
||||
/**
|
||||
* Enum representing available locale options for number formatting
|
||||
*/
|
||||
public enum LocaleOption {
|
||||
|
||||
SYSTEM_DEFAULT("System Default", null),
|
||||
ENGLISH_US("English (US) - 1,234.56", new Locale("en", "US")),
|
||||
RUSSIAN("Russian - 1 234,56", new Locale("ru", "RU")),
|
||||
GERMAN("German - 1.234,56", new Locale("de", "DE")),
|
||||
FRENCH("French - 1 234,56", new Locale("fr", "FR")),
|
||||
SPANISH("Spanish - 1.234,56", new Locale("es", "ES")),
|
||||
ITALIAN("Italian - 1.234,56", new Locale("it", "IT")),
|
||||
JAPANESE("Japanese - 1,234.56", new Locale("ja", "JP")),
|
||||
CHINESE("Chinese - 1,234.56", new Locale("zh", "CN")),
|
||||
POLISH("Polish - 1 234,56", new Locale("pl", "PL")),
|
||||
PORTUGUESE_BR("Portuguese (Brazil) - 1.234,56", new Locale("pt", "BR")),
|
||||
DUTCH("Dutch - 1.234,56", new Locale("nl", "NL")),
|
||||
SWEDISH("Swedish - 1 234,56", new Locale("sv", "SE")),
|
||||
NORWEGIAN("Norwegian - 1 234,56", new Locale("no", "NO")),
|
||||
FINNISH("Finnish - 1 234,56", new Locale("fi", "FI")),
|
||||
CZECH("Czech - 1 234,56", new Locale("cs", "CZ")),
|
||||
HUNGARIAN("Hungarian - 1 234,56", new Locale("hu", "HU"));
|
||||
|
||||
private final String displayName;
|
||||
private final Locale locale;
|
||||
|
||||
LocaleOption(String displayName, Locale locale) {
|
||||
this.displayName = displayName;
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
if (this == SYSTEM_DEFAULT || locale == null) {
|
||||
return Locale.getDefault(Locale.Category.FORMAT);
|
||||
}
|
||||
return locale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return displayName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizes the number formatting locale from config. Called during mod initialization and when config changes.
|
||||
*/
|
||||
public static void syncNumberFormatting() {
|
||||
// Parse and apply exponential format pattern
|
||||
try {
|
||||
EXPONENTIAL_FORMAT = ExponentialFormat.parse(formatPattern);
|
||||
} catch (IllegalStateException ignored) {
|
||||
GTNHLib.error("Invalid exponential format pattern: '" + formatPattern + "', fell back to SCIENTIFIC");
|
||||
EXPONENTIAL_FORMAT = ExponentialFormat.SCIENTIFIC;
|
||||
}
|
||||
|
||||
Locale activeLocale = numberFormatLocale.getLocale();
|
||||
GTNHLib.info("Number formatting: Using " + numberFormatLocale.getDisplayName() + " (" + activeLocale + ")");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the currently active locale for number formatting.
|
||||
*
|
||||
* @return Active locale selected by configuration
|
||||
*/
|
||||
public static Locale getActiveLocale() {
|
||||
return numberFormatLocale.getLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a non-system locale override is active. Intended for downstream consumers that want to detect a locale
|
||||
* override.
|
||||
*
|
||||
* @return true if a locale other than system default is selected
|
||||
*/
|
||||
public static boolean hasCustomLocale() {
|
||||
return numberFormatLocale != LocaleOption.SYSTEM_DEFAULT;
|
||||
}
|
||||
}
|
||||
@@ -230,7 +230,9 @@ public final class NumberFormatUtil {
|
||||
}
|
||||
|
||||
private static DecimalFormat getDefaultDecimalFormatter(NumberOptionsBase<?> options) {
|
||||
Locale locale = Locale.getDefault(Locale.Category.FORMAT);
|
||||
// Use custom locale from config if set, otherwise use system default
|
||||
Locale locale = NumberFormatConfig.getActiveLocale();
|
||||
|
||||
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
|
||||
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(locale);
|
||||
df.setDecimalFormatSymbols(symbols);
|
||||
@@ -261,7 +263,7 @@ public final class NumberFormatUtil {
|
||||
}
|
||||
|
||||
public static void postConfiguration() {
|
||||
EXPONENTIAL_FORMAT = ExponentialFormat.parse(formatPattern);
|
||||
NumberFormatConfig.syncNumberFormatting();
|
||||
}
|
||||
|
||||
public static BigDecimal bigDecimalConverter(Number number) {
|
||||
|
||||
Reference in new issue
Block a user