Basic I18n stuff
This commit is contained in:
@@ -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
|
||||
@@ -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";
|
||||
}
|
||||
@@ -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<String, String> keys;
|
||||
private String code;
|
||||
|
||||
public Language(String code) throws IOException {
|
||||
this.keys=new HashMap<String, String>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user