From 9a25a05d1b376dc20a14696afa280ff4940b1f8a Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 21 Feb 2025 23:51:13 -0500 Subject: Add internationalization API to GUI This commit intentionally fails one test, since that is for functionality I intend to add later. --- src/main/java/sevenUnitsGUI/Presenter.java | 58 +++++++++++++++++++++++++++++ src/main/java/sevenUnitsGUI/TabbedView.java | 5 +++ src/main/java/sevenUnitsGUI/View.java | 7 ++++ src/main/java/sevenUnitsGUI/ViewBot.java | 5 +++ 4 files changed, 75 insertions(+) (limited to 'src/main') diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index c46ee53..21f2951 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -22,6 +22,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -77,6 +78,15 @@ public final class Presenter { /** A Predicate that returns true iff the argument is a full base unit */ private static final Predicate IS_FULL_BASE = unit -> unit instanceof LinearUnit && ((LinearUnit) unit).isBase(); + /** + * The default locale, used in two situations: + * + */ + static final String DEFAULT_LOCALE = "en"; /** * Adds default units and dimensions to a database. @@ -317,6 +327,12 @@ public final class Presenter { */ private final Set metricExceptions; + /** maps locale names (e.g. 'en') to key-text maps */ + final Map> locales; + + /** name of locale in locales to use */ + String userLocale; + /** * If this is true, views that show units as a list will have metric units * removed from the From unit list and imperial/USC units removed from the To @@ -375,6 +391,9 @@ public final class Presenter { e); } + // TODO load locales + this.locales = new HashMap<>(); + // set default settings temporarily if (Files.exists(CONFIG_FILE)) { this.loadSettings(CONFIG_FILE); @@ -735,6 +754,14 @@ public final class Presenter { .replaceAll("\\[LOADSTATS\\]", wrapString(this.loadStatMsg(), 72)); } + /** + * @return set of all locales available to select + * @since 2025-02-21 + */ + public final Set getAvailableLocales() { + return this.locales.keySet(); + } + /** * Gets a name for this dimension using the database * @@ -750,6 +777,20 @@ public final class Presenter { .orElse(dimension.toString(Nameable::getName)); } + /** + * Gets the correct text for a provided ID. + * If this text is available in the user's locale, that text is provided. + * Otherwise, text is taken from the system default locale {@link #DEFAULT_LOCALE}. + * + * @param textID ID of text to get (used in locale files) + * @return text to be displayed + */ + public String getLocalizedText(String textID) { + final Map userLocale = this.locales.get(this.userLocale); + final Map defaultLocale = this.locales.get(DEFAULT_LOCALE); + return userLocale.getOrDefault(textID, defaultLocale.get(textID)); + } + /** * @return the rule that is used by this presenter to convert numbers into * strings @@ -785,6 +826,14 @@ public final class Presenter { return this.searchRule; } + /** + * @return user's selected locale + * @since 2025-02-21 + */ + public String getUserLocale() { + return userLocale; + } + /** * @return a search rule that shows all single prefixes * @since 2022-07-08 @@ -1100,6 +1149,15 @@ public final class Presenter { this.updateView(); } + /** + * Sets the user's locale, updating the view. + * @param userLocale locale to use + */ + public void setUserLocale(String userLocale) { + this.userLocale = userLocale; + this.view.updateText(); + } + private List> settingsFromFile(Path settingsFile) { try (Stream lines = Files.lines(settingsFile)) { return lines.map(Presenter::withoutComments) diff --git a/src/main/java/sevenUnitsGUI/TabbedView.java b/src/main/java/sevenUnitsGUI/TabbedView.java index 6542541..493fc10 100644 --- a/src/main/java/sevenUnitsGUI/TabbedView.java +++ b/src/main/java/sevenUnitsGUI/TabbedView.java @@ -827,4 +827,9 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView { this.presenter.setNumberDisplayRule(roundingRule); this.presenter.saveSettings(); } + + @Override + public void updateText() { + // TODO Auto-generated method stub + } } diff --git a/src/main/java/sevenUnitsGUI/View.java b/src/main/java/sevenUnitsGUI/View.java index 7dd0c44..4140992 100644 --- a/src/main/java/sevenUnitsGUI/View.java +++ b/src/main/java/sevenUnitsGUI/View.java @@ -112,4 +112,11 @@ public interface View { */ void showUnit(NameSymbol name, String definition, String dimensionName, UnitType type); + + /** + * Updates the view's text to reflect the presenter's locale. + * + * This method must not call {@link Presenter#setUserLocale(String)}. + */ + void updateText(); } diff --git a/src/main/java/sevenUnitsGUI/ViewBot.java b/src/main/java/sevenUnitsGUI/ViewBot.java index e6593fb..8fff46d 100644 --- a/src/main/java/sevenUnitsGUI/ViewBot.java +++ b/src/main/java/sevenUnitsGUI/ViewBot.java @@ -505,4 +505,9 @@ public final class ViewBot public List unitViewList() { return Collections.unmodifiableList(this.unitViewingRecords); } + + @Override + public void updateText() { + // do nothing, since ViewBot is not localized + } } -- cgit v1.2.3