LanguagePack { public byte[] toJson() throws IOException { HashMap<String, HashMap<String, Object>> language = new HashMap<>(); final HashMap<String, Object> translations = new HashMap<>(); Catalog catalog = poParser.parseCatalog(poFile); language.put(extractLanguageFrom(catalog.locateHeader()), translations); catalog.processMessages(new MessageProcessor() { @Override public void processMessage(Message message) { if (isHeader(message)) { return; } translations.put(message.getMsgid(), isPlural(message) ? message.getMsgstrPlural() : message.getMsgstr()); } }); return objectMapper.toJson(language); } LanguagePack(PoParser poParser, JacksonObjectMapper objectMapper, File poFile); byte[] toJson();  }
@Test public void should_convert_translation_into_json() throws Exception { File poFile = directory.newFile(); write(poFile.toPath(), readResource("/i18n/simple.po")); assertThat(new String(languagePackFactory.create(poFile).toJson())) .isEqualTo("{\"francais\":{\"A page\":\"Une page\"}}"); }
@Test public void should_convert_plural_translations_into_json() throws Exception { File poFile = directory.newFile(); write(poFile.toPath(), readResource("/i18n/plural.po")); assertThat(new String(languagePackFactory.create(poFile).toJson())) .isEqualTo("{\"francais\":{\"A page\":[\"Une page\",\"Des pages\"]}}"); }
@Test(expected = RuntimeException.class) public void should_throw_a_runtime_exception_if_the_po_file_does_not_contains_the_language() throws Exception { File poFile = directory.newFile(); write(poFile.toPath(), new String(readResource("/i18n/simple.po")).replace("Language: francais", "").getBytes()); assertThat(new String(languagePackFactory.create(poFile).toJson())) .isEqualTo("{\"francais\":{\"A page\":[\"Une page\",\"Des pages\"]}}"); }