From 53d2546455d094c7e165a004ed41f0be9a607a23 Mon Sep 17 00:00:00 2001 From: kelvencassamo Date: Fri, 13 Dec 2024 17:40:24 +0200 Subject: [PATCH 1/5] Creating xml-builder --- src/mz/cassamo/jls/Builder.java | 170 ++++++++++++++++++++++++++++++++ src/mz/cassamo/jls/Info.java | 11 +++ 2 files changed, 181 insertions(+) create mode 100644 src/mz/cassamo/jls/Builder.java create mode 100644 src/mz/cassamo/jls/Info.java diff --git a/src/mz/cassamo/jls/Builder.java b/src/mz/cassamo/jls/Builder.java new file mode 100644 index 0000000..cfc9fe3 --- /dev/null +++ b/src/mz/cassamo/jls/Builder.java @@ -0,0 +1,170 @@ +package mz.cassamo.jls; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +/** + * + * @author cassamo + */ +class Builder { + + private final Map> translations = new HashMap<>(); +private String filePath = null; + public Builder() { + } + + + public void loadFromFile(String filePath) { + this.filePath = filePath; + try { + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser saxParser = factory.newSAXParser(); + LanguageHandler handler = new LanguageHandler(); + + saxParser.parse(new File(filePath), handler); + mergeTranslations(handler.getLanguages()); + } catch (Exception e) { + + } + } + + + public void loadFromResources(String resourcePath, Class resourceClass) { + try (InputStream inputStream = resourceClass.getResourceAsStream(resourcePath)) { + if (inputStream == null) { + throw new IllegalArgumentException("Resource not found: " + resourcePath); + } + + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser saxParser = factory.newSAXParser(); + LanguageHandler handler = new LanguageHandler(); + + saxParser.parse(inputStream, handler); + mergeTranslations(handler.getLanguages()); + } catch (Exception e) { + + } + } + + + public void putLanguage(String language) { + translations.putIfAbsent(language, new HashMap<>()); + normalizeTranslations(); + } + + + public void removeLanguage(String language) { + translations.remove(language); + } + + + public void putTranslation(String language, String key, String value) { + translations.putIfAbsent(language, new HashMap<>()); + translations.get(language).put(key, value); + normalizeTranslations(); + } + + + public void removeTranslation(String language, String key) { + if (translations.containsKey(language)) { + translations.get(language).remove(key); + } + normalizeTranslations(); + } + + + public void saveToFile(String path) { + try (FileWriter writer = new FileWriter(path)) { + writer.write(toXmlString()); + } catch (IOException e) { + if (LanguageSystem.isDebugMode()) { + + } + } + } + + public void save() { + try (FileWriter writer = new FileWriter(filePath)) { + writer.write(toXmlString()); + } catch (IOException e) { + if (LanguageSystem.isDebugMode()) { + e.printStackTrace(); + } + } + } + + public String toXmlString() { + StringBuilder xmlBuilder = new StringBuilder(); + xmlBuilder.append("\n".formatted(Info.LIB_NAME, Info.VERSION)); + xmlBuilder.append("\n"); + + xmlBuilder.append("\n"); + + for (Map.Entry> languageEntry : translations.entrySet()) { + String language = languageEntry.getKey(); + Map languageTranslations = languageEntry.getValue(); + + xmlBuilder.append(" \n"); + for (Map.Entry translationEntry : languageTranslations.entrySet()) { + String key = translationEntry.getKey(); + String value = translationEntry.getValue(); + + xmlBuilder.append(" \n"); + xmlBuilder.append(" ").append(escapeXml(value)).append("\n"); + xmlBuilder.append(" \n"); + } + xmlBuilder.append(" \n"); + } + + xmlBuilder.append(""); + return xmlBuilder.toString(); + } + +// Escapa caracteres especiais para XML + private String escapeXml(String value) { + if (value == null) { + return ""; + } + return value.replace("&", "&") + .replace("<", "<") + .replace(">", ">") + .replace("\"", """) + .replace("'", "'"); + } + + + private void mergeTranslations(Map> newTranslations) { + for (Map.Entry> entry : newTranslations.entrySet()) { + translations.putIfAbsent(entry.getKey(), new HashMap<>()); + translations.get(entry.getKey()).putAll(entry.getValue()); + } + normalizeTranslations(); + } + + + private void normalizeTranslations() { + Set allKeys = new HashSet<>(); + + + for (Map languageMap : translations.values()) { + allKeys.addAll(languageMap.keySet()); + } + + + for (Map.Entry> entry : translations.entrySet()) { + Map languageMap = entry.getValue(); + for (String key : allKeys) { + languageMap.putIfAbsent(key, ""); + } + } + } +} diff --git a/src/mz/cassamo/jls/Info.java b/src/mz/cassamo/jls/Info.java new file mode 100644 index 0000000..912da9c --- /dev/null +++ b/src/mz/cassamo/jls/Info.java @@ -0,0 +1,11 @@ +package mz.cassamo.jls; + +/** + * + * @author cassamo + */ +class Info { + + public static final String LIB_NAME = "JLS"; + public static final String VERSION = "1.0.0"; +} From 31f013f2e828e84d042afc6ab1df6ef9188f1518 Mon Sep 17 00:00:00 2001 From: kelvencassamo Date: Fri, 13 Dec 2024 17:48:37 +0200 Subject: [PATCH 2/5] Removing pom.xml --- pom.xml | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 pom.xml diff --git a/pom.xml b/pom.xml deleted file mode 100644 index 7bc8fc4..0000000 --- a/pom.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - 4.0.0 - - mz.cassamo.jls - jls - 1.0.0 - jar - - Java Language System - a Java library designed to facilitate internationalization (i18n) of applications, enabling dynamic language management and text translation, Swing and Widgets components on Android based on XML files. - https://github.com/KelvenCassamo/java-language-system - - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - - scm:git:https://github.com/KelvenCassamo/java-language-system.git - scm:git:https://github.com/KelvenCassamo/java-language-system.git - https://github.com/KelvenCassamo/java-language-system - - - - - - - From b9d77cfe55a459f36ae60877b919665b7c051058 Mon Sep 17 00:00:00 2001 From: kelvencassamo Date: Fri, 13 Dec 2024 18:07:04 +0200 Subject: [PATCH 3/5] Adding Builder to the LanguageSystem.java --- .gitignore | 4 ++ src/mz/cassamo/jls/LanguageSystem.java | 53 ++++++++++++++++++++++---- src/test/ExampleBuildingLS.java | 33 ++++++++++++++++ 3 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 src/test/ExampleBuildingLS.java diff --git a/.gitignore b/.gitignore index 688f674..5f4f0c8 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,7 @@ build.xml manifest.mf /lib/ /keystores/ +/bin/ +.project +.classpath +/bin/ diff --git a/src/mz/cassamo/jls/LanguageSystem.java b/src/mz/cassamo/jls/LanguageSystem.java index d72b4a2..57b2907 100644 --- a/src/mz/cassamo/jls/LanguageSystem.java +++ b/src/mz/cassamo/jls/LanguageSystem.java @@ -5,6 +5,7 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; +import java.util.Locale; /** * A system for managing and applying language translations across user @@ -27,7 +28,6 @@ */ public class LanguageSystem { - private static final ArrayList> appliedComponents = new ArrayList<>(); private static final ArrayList> appliedWidgets = new ArrayList<>(); private static boolean debug = false; @@ -99,10 +99,11 @@ public static ArrayList getLanguages() { } return langs; } - + /** * Gets a list of available translations for specific language. - *@param language the language to be used. + * + * @param language the language to be used. * @return a list of translations. */ public static ArrayList getTranslationKeys(String language) { @@ -127,6 +128,25 @@ public static void setCurrentLanguage(String language) { } } + /** + * Sets the current language of the system with a fallback to the default + * language if the specified language does not exist. + * + * @param language the desired language code. + * @param default_language the default language code to use as a fallback. + */ + public static void setCurrentLanguage(String language, String default_language) { + String lang = language; + if (!existsLanguage(language)) { + lang = default_language; + } + LanguageReader.setLanguage(lang); + autoInsertLanguage(); + if (li != null) { + li.onChange(language); + } + } + /** * Automatically translates a single component based on the provided * language key. @@ -149,17 +169,13 @@ public static void autoTranslateComponent(Component component, String language_k * @param widget the android widget to be translated. * @param language_key the language key for translation. */ - - /* public static void autoTranslateWidget(Object widget, String language_key) { + /* public static void autoTranslateWidget(Object widget, String language_key) { HashMap temp = new HashMap<>(); temp.put("widget", widget); temp.put("language_value", language_key); appliedWidgets.add(temp); autoInsertLanguage(); }*/ - - - /** * Automatically translates multiple components based on the provided * language key. @@ -337,4 +353,25 @@ public static boolean existsLanguage(String language) { public static boolean existsKey(String key) { return get(key, null) != null; } + + /** + * Retrieves the default system language based on the user's locale. + * + * @return the system's default language in English. + */ + public static String getDefaultSystemLanguage() { + Locale currentLocale = Locale.getDefault(); + String language = currentLocale.getDisplayLanguage(Locale.ENGLISH); + + return language.toLowerCase(); + } + + public static class Builder extends mz.cassamo.jls.Builder { + + public Builder() { + super(); + } + + } + } diff --git a/src/test/ExampleBuildingLS.java b/src/test/ExampleBuildingLS.java new file mode 100644 index 0000000..a15239a --- /dev/null +++ b/src/test/ExampleBuildingLS.java @@ -0,0 +1,33 @@ + +package test; + +import mz.cassamo.jls.LanguageSystem; + +/** + * + * @author cassamo + */ +public class ExampleBuildingLS { + + +public static void main(String[] args) { + LanguageSystem.Builder builder = new LanguageSystem.Builder(); + + + // If the file doesn't exist, it will be created during the save step + builder.loadFromFile("test/languages.xml"); + + // Add new languages to the language system + builder.putLanguage("english"); + builder.putLanguage("portuguese"); + + // Add translations for the "hello_world" key in both languages. + builder.putTranslation("english", "hello_world", "Hello, World!"); + builder.putTranslation("portuguese", "hello_world", "Olá, Mundo!"); + + // Save the updated translations to the same file. + builder.save(); +} + + +} From 5946ffc46e44d23a90e7c43a148f091d68204683 Mon Sep 17 00:00:00 2001 From: kelvencassamo Date: Fri, 13 Dec 2024 19:42:30 +0200 Subject: [PATCH 4/5] Adding auto create dir --- src/mz/cassamo/jls/Builder.java | 7 +++++++ src/test/ExampleBuildingLS.java | 5 +++-- test/languages.xml | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 test/languages.xml diff --git a/src/mz/cassamo/jls/Builder.java b/src/mz/cassamo/jls/Builder.java index cfc9fe3..86fdd4c 100644 --- a/src/mz/cassamo/jls/Builder.java +++ b/src/mz/cassamo/jls/Builder.java @@ -25,7 +25,14 @@ public Builder() { public void loadFromFile(String filePath) { this.filePath = filePath; + + try { + String dir_path = new File(filePath).getAbsoluteFile().getParent(); + File f = new File(dir_path); + if(!f.exists()) { + f.mkdir(); + } SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); LanguageHandler handler = new LanguageHandler(); diff --git a/src/test/ExampleBuildingLS.java b/src/test/ExampleBuildingLS.java index a15239a..1ccaa0d 100644 --- a/src/test/ExampleBuildingLS.java +++ b/src/test/ExampleBuildingLS.java @@ -1,6 +1,8 @@ package test; +import java.io.File; + import mz.cassamo.jls.LanguageSystem; /** @@ -19,8 +21,7 @@ public static void main(String[] args) { // Add new languages to the language system builder.putLanguage("english"); - builder.putLanguage("portuguese"); - + builder.putLanguage("portuguese"); // Add translations for the "hello_world" key in both languages. builder.putTranslation("english", "hello_world", "Hello, World!"); builder.putTranslation("portuguese", "hello_world", "Olá, Mundo!"); diff --git a/test/languages.xml b/test/languages.xml new file mode 100644 index 0000000..445ae1e --- /dev/null +++ b/test/languages.xml @@ -0,0 +1,14 @@ + + + + + + Hello, World! + + + + + Olá, Mundo! + + + \ No newline at end of file From 0b918b38dee16d9579699d3daf288d3dfc4ec542 Mon Sep 17 00:00:00 2001 From: kelvencassamo Date: Sat, 14 Dec 2024 14:10:49 +0200 Subject: [PATCH 5/5] Adding BuilderParseException --- src/mz/cassamo/jls/Builder.java | 24 +++++++++++++++-- .../jls/exceptions/BuilderParseException.java | 20 ++++++++++++++ src/test/ExampleBuildingLS.java | 27 ++++++++++++------- test/languages.xml | 14 ---------- 4 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 src/mz/cassamo/jls/exceptions/BuilderParseException.java diff --git a/src/mz/cassamo/jls/Builder.java b/src/mz/cassamo/jls/Builder.java index 86fdd4c..161ed49 100644 --- a/src/mz/cassamo/jls/Builder.java +++ b/src/mz/cassamo/jls/Builder.java @@ -11,6 +11,8 @@ import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import mz.cassamo.jls.exceptions.BuilderParseException; + /** * * @author cassamo @@ -23,7 +25,7 @@ public Builder() { } - public void loadFromFile(String filePath) { + public void loadFromFile(String filePath) throws BuilderParseException { this.filePath = filePath; @@ -40,7 +42,25 @@ public void loadFromFile(String filePath) { saxParser.parse(new File(filePath), handler); mergeTranslations(handler.getLanguages()); } catch (Exception e) { - + if(e.getClass().getName().equals("java.io.FileNotFoundException")) { + File file = new File(filePath); + try { + FileWriter w = new FileWriter(filePath); + w.write("\n"); + w.flush(); + } catch (IOException e1) { + + } + + } + else if(e.getClass().getName().equals("org.xml.sax.SAXParseException")) { + throw new BuilderParseException(e.getMessage()); + + } else { + System.out.println(e.getClass().getName()); + System.out.println(e.getMessage()); + } + } } diff --git a/src/mz/cassamo/jls/exceptions/BuilderParseException.java b/src/mz/cassamo/jls/exceptions/BuilderParseException.java new file mode 100644 index 0000000..3497857 --- /dev/null +++ b/src/mz/cassamo/jls/exceptions/BuilderParseException.java @@ -0,0 +1,20 @@ +package mz.cassamo.jls.exceptions; + +public class BuilderParseException extends Exception { + + public BuilderParseException(String message) { + super(message); + + } + + public BuilderParseException(Throwable cause) { + super(cause); + } + + public BuilderParseException(String message, Throwable cause) { + super(message, cause); + + } + + +} diff --git a/src/test/ExampleBuildingLS.java b/src/test/ExampleBuildingLS.java index 1ccaa0d..84f7f6f 100644 --- a/src/test/ExampleBuildingLS.java +++ b/src/test/ExampleBuildingLS.java @@ -4,6 +4,7 @@ import java.io.File; import mz.cassamo.jls.LanguageSystem; +import mz.cassamo.jls.exceptions.BuilderParseException; /** * @@ -16,18 +17,24 @@ public static void main(String[] args) { LanguageSystem.Builder builder = new LanguageSystem.Builder(); - // If the file doesn't exist, it will be created during the save step - builder.loadFromFile("test/languages.xml"); + + try { + // If the file doesn't exist, it will be created during the save step + builder.loadFromFile("test/languages.xml"); + // Add new languages to the language system + builder.putLanguage("english"); + builder.putLanguage("portuguese"); + // Add translations for the "hello_world" key in both languages. + builder.putTranslation("english", "hello_world", "Hello, World!"); + builder.putTranslation("portuguese", "hello_world", "Olá, Mundo!"); + + // Save the updated translations to the same file. + builder.save(); + } catch (BuilderParseException e) { + e.printStackTrace(); + } - // Add new languages to the language system - builder.putLanguage("english"); - builder.putLanguage("portuguese"); - // Add translations for the "hello_world" key in both languages. - builder.putTranslation("english", "hello_world", "Hello, World!"); - builder.putTranslation("portuguese", "hello_world", "Olá, Mundo!"); - // Save the updated translations to the same file. - builder.save(); } diff --git a/test/languages.xml b/test/languages.xml index 445ae1e..e69de29 100644 --- a/test/languages.xml +++ b/test/languages.xml @@ -1,14 +0,0 @@ - - - - - - Hello, World! - - - - - Olá, Mundo! - - - \ No newline at end of file