Skip to content

Commit ab0dc58

Browse files
committed
fix(LanguageManager): show the list of languages on JAR files
1 parent a0c329d commit ab0dc58

File tree

9 files changed

+138
-156
lines changed

9 files changed

+138
-156
lines changed

src/main/java/com/dinosaur/dinosaurexploder/controller/DinosaurController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.almasb.fxgl.entity.Entity;
55
import com.dinosaur.dinosaurexploder.model.*;
66
import com.dinosaur.dinosaurexploder.view.DinosaurGUI;
7-
import com.dinosaur.dinosaurexploder.view.LanguageManager;
7+
import com.dinosaur.dinosaurexploder.model.LanguageManager;
88
import javafx.event.ActionEvent;
99
import javafx.event.EventHandler;
1010
import javafx.scene.control.Button;

src/main/java/com/dinosaur/dinosaurexploder/model/BombComponent.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.dinosaur.dinosaurexploder.model;
22

33
import com.almasb.fxgl.core.math.Vec2;
4-
import com.almasb.fxgl.dsl.FXGL;
54
import com.almasb.fxgl.entity.Entity;
65
import com.almasb.fxgl.entity.SpawnData;
76
import com.almasb.fxgl.entity.component.Component;
87
import com.dinosaur.dinosaurexploder.utils.GameData;
9-
import com.dinosaur.dinosaurexploder.view.LanguageManager;
108

119
import javafx.geometry.Point2D;
1210
import javafx.scene.Node;
@@ -17,9 +15,6 @@
1715
import javafx.scene.text.Font;
1816
import javafx.scene.text.Text;
1917

20-
import java.util.List;
21-
22-
import static com.almasb.fxgl.dsl.FXGL.getLocalizationService;
2318
import static com.almasb.fxgl.dsl.FXGLForKtKt.spawn;
2419

2520
public class BombComponent extends Component implements Bomb {
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.dinosaur.dinosaurexploder.model;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import javafx.beans.property.SimpleStringProperty;
5+
import javafx.beans.property.StringProperty;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.net.URISyntaxException;
10+
import java.util.*;
11+
import java.util.jar.JarEntry;
12+
import java.util.jar.JarFile;
13+
14+
public class LanguageManager {
15+
private final StringProperty selectedLanguage = new SimpleStringProperty("English");
16+
private final String TRANSLATION_PATH = "/assets/translation/";
17+
private Map<String, String> translations = new HashMap<>();
18+
19+
private static LanguageManager instance;
20+
21+
// Private constructor to prevent instantiation
22+
private LanguageManager() {}
23+
24+
// Get the singleton instance
25+
public static synchronized LanguageManager getInstance() {
26+
if (instance == null) {
27+
instance = new LanguageManager();
28+
instance.setSelectedLanguage("English");
29+
}
30+
return instance;
31+
}
32+
33+
// Setter for selected language, loads the respective translations
34+
public void setSelectedLanguage(String language) {
35+
translations = loadTranslations(language);
36+
selectedLanguage.set(language);
37+
}
38+
39+
// Getter for selected language
40+
public String getSelectedLanguage() {
41+
return selectedLanguage.get();
42+
}
43+
44+
public StringProperty selectedLanguageProperty() {
45+
return selectedLanguage;
46+
}
47+
48+
// Main method to fetch all available languages
49+
public List<String> getAvailableLanguages() {
50+
List<String> languages = new ArrayList<>();
51+
52+
// Try loading languages from resources or JAR file based on environment
53+
if (isRunningInsideJar()) {
54+
languages = loadLanguagesFromJar();
55+
} else {
56+
languages = loadLanguagesFromResources();
57+
}
58+
59+
System.out.println("Available Languages: " + languages);
60+
return languages;
61+
}
62+
63+
// Check if the application is running inside a JAR
64+
private boolean isRunningInsideJar() {
65+
return getClass().getClassLoader().getResourceAsStream("assets/translation/") == null;
66+
}
67+
68+
// Load language files from the JAR (for packaged applications)
69+
private List<String> loadLanguagesFromJar() {
70+
List<String> languages = new ArrayList<>();
71+
try {
72+
String jarPath = LanguageManager.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
73+
try (JarFile jarFile = new JarFile(jarPath)) {
74+
jarFile.stream()
75+
.map(JarEntry::getName)
76+
.filter(name -> name.startsWith("assets/translation/") && name.endsWith(".json"))
77+
.forEach(name -> languages.add(extractLanguageName(name)));
78+
}
79+
} catch (IOException e) {
80+
System.err.println("Error reading languages from JAR: " + e.getMessage());
81+
} catch (URISyntaxException e) {
82+
throw new RuntimeException(e);
83+
}
84+
return languages;
85+
}
86+
87+
// Extract language name from file path (e.g. "assets/translation/english.json" => "English")
88+
private String extractLanguageName(String path) {
89+
String lang = path.substring("assets/translation/".length(), path.length() - 5); // Remove "assets/translation/" and ".json"
90+
return capitalizeFirstLetter(lang);
91+
}
92+
93+
// Capitalize the first letter of a string
94+
private String capitalizeFirstLetter(String text) {
95+
return text.substring(0, 1).toUpperCase() + text.substring(1);
96+
}
97+
98+
// Load language files from the resources folder (for non-JAR environment)
99+
private List<String> loadLanguagesFromResources() {
100+
List<String> languages = new ArrayList<>();
101+
String[] availableLanguages = {"english", "french", "german", "spanish", "japanese", "russian"};
102+
for (String lang : availableLanguages) {
103+
String filePath = TRANSLATION_PATH + lang + ".json";
104+
if (getClass().getResourceAsStream(filePath) != null) {
105+
languages.add(capitalizeFirstLetter(lang));
106+
}
107+
}
108+
return languages;
109+
}
110+
111+
// Load the translations for the selected language
112+
public Map<String, String> loadTranslations(String language) {
113+
String filePath = TRANSLATION_PATH + language.toLowerCase() + ".json";
114+
try (InputStream inputStream = getClass().getResourceAsStream(filePath)) {
115+
if (inputStream == null) {
116+
throw new RuntimeException("Translation file not found: " + filePath);
117+
}
118+
119+
ObjectMapper objectMapper = new ObjectMapper();
120+
return objectMapper.readValue(inputStream, Map.class);
121+
} catch (IOException e) {
122+
e.printStackTrace();
123+
return Collections.emptyMap();
124+
}
125+
}
126+
127+
// Get a translated string by key
128+
public String getTranslation(String key) {
129+
return translations.getOrDefault(key, key); // Default to key if translation not found
130+
}
131+
}

src/main/java/com/dinosaur/dinosaurexploder/model/LifeComponent.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.dinosaur.dinosaurexploder.model;
22

3-
import static com.almasb.fxgl.dsl.FXGL.getLocalizationService;
4-
53
import com.almasb.fxgl.entity.component.Component;
6-
import com.dinosaur.dinosaurexploder.view.LanguageManager;
74
import javafx.scene.Node;
85
import javafx.scene.image.Image;
96
import javafx.scene.image.ImageView;

src/main/java/com/dinosaur/dinosaurexploder/model/ScoreComponent.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package com.dinosaur.dinosaurexploder.model;
22

33

4-
import static com.almasb.fxgl.dsl.FXGL.getLocalizationService;
5-
64
import java.io.FileInputStream;
75
import java.io.FileOutputStream;
86
import java.io.IOException;
97
import java.io.ObjectInputStream;
108
import java.io.ObjectOutputStream;
119

1210
import com.almasb.fxgl.entity.component.Component;
13-
import com.dinosaur.dinosaurexploder.view.LanguageManager;
1411
import javafx.scene.image.Image;
1512
import javafx.scene.image.ImageView;
1613
import javafx.scene.layout.GridPane;

src/main/java/com/dinosaur/dinosaurexploder/view/DinosaurMenu.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import com.almasb.fxgl.dsl.FXGL;
77
import com.almasb.fxgl.scene.Scene;
88
import com.almasb.fxgl.ui.FontType;
9-
import com.dinosaur.dinosaurexploder.DinosaurApp;
109
import com.dinosaur.dinosaurexploder.model.GameConstants;
1110

11+
import com.dinosaur.dinosaurexploder.model.LanguageManager;
1212
import javafx.animation.Interpolator;
1313
import javafx.animation.TranslateTransition;
1414
import javafx.beans.value.ChangeListener;
@@ -31,20 +31,13 @@
3131

3232
import javafx.util.Duration;
3333
import java.io.InputStream;
34-
import javafx.beans.value.ChangeListener;
35-
import javafx.beans.value.ObservableValue;
36-
import javafx.geometry.Pos;
37-
import javafx.scene.control.Label;
38-
import javafx.scene.control.Slider;
39-
import javafx.scene.layout.BorderPane;
34+
4035
import javafx.scene.layout.HBox;
41-
import javafx.scene.layout.Region;
36+
4237
import java.io.FileNotFoundException;
43-
import java.io.FileReader;
44-
import java.util.List;
4538
import java.util.Map;
4639
import java.util.Objects;
47-
import javafx.scene.layout.StackPane;
40+
4841
import static com.almasb.fxgl.dsl.FXGL.getLocalizationService;
4942

5043
public class DinosaurMenu extends FXGLMenu {

src/main/java/com/dinosaur/dinosaurexploder/view/LanguageManager.java

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/main/java/com/dinosaur/dinosaurexploder/view/PauseMenu.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.almasb.fxgl.app.scene.FXGLMenu;
44
import com.almasb.fxgl.app.scene.MenuType;
55
import com.almasb.fxgl.dsl.FXGL;
6-
import com.almasb.fxgl.localization.Language;
76
import com.almasb.fxgl.ui.FontType;
7+
import com.dinosaur.dinosaurexploder.model.LanguageManager;
88
import javafx.beans.binding.Bindings;
99
import javafx.event.ActionEvent;
1010
import javafx.event.EventHandler;
@@ -19,11 +19,8 @@
1919
import static com.almasb.fxgl.dsl.FXGL.*;
2020
import static com.almasb.fxgl.dsl.FXGLForKtKt.getUIFactoryService;
2121

22-
import com.dinosaur.dinosaurexploder.DinosaurApp;
2322
import com.dinosaur.dinosaurexploder.model.GameConstants;
2423

25-
import java.util.Map;
26-
2724
public class PauseMenu extends FXGLMenu {
2825
LanguageManager languageManager = LanguageManager.getInstance();
2926
PauseButton btnBack = new PauseButton(languageManager.getTranslation("back"), () -> fireResume());

src/main/java/com/dinosaur/dinosaurexploder/view/ShipSelectionMenu.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.almasb.fxgl.dsl.FXGL;
66
import com.almasb.fxgl.ui.FontType;
77

8+
import com.dinosaur.dinosaurexploder.model.LanguageManager;
89
import javafx.animation.Interpolator;
910
import javafx.animation.TranslateTransition;
1011
import javafx.geometry.Pos;

0 commit comments

Comments
 (0)