use of com.xenoage.utils.lang.Language in project Zong by Xenoage.
the class LanguageTest method get1.
/**
* Tests the get(String)-method.
*/
@Test
public void get1() throws Exception {
Language l = LanguageReader.read("data/test/lang", "testlang");
assertEquals("This is a test vocabulary.", l.get(TestVocabulary.TestVoc));
assertNull(l.getWithNull(TestVocabulary.NotExisting));
}
use of com.xenoage.utils.lang.Language in project Zong by Xenoage.
the class LanguageTest method get2.
/**
* Tests the get(String, String[])-method.
*/
@Test
public void get2() throws Exception {
Language l = LanguageReader.read("data/test/lang", "testlang");
String[] tokens = new String[] { "stupid", "- haha", "crazy" };
assertEquals("This (stupid) text has some crazy tokens in it - haha.", l.get(TestVocabulary.TestVoc2, tokens));
}
use of com.xenoage.utils.lang.Language in project Zong by Xenoage.
the class POCreator method main.
public static void main(String... args) throws IOException {
JsePlatformUtils.init(POCreator.class.getName());
Language lang = LanguageReader.read(LangManager.defaultLangPath, "de");
POCreator.write(Voc.values(), lang, "de", "de.po");
}
use of com.xenoage.utils.lang.Language in project Zong by Xenoage.
the class LanguageReader method read.
/**
* Creates a {@link Language} from all .xml and .po files in the folder <code>basePath/id</code>.
* If the language pack can not be loaded, an {@link IOException} is thrown.
* @param path path to the language pack directory (without trailing slash)
* @param id id of the language pack
*/
public static Language read(String basePath, String id) throws IOException {
log(remark("Loading language pack \"" + id + "\" from folder \"" + basePath + "\"..."));
// check if language exists
String dir = basePath + "/" + id;
if (false == io().existsFile(dir + "/id.xml"))
throw new FileNotFoundException("Language " + id + " does not exist");
// locate vocabulary files
List<String> langFiles = io().listFiles(dir, orFilter(xmlFilter, poFilter));
langFiles.remove("id.xml");
// load entries
HashMap<String, String> entries = map();
int entriesCount = 0;
int entriesOverwrittenCount = 0;
for (String langFileName : langFiles) {
JseInputStream langStream = io().openFile(dir + "/" + langFileName);
// read XML or PO file
HashMap<String, String> fileEntries = null;
if (langFileName.endsWith(".po")) {
log(remark("Reading PO language file \"" + langFileName + "\""));
fileEntries = readPO(langStream);
} else {
log(remark("Reading XML language file \"" + langFileName + "\""));
fileEntries = readXML(langStream);
}
// insert vocabulary data
for (Entry<String, String> fileEntry : fileEntries.entrySet()) {
String oldValue = entries.put(fileEntry.getKey(), fileEntry.getValue());
if (oldValue == null)
entriesCount++;
else {
log(warning("Overwritten entry: " + fileEntry.getKey()));
entriesOverwrittenCount++;
}
}
}
log(remark("Language pack loaded. Entries: " + entriesCount + ". Overwritten entries: " + entriesOverwrittenCount));
// replace all tokens
for (String key : entries.keySet()) {
String value = entries.get(key);
if (value.contains("{")) {
entries.put(key, replaceTokens(value, entries));
}
}
return new Language(id, entries);
}
Aggregations