@Test public void testWriteTranslatedFile() throws IOException { File sourceFile = new File("src/test/resources/test.pot"); InputStream inputStream = new FileInputStream(sourceFile); LocaleCode fromLocaleCode = LocaleCode.EN; LocaleCode toLocaleCode = LocaleCode.DE; String attribution = "translated by test"; String filename = "test.pot"; Pair<DocumentContent, Map<String, Message>> contents = poFileAdapter.parseSourceDocument(inputStream, filename, fromLocaleCode); for (TypeString typeString: contents.getLeft().getContents()) { typeString.setValue(typeString.getValue() + "_translated"); } OutputStream outputStream = Mockito.mock(OutputStream.class); poFileAdapter .writeTranslatedFile(outputStream, fromLocaleCode, toLocaleCode, contents.getLeft(), contents.getRight(), attribution); verify(outputStream, atLeastOnce()) .write(any(byte[].class), anyInt(), anyInt()); }
@Override public void writeTranslatedFile(@NotNull OutputStream output, LocaleCode fromLocaleCode, LocaleCode toLocaleCode, DocumentContent translatedDocContent, Map<String, Message> messages, String attribution) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(output, charset)); PoWriter poWriter = new PoWriter(); if (messages == null || messages.isEmpty()) { LOG.warn( "No messages to output. Please make sure parseSourceDocument is triggered before output file"); return; } for (TypeString trans : translatedDocContent.getContents()) { Message message = messages.get(trans.getMetadata()); message.setMsgstr(trans.getValue()); } boolean haveInsertedAttribution = false; for (Map.Entry<String, Message> entry : messages.entrySet()) { if (entry.getValue().isHeader()) { if (!haveInsertedAttribution) { entry.getValue().addComment(attribution); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } else { if (!haveInsertedAttribution) { Message message = new Message(); message.addComment(attribution); message.setMsgid(entry.getValue().getMsgid()); poWriter.write(message, writer); writer.write("\n"); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } } messages.clear(); }
PoFileAdapter implements TranslationFileAdapter { @Override public void writeTranslatedFile(@NotNull OutputStream output, LocaleCode fromLocaleCode, LocaleCode toLocaleCode, DocumentContent translatedDocContent, Map<String, Message> messages, String attribution) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(output, charset)); PoWriter poWriter = new PoWriter(); if (messages == null || messages.isEmpty()) { LOG.warn( "No messages to output. Please make sure parseSourceDocument is triggered before output file"); return; } for (TypeString trans : translatedDocContent.getContents()) { Message message = messages.get(trans.getMetadata()); message.setMsgstr(trans.getValue()); } boolean haveInsertedAttribution = false; for (Map.Entry<String, Message> entry : messages.entrySet()) { if (entry.getValue().isHeader()) { if (!haveInsertedAttribution) { entry.getValue().addComment(attribution); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } else { if (!haveInsertedAttribution) { Message message = new Message(); message.addComment(attribution); message.setMsgid(entry.getValue().getMsgid()); poWriter.write(message, writer); writer.write("\n"); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } } messages.clear(); } }
PoFileAdapter implements TranslationFileAdapter { @Override public void writeTranslatedFile(@NotNull OutputStream output, LocaleCode fromLocaleCode, LocaleCode toLocaleCode, DocumentContent translatedDocContent, Map<String, Message> messages, String attribution) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(output, charset)); PoWriter poWriter = new PoWriter(); if (messages == null || messages.isEmpty()) { LOG.warn( "No messages to output. Please make sure parseSourceDocument is triggered before output file"); return; } for (TypeString trans : translatedDocContent.getContents()) { Message message = messages.get(trans.getMetadata()); message.setMsgstr(trans.getValue()); } boolean haveInsertedAttribution = false; for (Map.Entry<String, Message> entry : messages.entrySet()) { if (entry.getValue().isHeader()) { if (!haveInsertedAttribution) { entry.getValue().addComment(attribution); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } else { if (!haveInsertedAttribution) { Message message = new Message(); message.addComment(attribution); message.setMsgid(entry.getValue().getMsgid()); poWriter.write(message, writer); writer.write("\n"); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } } messages.clear(); } PoFileAdapter(Charset charset); }
PoFileAdapter implements TranslationFileAdapter { @Override public void writeTranslatedFile(@NotNull OutputStream output, LocaleCode fromLocaleCode, LocaleCode toLocaleCode, DocumentContent translatedDocContent, Map<String, Message> messages, String attribution) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(output, charset)); PoWriter poWriter = new PoWriter(); if (messages == null || messages.isEmpty()) { LOG.warn( "No messages to output. Please make sure parseSourceDocument is triggered before output file"); return; } for (TypeString trans : translatedDocContent.getContents()) { Message message = messages.get(trans.getMetadata()); message.setMsgstr(trans.getValue()); } boolean haveInsertedAttribution = false; for (Map.Entry<String, Message> entry : messages.entrySet()) { if (entry.getValue().isHeader()) { if (!haveInsertedAttribution) { entry.getValue().addComment(attribution); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } else { if (!haveInsertedAttribution) { Message message = new Message(); message.addComment(attribution); message.setMsgid(entry.getValue().getMsgid()); poWriter.write(message, writer); writer.write("\n"); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } } messages.clear(); } PoFileAdapter(Charset charset); @Override Pair<DocumentContent, Map<String, Message>> parseSourceDocument(
        InputStream inputStream, String url, LocaleCode fromLocaleCode); @Override void writeTranslatedFile(@NotNull OutputStream output,
        LocaleCode fromLocaleCode, LocaleCode toLocaleCode,
        DocumentContent translatedDocContent, Map<String, Message> messages,
        String attribution); @Override String getTranslationFileExtension(); }
PoFileAdapter implements TranslationFileAdapter { @Override public void writeTranslatedFile(@NotNull OutputStream output, LocaleCode fromLocaleCode, LocaleCode toLocaleCode, DocumentContent translatedDocContent, Map<String, Message> messages, String attribution) throws IOException { Writer writer = new BufferedWriter(new OutputStreamWriter(output, charset)); PoWriter poWriter = new PoWriter(); if (messages == null || messages.isEmpty()) { LOG.warn( "No messages to output. Please make sure parseSourceDocument is triggered before output file"); return; } for (TypeString trans : translatedDocContent.getContents()) { Message message = messages.get(trans.getMetadata()); message.setMsgstr(trans.getValue()); } boolean haveInsertedAttribution = false; for (Map.Entry<String, Message> entry : messages.entrySet()) { if (entry.getValue().isHeader()) { if (!haveInsertedAttribution) { entry.getValue().addComment(attribution); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } else { if (!haveInsertedAttribution) { Message message = new Message(); message.addComment(attribution); message.setMsgid(entry.getValue().getMsgid()); poWriter.write(message, writer); writer.write("\n"); haveInsertedAttribution = true; } poWriter.write(entry.getValue(), writer); writer.write("\n"); } } messages.clear(); } PoFileAdapter(Charset charset); @Override Pair<DocumentContent, Map<String, Message>> parseSourceDocument(
        InputStream inputStream, String url, LocaleCode fromLocaleCode); @Override void writeTranslatedFile(@NotNull OutputStream output,
        LocaleCode fromLocaleCode, LocaleCode toLocaleCode,
        DocumentContent translatedDocContent, Map<String, Message> messages,
        String attribution); @Override String getTranslationFileExtension();  }