Skip to content

Commit

Permalink
refactor: separation of translation functionality into the Translatio…
Browse files Browse the repository at this point in the history
…n class (#2108)

Co-authored-by: Sebastian Reddig <[email protected]>
  • Loading branch information
sgcr and sgcr authored Aug 15, 2024
1 parent 058edd8 commit 381835e
Show file tree
Hide file tree
Showing 56 changed files with 275 additions and 155 deletions.
80 changes: 40 additions & 40 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -137,43 +137,43 @@ poeditor {
apiKey = System.getenv('POEDITOR_APIKEY') ?: 'xxx'
projectId = '206221'

trans.add type:'properties', lang: 'bg', file: "${projectDir}/src/main/resources/sprache/Bulgarian.properties"
trans.add type:'properties', lang: 'ca', file: "${projectDir}/src/main/resources/sprache/Catalan.properties"
trans.add type:'properties', lang: 'hr', file: "${projectDir}/src/main/resources/sprache/Hrvatski(Croatian).properties"
trans.add type:'properties', lang: 'cs', file: "${projectDir}/src/main/resources/sprache/Czech.properties"
trans.add type:'properties', lang: 'da', file: "${projectDir}/src/main/resources/sprache/Danish.properties"
trans.add type:'properties', lang: 'nl', file: "${projectDir}/src/main/resources/sprache/Nederlands.properties"
trans.add type:'properties', lang: 'fi', file: "${projectDir}/src/main/resources/sprache/Finnish.properties"
trans.add type:'properties', lang: 'fr', file: "${projectDir}/src/main/resources/sprache/French.properties"
trans.add type:'properties', lang: 'de', file: "${projectDir}/src/main/resources/sprache/Deutsch.properties"
trans.add type:'properties', lang: 'el', file: "${projectDir}/src/main/resources/sprache/Greeklish.properties"
trans.add type:'properties', lang: 'he', file: "${projectDir}/src/main/resources/sprache/Hebrew.properties" //Hebrew
trans.add type:'properties', lang: 'hu', file: "${projectDir}/src/main/resources/sprache/Magyar.properties" //Hungarian
trans.add type:'properties', lang: 'it', file: "${projectDir}/src/main/resources/sprache/Italiano.properties"
trans.add type:'properties', lang: 'ko', file: "${projectDir}/src/main/resources/sprache/Hangul(Korean).properties"
trans.add type:'properties', lang: 'lv', file: "${projectDir}/src/main/resources/sprache/Latvija.properties" //Lettonie
trans.add type:'properties', lang: 'lt', file: "${projectDir}/src/main/resources/sprache/Lithuanian.properties"
trans.add type:'properties', lang: 'fa', file: "${projectDir}/src/main/resources/sprache/Persian.properties"
trans.add type:'properties', lang: 'pl', file: "${projectDir}/src/main/resources/sprache/Polish.properties"
trans.add type:'properties', lang: 'pt', file: "${projectDir}/src/main/resources/sprache/Portugues.properties"
trans.add type:'properties', lang: 'pt-br', file: "${projectDir}/src/main/resources/sprache/PortuguesBrasil.properties"
trans.add type:'properties', lang: 'ro', file: "${projectDir}/src/main/resources/sprache/Romanian.properties"
trans.add type:'properties', lang: 'ru', file: "${projectDir}/src/main/resources/sprache/Russian.properties"
trans.add type:'properties', lang: 'sr-cyrl', file: "${projectDir}/src/main/resources/sprache/Serbian(Cyrillic).properties"
trans.add type:'properties', lang: 'sk', file: "${projectDir}/src/main/resources/sprache/Slovak.properties" //Slovak
trans.add type:'properties', lang: 'es', file: "${projectDir}/src/main/resources/sprache/Spanish.properties"
trans.add type:'properties', lang: 'sv', file: "${projectDir}/src/main/resources/sprache/Svenska.properties" //Swedish
trans.add type:'properties', lang: 'tr', file: "${projectDir}/src/main/resources/sprache/Turkish.properties"
trans.add type:'properties', lang: 'no', file: "${projectDir}/src/main/resources/sprache/Norsk.properties" // Norwegian
trans.add type:'properties', lang: 'id', file: "${projectDir}/src/main/resources/sprache/Indonesian.properties"
trans.add type:'properties', lang: 'ja', file: "${projectDir}/src/main/resources/sprache/Japanese.properties"
trans.add type:'properties', lang: 'sl', file: "${projectDir}/src/main/resources/sprache/Slovenian.properties"
trans.add type:'properties', lang: 'uk', file: "${projectDir}/src/main/resources/sprache/Ukranian.properties"
trans.add type:'properties', lang: 'ka', file: "${projectDir}/src/main/resources/sprache/Georgian.properties"
trans.add type:'properties', lang: 'zh-CN', file: "${projectDir}/src/main/resources/sprache/Chinese.properties"
trans.add type:'properties', lang: 'gl', file: "${projectDir}/src/main/resources/sprache/Galego.properties" // Galician
trans.add type:'properties', lang: 'nl-be', file: "${projectDir}/src/main/resources/sprache/Vlaams.properties" // Flemish
trans.add type:'properties', lang: 'es-ar', file: "${projectDir}/src/main/resources/sprache/Spanish(AR).properties" // Argentina
trans.add type:'properties', lang: 'bg', file: "${projectDir}/src/main/resources/language/Bulgarian.properties"
trans.add type:'properties', lang: 'ca', file: "${projectDir}/src/main/resources/language/Catalan.properties"
trans.add type:'properties', lang: 'hr', file: "${projectDir}/src/main/resources/language/Hrvatski(Croatian).properties"
trans.add type:'properties', lang: 'cs', file: "${projectDir}/src/main/resources/language/Czech.properties"
trans.add type:'properties', lang: 'da', file: "${projectDir}/src/main/resources/language/Danish.properties"
trans.add type:'properties', lang: 'nl', file: "${projectDir}/src/main/resources/language/Nederlands.properties"
trans.add type:'properties', lang: 'fi', file: "${projectDir}/src/main/resources/language/Finnish.properties"
trans.add type:'properties', lang: 'fr', file: "${projectDir}/src/main/resources/language/French.properties"
trans.add type:'properties', lang: 'de', file: "${projectDir}/src/main/resources/language/German.properties"
trans.add type:'properties', lang: 'el', file: "${projectDir}/src/main/resources/language/Greeklish.properties"
trans.add type:'properties', lang: 'he', file: "${projectDir}/src/main/resources/language/Hebrew.properties" //Hebrew
trans.add type:'properties', lang: 'hu', file: "${projectDir}/src/main/resources/language/Magyar.properties" //Hungarian
trans.add type:'properties', lang: 'it', file: "${projectDir}/src/main/resources/language/Italiano.properties"
trans.add type:'properties', lang: 'ko', file: "${projectDir}/src/main/resources/language/Hangul(Korean).properties"
trans.add type:'properties', lang: 'lv', file: "${projectDir}/src/main/resources/language/Latvija.properties" //Lettonie
trans.add type:'properties', lang: 'lt', file: "${projectDir}/src/main/resources/language/Lithuanian.properties"
trans.add type:'properties', lang: 'fa', file: "${projectDir}/src/main/resources/language/Persian.properties"
trans.add type:'properties', lang: 'pl', file: "${projectDir}/src/main/resources/language/Polish.properties"
trans.add type:'properties', lang: 'pt', file: "${projectDir}/src/main/resources/language/Portugues.properties"
trans.add type:'properties', lang: 'pt-br', file: "${projectDir}/src/main/resources/language/PortuguesBrasil.properties"
trans.add type:'properties', lang: 'ro', file: "${projectDir}/src/main/resources/language/Romanian.properties"
trans.add type:'properties', lang: 'ru', file: "${projectDir}/src/main/resources/language/Russian.properties"
trans.add type:'properties', lang: 'sr-cyrl', file: "${projectDir}/src/main/resources/language/Serbian(Cyrillic).properties"
trans.add type:'properties', lang: 'sk', file: "${projectDir}/src/main/resources/language/Slovak.properties" //Slovak
trans.add type:'properties', lang: 'es', file: "${projectDir}/src/main/resources/language/Spanish.properties"
trans.add type:'properties', lang: 'sv', file: "${projectDir}/src/main/resources/language/Svenska.properties" //Swedish
trans.add type:'properties', lang: 'tr', file: "${projectDir}/src/main/resources/language/Turkish.properties"
trans.add type:'properties', lang: 'no', file: "${projectDir}/src/main/resources/language/Norsk.properties" // Norwegian
trans.add type:'properties', lang: 'id', file: "${projectDir}/src/main/resources/language/Indonesian.properties"
trans.add type:'properties', lang: 'ja', file: "${projectDir}/src/main/resources/language/Japanese.properties"
trans.add type:'properties', lang: 'sl', file: "${projectDir}/src/main/resources/language/Slovenian.properties"
trans.add type:'properties', lang: 'uk', file: "${projectDir}/src/main/resources/language/Ukranian.properties"
trans.add type:'properties', lang: 'ka', file: "${projectDir}/src/main/resources/language/Georgian.properties"
trans.add type:'properties', lang: 'zh-CN', file: "${projectDir}/src/main/resources/language/Chinese.properties"
trans.add type:'properties', lang: 'gl', file: "${projectDir}/src/main/resources/language/Galego.properties" // Galician
trans.add type:'properties', lang: 'nl-be', file: "${projectDir}/src/main/resources/language/Vlaams.properties" // Flemish
trans.add type:'properties', lang: 'es-ar', file: "${projectDir}/src/main/resources/language/Spanish(AR).properties" // Argentina
}


Expand Down Expand Up @@ -463,15 +463,15 @@ tasks.register("createLanguageFileList") {
outputs.upToDateWhen { false }

def lTranslationFiles = []
fileTree(dir: "${projectDir}/src/main/resources/sprache", include: '*.properties').visit {
fileTree(dir: "${projectDir}/src/main/resources/language", include: '*.properties').visit {
FileVisitDetails details -> lTranslationFiles << details.file.name
}

doLast {
println("listing available translation in ${projectDir}/src/main/resources/sprache")
println("listing available translation in ${projectDir}/src/main/resources/language")
lTranslationFiles = lTranslationFiles.collect { it.take(it.lastIndexOf('.'))}
lTranslationFiles.each {out.println it}
File lstFile = new File("${projectDir}/src/main/resources/sprache/ListLanguages.txt")
File lstFile = new File("${projectDir}/src/main/resources/language/ListLanguages.txt")
lstFile.withWriter{ out -> lTranslationFiles.each {out.println it} }
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/core/HO.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ public static void main(String[] args) {
}

// Check if language file available
interruptionWindow.setInfoText(4, "Check Languagefiles");
interruptionWindow.setInfoText(4, "Check Language files");
HOVerwaltung.checkLanguageFile(UserParameter.instance().sprachDatei);
HOVerwaltung.instance().setResource(UserParameter.instance().sprachDatei);
HOVerwaltung.instance().setTranslator(UserParameter.instance().sprachDatei);

if (DBManager.instance().isFirstStart()) {
interruptionWindow.setVisible(false);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/core/file/xml/XMLMatchOrderParserNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private MatchRoleID getPosition(Node playerNode) {
* @param args
*/
public static void main(String[] args) {
HOVerwaltung.instance().setResource(UserParameter.instance().sprachDatei);
HOVerwaltung.instance().setTranslator(UserParameter.instance().sprachDatei);
File file = new File(
"/home/chr/tmp/matchorders_version_1_8_matchID_353869167_isYouth_false.xml");
new XMLMatchOrderParserNew(XMLManager.parseFile(file));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/core/gui/language/LanguageComboBoxModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void characters(char ch[], int start, int length) throws SAXException {

};

URL languages = this.getClass().getClassLoader().getResource("sprache/languages.xml");
URL languages = this.getClass().getClassLoader().getResource("language/languages.xml");
saxParser.parse(languages.getPath(), handler);

} catch (Exception e) {
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/core/gui/language/LanguageTableModel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package core.gui.language;

import core.model.Translator;
import core.util.HOLogger;
import core.util.UTF8Control;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
Expand All @@ -19,11 +22,6 @@
import java.util.Map;
import java.util.ResourceBundle;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
* This class represents the table model for editing language resource files.
* @author edswifa
Expand All @@ -46,7 +44,7 @@ public LanguageTableModel() {

LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();

URL englishPath = this.getClass().getClassLoader().getResource("sprache/English.properties");
URL englishPath = this.getClass().getClassLoader().getResource("language/English.properties");

FileInputStream fis = null;
try {
Expand Down Expand Up @@ -88,8 +86,8 @@ public LanguageTableModel(String languageName) {
this.isDestinationFile = true;
this.langauageName = languageName;

ResourceBundle englishBundle = ResourceBundle.getBundle("sprache.English", new UTF8Control());
ResourceBundle destBundle = ResourceBundle.getBundle("sprache." + languageName, new UTF8Control());
ResourceBundle englishBundle = Translator.loadDefault().getResourceBundle();
ResourceBundle destBundle = Translator.load(languageName).getResourceBundle();
Iterator<String> rbKeys = this.keys.iterator();
String value = null;

Expand Down Expand Up @@ -158,7 +156,7 @@ public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
* Save the table model back to a properties file
*/
public void save() {
StringBuilder fileName = new StringBuilder("sprache/");
StringBuilder fileName = new StringBuilder("language/");
fileName.append(this.langauageName);
fileName.append(".properties");
URL destinationPath = this.getClass().getClassLoader().getResource(fileName.toString());
Expand Down
118 changes: 35 additions & 83 deletions src/main/java/core/model/HOVerwaltung.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package core.model;


import core.db.DBManager;
import core.file.FileLoader;
import core.file.hrf.HRF;
import core.gui.HOMainFrame;
import core.gui.RefreshManager;
import core.util.HODateTime;
import core.util.HOLogger;
import core.util.Languages;
import core.util.UTF8Control;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.InputStream;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
import java.util.Optional;
import java.util.ResourceBundle;

public class HOVerwaltung {
Expand All @@ -29,9 +26,7 @@ public class HOVerwaltung {
/** das Model */
protected HOModel m_clHoModel;

/** Resource */
protected ResourceBundle languageBundle;
protected Locale m_locale;
private Translator translator = null;

public static boolean isNewModel(HOModel homodel) {
return (homodel != null && ((instance().getModel() == null) ||
Expand Down Expand Up @@ -87,23 +82,28 @@ public static HOVerwaltung instance() {
return m_clInstance;
}

/**
* @deprecated Provided for compatibility. Please use {@link #setTranslator(String)} instead.
*/
@Deprecated(since = "9.0", forRemoval = true)
public void setResource(String pfad) {
setTranslator(pfad);
}

public void setTranslator(String language) {
try {
languageBundle = ResourceBundle.getBundle("sprache." + pfad, new UTF8Control());
m_locale = Languages.lookup(UserParameter.instance().sprachDatei).getLocale();
}
catch (UnsupportedOperationException e) {
// ResourceBundle.Control not supported in named modules in JDK9+
languageBundle = ResourceBundle.getBundle("sprache." + pfad);
m_locale = Languages.lookup(UserParameter.instance().sprachDatei).getLocale();
} catch (Exception e) {
translator = Translator.load(language);
} catch (RuntimeException e) {
HOLogger.instance().log(getClass(), e);
}

}

/**
* @deprecated Provided for compatibility. Will be removed without substitution!
*/
@Deprecated(since = "9.0", forRemoval = true)
public ResourceBundle getResource() {
return languageBundle;
return Optional.ofNullable(translator).map(Translator::getResourceBundle).orElse(null);
}

/**
Expand Down Expand Up @@ -205,33 +205,7 @@ protected HOModel loadModel(int id) {
* language files
*/
public String getLanguageString(String key) {
String temp = null;
try {
if (languageBundle != null) {
temp = languageBundle.getString(key);
}
} catch (Exception e) {
// Do nothing, it just throws error if key is missing.
}
if (temp != null)
return temp;
// Search in english.properties if nothing found and active language not
// english
if (!UserParameter.instance().sprachDatei.equalsIgnoreCase("english")) {
ResourceBundle tempBundle = ResourceBundle.getBundle("sprache.English", new UTF8Control());

try {
temp = tempBundle.getString(key);
} catch (Exception e) {
// Ignore
}

if (temp != null)
return temp;
}

HOLogger.instance().warning(getClass(), "getLanguageString: '" + key + "' not found!");
return "!" + key + "!";
return translator.translate(key);
}

/**
Expand All @@ -245,59 +219,37 @@ public String getLanguageString(String key) {
* replaced by the given value(s).
*/
public String getLanguageString(String key, Object... values) {

String str = getLanguageString(key);

MessageFormat formatter = new MessageFormat("");
formatter.setLocale(m_locale);
formatter.applyPattern(str);

return formatter.format(values);
}

public static String[] getLanguageFileNames() {
String[] files = null;

try {
java.io.InputStream is = HOVerwaltung.class.getClassLoader().getResourceAsStream("sprache/ListLanguages.txt");
assert is != null;
java.util.Scanner s = new java.util.Scanner(is);
java.util.ArrayList<String> llist = new java.util.ArrayList<>();
while (s.hasNext()){
llist.add(s.next());
}
s.close();

files = llist.toArray(new String[0]);

} catch (Exception e) {
HOLogger.instance().log(HOVerwaltung.class, e);
}

return files;
return translator.translate(key, values);
}

/**
* Checked die Sprachdatei oder Fragt nach einer passenden
*/
public static void checkLanguageFile(String languageFilename) {
try {
final InputStream translationFile = FileLoader.instance().getFileInputStream("sprache/" + languageFilename + ".properties");
if (translationFile != null) {
if (Translator.isAvailable(languageFilename)) {
HOLogger.instance().info(HOVerwaltung.class, "language used for interface is: " + languageFilename);
}
else{
HOLogger.instance().error(HOVerwaltung.class, "language set for interface (" + languageFilename +") can't be loaded ... reverting to English !");
UserParameter.instance().sprachDatei = "English";
HOLogger.instance().error(HOVerwaltung.class, "language set for interface (" + languageFilename + ") can't be loaded ... reverting to " + Translator.LANGUAGE_DEFAULT + " !");
UserParameter.instance().sprachDatei = Translator.LANGUAGE_DEFAULT;
}
}
catch (Exception e) {
HOLogger.instance().error(HOVerwaltung.class, "language set for interface (" + languageFilename +") can't be loaded ... reverting to English !" + " " + e);
UserParameter.instance().sprachDatei = "English";
HOLogger.instance().error(HOVerwaltung.class, "language set for interface (" + languageFilename + ") can't be loaded ... reverting to " + Translator.LANGUAGE_DEFAULT + " !" + " " + e);
UserParameter.instance().sprachDatei = Translator.LANGUAGE_DEFAULT;
}
}

public void setLanguageBundle(ResourceBundle bundle) {
this.languageBundle = bundle;
/**
* @deprecated Provided for compatibility. Please use {@link #clearTranslator()} instead.
*/
@Deprecated(since = "9.0", forRemoval = true)
public void clearLanguageBundle() {
clearTranslator();
}

public void clearTranslator() {
translator = null;
}
}
Loading

0 comments on commit 381835e

Please sign in to comment.