Search code examples
javaregexreplacepattern-matchingreplaceall

Advise to create a translator in Java


I want to make a translator ex: English to Spanish.

I want to translate a large text with a map for the translation.

HashMap <String, Object> hashmap = new HashMap <String, Object>();
hashmap.put("hello", "holla"); 
.
.
.

Witch object should I use to handle my inital text of 1000 words? A String or StringBuilder is fine ?

How can I do a large replace? Without iterate each word with each element of the map ?

I don't want take each word of the string, and see there is a match in my map

Maybe a multimap with the first letter of the word?

If you have any answer or advise thank you


Solution

  • Here is an example implementation:

    import java.io.*;
    import java.util.*;
    
    public class Translator {
    
        public enum Language {
            EN, ES
        }
    
        private static final String TRANSLATION_TEMPLATE = "translation_%s_%s.properties";
        private final Properties translations = new Properties();
    
        public Translator(Language from, Language to) {
            String translationFile = String.format(TRANSLATION_TEMPLATE, from, to);
            try (InputStream is = getClass().getResourceAsStream(translationFile)) {
                translations.load(is);
            } catch (final IOException e) {
                throw new RuntimeException("Could not read: " + translationFile, e);
            }
        }
    
        private String[] translate(String text) {
            String[] source = normalizeText(text);
            List<String> translation = new ArrayList<>();
            for (String sourceWord : source) {
                translation.add(translateWord(sourceWord));
            }
            return translation.toArray(new String[source.length]);
        }
    
        private String translateWord(String sourceWord) {
            Object value = translations.get(sourceWord);
            String translatedWord;
            if (value != null) {
                translatedWord = String.valueOf(value);
            }
            else {
                // if no translation is found, add the source word with a question mark
                translatedWord = sourceWord + "?";
            }
            return translatedWord;
        }
    
        private String[] normalizeText(String text) {
            String alphaText = text.replaceAll("[^A-Za-z]", " ");
            return alphaText.split("\\s+");
        }
    
        public static void main(final String[] args) {
            final Translator translator = new Translator(Language.EN, Language.ES);
            System.out.println(Arrays.toString(translator.translate("hello world!")));
        }
    }
    

    And put a file called 'translation_EN_ES.properties' on your classpath (e.g. src/main/resources) with:

    hello=holla
    world=mundo