diff --git a/data/en.lang b/data/en.lang new file mode 100644 index 0000000..2ef5c4a --- /dev/null +++ b/data/en.lang @@ -0,0 +1,7 @@ +title=Totally Not Malware +text.intro=H-hi there... +text.question=Do you th-think I could have your credit card information, p-please +label.card=Card number: +label.date=Expiry date: +label.code=Security code: +button=Th-thanks diff --git a/src/totallynotmalware/i18n/I18n.java b/src/totallynotmalware/i18n/I18n.java new file mode 100644 index 0000000..3a3e43d --- /dev/null +++ b/src/totallynotmalware/i18n/I18n.java @@ -0,0 +1,27 @@ +package totallynotmalware.i18n; + +import java.io.IOException; + +public class I18n { + private static Language lang; + + public static void setLanguage(String lang) throws IOException { + if(I18n.lang==null || !I18n.lang.getCode().equals(lang)) { + I18n.lang=new Language(lang); + } + } + + public static String get(String name) { + if(I18n.lang==null) { + try { + I18n.setLanguage(I18n.defaultLang); + } catch (IOException e) { + throw new RuntimeException("Couldn't load language file", e); + } + } + + return I18n.lang.get(name); + } + + public static final String defaultLang="en"; +} diff --git a/src/totallynotmalware/i18n/Language.java b/src/totallynotmalware/i18n/Language.java new file mode 100644 index 0000000..fd42222 --- /dev/null +++ b/src/totallynotmalware/i18n/Language.java @@ -0,0 +1,38 @@ +package totallynotmalware.i18n; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; + +public class Language { + private Map keys; + private String code; + + public Language(String code) throws IOException { + this.keys=new HashMap(); + this.code=code; + + // could be in these two places + InputStream in=this.getClass().getClassLoader().getResourceAsStream(code+".lang"); + if(in==null) in=this.getClass().getClassLoader().getResourceAsStream("/data/"+code+".lang"); + + // read the language file + BufferedReader reader=new BufferedReader(new InputStreamReader(in)); + for(String line=reader.readLine(); line!=null; line=reader.readLine()) { + if(line.isEmpty()) continue; + String[] parts=line.split("=", 2); + this.keys.put(parts[0], parts[1]); + } + reader.close(); + } + + public String get(String name) { + return this.keys.getOrDefault(name, name); + } + public String getCode() { + return this.code; + } +} diff --git a/test/totallynotmalware/TestI18n.java b/test/totallynotmalware/TestI18n.java new file mode 100644 index 0000000..040db8a --- /dev/null +++ b/test/totallynotmalware/TestI18n.java @@ -0,0 +1,21 @@ +package totallynotmalware; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import totallynotmalware.i18n.I18n; + +class TestI18n { + + @BeforeEach + void setUp() throws Exception { + } + + @Test + void testDefaultLang() { + assertEquals(I18n.get("title"), "Totally Not Malware"); + } + +} diff --git a/test/totallynotmalware/TestLanguage.java b/test/totallynotmalware/TestLanguage.java new file mode 100644 index 0000000..b17feb1 --- /dev/null +++ b/test/totallynotmalware/TestLanguage.java @@ -0,0 +1,30 @@ +package totallynotmalware; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import totallynotmalware.i18n.Language; + +class TestLanguage { + + Language en; + + @BeforeEach + void setUp() throws Exception { + en=new Language("en"); + } + + @Test + void testGet() { + assertEquals(en.get("title"), "Totally Not Malware"); + assertEquals(en.get("button"), "Th-thanks"); + } + + @Test + void testGetFallback() { + assertEquals(en.get("unknown.key"), "unknown.key"); + } + +}