Add persistent language selection with ConfigService integration
This commit is contained in:
parent
04c0315f3e
commit
916fa07418
3 changed files with 86 additions and 3 deletions
|
|
@ -19,6 +19,7 @@ import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import client.utils.LocaleManager;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
|
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
|
|
@ -38,7 +39,8 @@ public class MyFXML {
|
||||||
|
|
||||||
public <T> Pair<T, Parent> load(Class<T> c, String... parts) {
|
public <T> Pair<T, Parent> load(Class<T> c, String... parts) {
|
||||||
try {
|
try {
|
||||||
var loader = new FXMLLoader(getLocation(parts), null, null, new MyFactory(), StandardCharsets.UTF_8);
|
var localeManager = injector.getInstance(LocaleManager.class);
|
||||||
|
var loader = new FXMLLoader(getLocation(parts), localeManager.getBundle(), null, new MyFactory(), StandardCharsets.UTF_8);
|
||||||
Parent parent = loader.load();
|
Parent parent = loader.load();
|
||||||
T ctrl = loader.getController();
|
T ctrl = loader.getController();
|
||||||
return new Pair<>(ctrl, parent);
|
return new Pair<>(ctrl, parent);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package client.utils;
|
package client.utils;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
import javafx.beans.property.ObjectProperty;
|
import javafx.beans.property.ObjectProperty;
|
||||||
import javafx.beans.property.SimpleObjectProperty;
|
import javafx.beans.property.SimpleObjectProperty;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
@ -10,13 +11,30 @@ public class LocaleManager {
|
||||||
private final ObjectProperty<ResourceBundle> currentBundle = new SimpleObjectProperty<>();
|
private final ObjectProperty<ResourceBundle> currentBundle = new SimpleObjectProperty<>();
|
||||||
|
|
||||||
private static final String RESOURCE_BUNDLE_PATH = "locale/lang";
|
private static final String RESOURCE_BUNDLE_PATH = "locale/lang";
|
||||||
|
private final ConfigService configService;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public LocaleManager(ConfigService configService) {
|
||||||
|
this.configService = configService;
|
||||||
|
String savedLanguage = configService.getConfig().getLanguage();
|
||||||
|
Locale initialLocale = Locale.forLanguageTag(savedLanguage);
|
||||||
|
currentLocale.set(initialLocale);
|
||||||
|
|
||||||
public LocaleManager() {
|
|
||||||
// TODO: Set currentLocale to config value instead of EN default.
|
|
||||||
updateBundle();
|
updateBundle();
|
||||||
|
|
||||||
|
currentLocale.addListener((_, oldLocale, newLocale) -> {
|
||||||
|
updateBundle();
|
||||||
|
saveLanguagePreference(newLocale.getLanguage());
|
||||||
|
});
|
||||||
|
|
||||||
currentLocale.addListener((_, _, _) -> updateBundle());
|
currentLocale.addListener((_, _, _) -> updateBundle());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void saveLanguagePreference(String languageCode) {
|
||||||
|
configService.getConfig().setLanguage(languageCode);
|
||||||
|
configService.save();
|
||||||
|
}
|
||||||
|
|
||||||
private void updateBundle() {
|
private void updateBundle() {
|
||||||
ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_PATH,
|
ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_PATH,
|
||||||
currentLocale.get(),
|
currentLocale.get(),
|
||||||
|
|
@ -44,4 +62,6 @@ public class LocaleManager {
|
||||||
public ObjectProperty<ResourceBundle> getBundleProperty() {
|
public ObjectProperty<ResourceBundle> getBundleProperty() {
|
||||||
return currentBundle;
|
return currentBundle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
61
client/src/test/java/client/scenes/LocaleManagerTest.java
Normal file
61
client/src/test/java/client/scenes/LocaleManagerTest.java
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
package client.scenes;
|
||||||
|
|
||||||
|
import client.utils.ConfigService;
|
||||||
|
import client.utils.LocaleManager;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
public class LocaleManagerTest {
|
||||||
|
|
||||||
|
@TempDir
|
||||||
|
Path tempDir;
|
||||||
|
|
||||||
|
private ConfigService configService;
|
||||||
|
private LocaleManager localeManager;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
Path configPath = tempDir.resolve("test-config.json");
|
||||||
|
configService = new ConfigService(configPath);
|
||||||
|
localeManager = new LocaleManager(configService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSetLocaleChangesCurrentLocale() {
|
||||||
|
localeManager.setLocale(Locale.forLanguageTag("nl"));
|
||||||
|
assertEquals("nl", localeManager.getLocale().getLanguage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSetLocaleSavesToConfig() {
|
||||||
|
localeManager.setLocale(Locale.forLanguageTag("pl"));
|
||||||
|
assertEquals("pl", configService.getConfig().getLanguage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLocalePersistsAcrossInstances() {
|
||||||
|
localeManager.setLocale(Locale.forLanguageTag("en"));
|
||||||
|
LocaleManager newLocaleManager = new LocaleManager(configService);
|
||||||
|
assertEquals("en", newLocaleManager.getLocale().getLanguage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBundleIsNotNull() {
|
||||||
|
assertNotNull(localeManager.getBundle());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBundlePropertyIsNotNull() {
|
||||||
|
assertNotNull(localeManager.getBundleProperty());
|
||||||
|
assertNotNull(localeManager.getBundleProperty().get());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue