From f9c4725224fab5ac04c067683e177ebfd6feda65 Mon Sep 17 00:00:00 2001 From: samuelowino Date: Sun, 19 Jun 2022 13:11:11 +0300 Subject: [PATCH] Removed hardcoded output file path, added execution shell script --- mobile-translate.sh | 1 + pom.xml | 3 +- .../TranslateApplication.java | 48 +++++------------- .../android/AndroidTranslationProcessor.java | 12 +++++ .../android/xml/XmlParserReader.java | 45 +++++++++-------- .../common/LanguagesDirectory.java | 31 ++++++++++++ .../common/RunnerInputReader.java | 50 +++++++++++++++++++ .../common/TranslationProcessor.java | 8 +++ .../googletranslatelabs/enums/TargetOS.java | 6 +++ .../ios/iOSTranslationProcessor.java | 12 +++++ .../ios/model/LocalizableTable.java | 2 +- 11 files changed, 160 insertions(+), 58 deletions(-) create mode 100644 mobile-translate.sh create mode 100644 src/main/java/org/apluscreators/googletranslatelabs/android/AndroidTranslationProcessor.java create mode 100644 src/main/java/org/apluscreators/googletranslatelabs/common/LanguagesDirectory.java create mode 100644 src/main/java/org/apluscreators/googletranslatelabs/common/RunnerInputReader.java create mode 100644 src/main/java/org/apluscreators/googletranslatelabs/common/TranslationProcessor.java create mode 100644 src/main/java/org/apluscreators/googletranslatelabs/enums/TargetOS.java create mode 100644 src/main/java/org/apluscreators/googletranslatelabs/ios/iOSTranslationProcessor.java diff --git a/mobile-translate.sh b/mobile-translate.sh new file mode 100644 index 0000000..f394f99 --- /dev/null +++ b/mobile-translate.sh @@ -0,0 +1 @@ +mvn clean install;cd target;java -jar mobile-translate-jar-with-dependencies.jar \ No newline at end of file diff --git a/pom.xml b/pom.xml index db8c385..44c63f3 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 org.apluscreators Google-Translate-Labs - 2.0-BETA + 2.0 jar @@ -55,6 +55,7 @@ + mobile-translate org.apache.maven.plugins diff --git a/src/main/java/org/apluscreators/googletranslatelabs/TranslateApplication.java b/src/main/java/org/apluscreators/googletranslatelabs/TranslateApplication.java index 3b33772..4b98690 100644 --- a/src/main/java/org/apluscreators/googletranslatelabs/TranslateApplication.java +++ b/src/main/java/org/apluscreators/googletranslatelabs/TranslateApplication.java @@ -1,48 +1,26 @@ package org.apluscreators.googletranslatelabs; - -import org.apluscreators.googletranslatelabs.android.xml.XmlParserReader; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.logging.Logger; -import java.util.stream.Collectors; +import org.apluscreators.googletranslatelabs.android.AndroidTranslationProcessor; +import org.apluscreators.googletranslatelabs.common.LanguagesDirectory; +import org.apluscreators.googletranslatelabs.common.RunnerInputReader; +import org.apluscreators.googletranslatelabs.enums.TargetOS; +import org.apluscreators.googletranslatelabs.ios.iOSTranslationProcessor; public class TranslateApplication { - private static final Logger LOGGER = Logger.getLogger(TranslateApplication.class.getName()); - - private static final List languages = Arrays.asList( - "ha", "pl", "am", "ar", "az", "be", "bg", "bn", "bs", "fr", "ga", - "ca", "cs", "da", "es", "et", "fa", "fi", "gl", - "it", "iw", "ja", "jv", "ka", "km", "ko", "ky", - "my", "nb", "ne", "nl", "nn", "no", "pa", "ro", "ru", "si", "sk", "sl", "sq", "sr", - "sv", "sw", "ta", "te", "th", "tr", "vi", "zh", "zu", "kn", "kk", - "af","de", "fil","hi","hr", "hu","hy","id","in","lo","lv","mk","ml","mn","pt","uk","ur","ms","is","el","mr" - ); - - private static final Set uniqueLanguagesList = new HashSet<>(languages); - public static void main(String[] args) { - checkForDuplicates(languages); + var targetOS = RunnerInputReader.getTargetOSFromUser(); - List distinctLanguages = uniqueLanguagesList.stream() - .distinct() - .peek(System.out::println) - .collect(Collectors.toList()); + var outputFile = RunnerInputReader.getOutputFileFromPrompt(); - for (String language : distinctLanguages) { - XmlParserReader xmlParserReader = new XmlParserReader(); - xmlParserReader.executeXmlAndTranslationParser(language); - } + var distinctLanguages = new LanguagesDirectory().findUniqueListOfLocales(); - } + if (targetOS == TargetOS.ANDROID) { + new AndroidTranslationProcessor().runTranslation(outputFile, distinctLanguages); + } else if (targetOS == TargetOS.IOS) { + new iOSTranslationProcessor().runTranslation(outputFile, distinctLanguages); + } - public static void checkForDuplicates(List languages) { - LOGGER.info("Original List" + languages); - LOGGER.info("Unique List " + uniqueLanguagesList); } } diff --git a/src/main/java/org/apluscreators/googletranslatelabs/android/AndroidTranslationProcessor.java b/src/main/java/org/apluscreators/googletranslatelabs/android/AndroidTranslationProcessor.java new file mode 100644 index 0000000..0399e00 --- /dev/null +++ b/src/main/java/org/apluscreators/googletranslatelabs/android/AndroidTranslationProcessor.java @@ -0,0 +1,12 @@ +package org.apluscreators.googletranslatelabs.android; + +import java.io.File; +import java.util.List; +import org.apluscreators.googletranslatelabs.android.xml.XmlParserReader; +import org.apluscreators.googletranslatelabs.common.TranslationProcessor; + +public class AndroidTranslationProcessor implements TranslationProcessor { + public void runTranslation(File outputFile, List targetLanguages){ + new XmlParserReader().executeXmlAndTranslationParser(outputFile, targetLanguages); + } +} diff --git a/src/main/java/org/apluscreators/googletranslatelabs/android/xml/XmlParserReader.java b/src/main/java/org/apluscreators/googletranslatelabs/android/xml/XmlParserReader.java index 3845ec2..afd0538 100644 --- a/src/main/java/org/apluscreators/googletranslatelabs/android/xml/XmlParserReader.java +++ b/src/main/java/org/apluscreators/googletranslatelabs/android/xml/XmlParserReader.java @@ -1,32 +1,39 @@ package org.apluscreators.googletranslatelabs.android.xml; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.List; +import java.util.logging.Logger; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; - -import org.apluscreators.googletranslatelabs.android.model.String; import org.apluscreators.googletranslatelabs.android.model.Resources; +import org.apluscreators.googletranslatelabs.android.model.String; import org.apluscreators.googletranslatelabs.android.translate.GoogleTranslateFactory; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; - public class XmlParserReader { - public void executeXmlAndTranslationParser(java.lang.String targetLanguage) { + private static final Logger LOGGER = Logger.getLogger(XmlParserReader.class.getSimpleName()); - Resources untranslatedResources = fromXml(); + public void executeXmlAndTranslationParser(File outputFile, List targetLanguages) { - GoogleTranslateFactory translateFactory = new GoogleTranslateFactory(untranslatedResources); - Resources translatedResources = translateFactory.getTranslatedResources(targetLanguage); + LOGGER.info("Translating " + targetLanguages.size() + " locales to target output file " + outputFile.getAbsolutePath()); - for (String string : translatedResources.getStrings()) { - System.out.println(string); - } + for (java.lang.String targetLanguage : targetLanguages) { + + Resources untranslatedResources = fromXml(outputFile); + + GoogleTranslateFactory translateFactory = new GoogleTranslateFactory(untranslatedResources); + Resources translatedResources = translateFactory.getTranslatedResources(targetLanguage); + + for (String string : translatedResources.getStrings()) { + LOGGER.info(string.toString()); + } - toXml(translatedResources,targetLanguage + ".xml"); + toXml(translatedResources,targetLanguage + ".xml"); + } } @@ -36,23 +43,19 @@ public void toXml(Resources contentRoot,java.lang.String outputFileName) { Marshaller marshallerObj = contextObj.createMarshaller(); marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshallerObj.marshal(contentRoot, new FileOutputStream(outputFileName)); - - } catch (JAXBException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { + } catch (JAXBException | FileNotFoundException e) { e.printStackTrace(); } } - public Resources fromXml() { + public Resources fromXml(File outputFile) { try { - File file = new File("/Users/samuelowino/Documents/projects/Google-Translate-Script/output.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Resources.class); Unmarshaller jaxbContextUnmarshaller = jaxbContext.createUnmarshaller(); - Resources resources = (Resources) jaxbContextUnmarshaller.unmarshal(file); + Resources resources = (Resources) jaxbContextUnmarshaller.unmarshal(outputFile); System.out.println("Resources | " + resources); diff --git a/src/main/java/org/apluscreators/googletranslatelabs/common/LanguagesDirectory.java b/src/main/java/org/apluscreators/googletranslatelabs/common/LanguagesDirectory.java new file mode 100644 index 0000000..e941ae6 --- /dev/null +++ b/src/main/java/org/apluscreators/googletranslatelabs/common/LanguagesDirectory.java @@ -0,0 +1,31 @@ +package org.apluscreators.googletranslatelabs.common; + +import java.util.Arrays; +import java.util.List; +import java.util.logging.Logger; +import java.util.stream.Collectors; +import org.apluscreators.googletranslatelabs.TranslateApplication; + +public class LanguagesDirectory { + + public static final Logger LOGGER = Logger.getLogger(TranslateApplication.class.getSimpleName()); + + private final List languages = Arrays.asList( + "ha", "pl", "am", "ar", "az", "be", "bg", "bn", "bs", "fr", "ga", + "ca", "cs", "da", "es", "et", "fa", "fi", "gl", + "it", "iw", "ja", "jv", "ka", "km", "ko", "ky", + "my", "nb", "ne", "nl", "nn", "no", "pa", "ro", "ru", "si", "sk", "sl", "sq", "sr", + "sv", "sw", "ta", "te", "th", "tr", "vi", "zh", "zu", "kn", "kk", + "af", "de", "fil", "hi", "hr", "hu", "hy", "id", "in", "lo", "lv", "mk", "ml", "mn", "pt", "uk", "ur", "ms", "is", "el", "mr" + ); + + public List findUniqueListOfLocales() { + + LOGGER.info("Cleaning up locales list..."); + + return languages.stream() + .distinct() + .peek(System.out::println) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/org/apluscreators/googletranslatelabs/common/RunnerInputReader.java b/src/main/java/org/apluscreators/googletranslatelabs/common/RunnerInputReader.java new file mode 100644 index 0000000..ac603cf --- /dev/null +++ b/src/main/java/org/apluscreators/googletranslatelabs/common/RunnerInputReader.java @@ -0,0 +1,50 @@ +package org.apluscreators.googletranslatelabs.common; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.util.Scanner; +import java.util.logging.Logger; +import org.apluscreators.googletranslatelabs.TranslateApplication; +import org.apluscreators.googletranslatelabs.enums.TargetOS; + +public class RunnerInputReader { + + public static final Logger LOGGER = Logger.getLogger(TranslateApplication.class.getSimpleName()); + + public static File getOutputFileFromPrompt(){ + LOGGER.severe("Enter the output file directory for the translated text...."); + + var scanner = new Scanner(System.in); + var filePath = scanner.nextLine(); + var outputFile = new File(filePath); + + boolean fileExists = Files.exists(outputFile.toPath(), new LinkOption[]{LinkOption.NOFOLLOW_LINKS}); + + if (!fileExists) { + LOGGER.severe("This output file does not exist"); + outputFile = getOutputFileFromPrompt(); + } else { + LOGGER.info("Processing translation into output file [" + filePath + "]"); + } + + return outputFile; + } + + public static TargetOS getTargetOSFromUser() { + LOGGER.severe("What is the target mobile operating system for this localization? type in (A) for Android or (I) for iOS..."); + + var scanner = new Scanner(System.in); + + var option = scanner.nextLine(); + + switch (option){ + case "A": + return TargetOS.ANDROID; + case "I": + return TargetOS.IOS; + default: + throw new AssertionError("Invalid OS option " + option); + } + } +} diff --git a/src/main/java/org/apluscreators/googletranslatelabs/common/TranslationProcessor.java b/src/main/java/org/apluscreators/googletranslatelabs/common/TranslationProcessor.java new file mode 100644 index 0000000..f829457 --- /dev/null +++ b/src/main/java/org/apluscreators/googletranslatelabs/common/TranslationProcessor.java @@ -0,0 +1,8 @@ +package org.apluscreators.googletranslatelabs.common; + +import java.io.File; +import java.util.List; + +public interface TranslationProcessor { + void runTranslation(File outputFile, List targetLanguages); +} diff --git a/src/main/java/org/apluscreators/googletranslatelabs/enums/TargetOS.java b/src/main/java/org/apluscreators/googletranslatelabs/enums/TargetOS.java new file mode 100644 index 0000000..3878aae --- /dev/null +++ b/src/main/java/org/apluscreators/googletranslatelabs/enums/TargetOS.java @@ -0,0 +1,6 @@ +package org.apluscreators.googletranslatelabs.enums; + +public enum TargetOS { + ANDROID, + IOS +} diff --git a/src/main/java/org/apluscreators/googletranslatelabs/ios/iOSTranslationProcessor.java b/src/main/java/org/apluscreators/googletranslatelabs/ios/iOSTranslationProcessor.java new file mode 100644 index 0000000..6fc32c2 --- /dev/null +++ b/src/main/java/org/apluscreators/googletranslatelabs/ios/iOSTranslationProcessor.java @@ -0,0 +1,12 @@ +package org.apluscreators.googletranslatelabs.ios; + +import java.io.File; +import java.util.List; +import org.apluscreators.googletranslatelabs.common.TranslationProcessor; + +public class iOSTranslationProcessor implements TranslationProcessor { + @Override + public void runTranslation(File outputFile, List targetLanguages) { + + } +} diff --git a/src/main/java/org/apluscreators/googletranslatelabs/ios/model/LocalizableTable.java b/src/main/java/org/apluscreators/googletranslatelabs/ios/model/LocalizableTable.java index 42f06e2..8aaa062 100644 --- a/src/main/java/org/apluscreators/googletranslatelabs/ios/model/LocalizableTable.java +++ b/src/main/java/org/apluscreators/googletranslatelabs/ios/model/LocalizableTable.java @@ -2,5 +2,5 @@ public class LocalizableTable { private String key; - private String translatableResouce; + private String translatableResource; }