Search in sources :

Example 6 with PrepareTMXEntry

use of org.omegat.core.data.PrepareTMXEntry in project omegat by omegat-org.

the class TMXWriter method buildTMXFile.

/**
 * Saves a TMX file to disk
 *
 * @param filename
 *            The name of the file to create
 * @param forceValidTMX
 *            When true, OmegaT-tags are stripped from the segments.
 * @param levelTwo
 *            When true, the tmx is made compatible with level 2 (TMX
 *            version 1.4)
 * @param config
 *            Project configuration, to get the languages
 * @param data
 *            Data for save to TMX, a map of {source segments, translation}
 * @throws IOException
 */
public static void buildTMXFile(final String filename, final boolean forceValidTMX, final boolean levelTwo, final ProjectProperties config, final Map<String, PrepareTMXEntry> data) throws IOException {
    // we got this far, so assume lang codes are proper
    String sourceLocale = config.getSourceLanguage().toString();
    String targetLocale = config.getTargetLanguage().toString();
    String segmenting;
    if (config.isSentenceSegmentingEnabled()) {
        segmenting = "sentence";
    } else {
        segmenting = "paragraph";
    }
    FileOutputStream fos = new FileOutputStream(filename);
    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
    // PW is easier to use than
    PrintWriter out = new PrintWriter(osw);
    // Buff.Writer
    String version = OStrings.VERSION;
    if (!OStrings.UPDATE.equals("0")) {
        version = version + "_" + OStrings.UPDATE;
    }
    // Write TMX header
    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    if (levelTwo) {
        out.println("<!DOCTYPE tmx SYSTEM \"tmx14.dtd\">");
    } else {
        out.println("<!DOCTYPE tmx SYSTEM \"tmx11.dtd\">");
    }
    if (levelTwo) {
        out.println("<tmx version=\"1.4\">");
    } else {
        out.println("<tmx version=\"1.1\">");
    }
    out.println("  <header");
    out.println("    creationtool=\"OmegaT\"");
    out.println("    creationtoolversion=\"" + version + "\"");
    out.println("    segtype=\"" + segmenting + "\"");
    out.println("    o-tmf=\"OmegaT TMX\"");
    out.println("    adminlang=\"EN-US\"");
    out.println("    srclang=\"" + sourceLocale + "\"");
    out.println("    datatype=\"plaintext\"");
    out.println("  >");
    out.println("  </header>");
    out.println("  <body>");
    // Determine language attribute to use
    String langAttr = levelTwo ? "xml:lang" : "lang";
    // Write TUs
    String source = null;
    String target = null;
    String note = null;
    TMXDateParser dateParser = new TMXDateParser();
    for (Map.Entry<String, PrepareTMXEntry> en : data.entrySet()) {
        PrepareTMXEntry transEntry = en.getValue();
        source = forceValidTMX ? TagUtil.stripXmlTags(en.getKey()) : en.getKey();
        target = forceValidTMX ? TagUtil.stripXmlTags(transEntry.translation) : transEntry.translation;
        source = StringUtil.makeValidXML(source);
        target = StringUtil.makeValidXML(target);
        if (transEntry.note != null) {
            note = forceValidTMX ? TagUtil.stripXmlTags(transEntry.note) : transEntry.note;
            note = StringUtil.makeValidXML(note);
        }
        // which it should not.
        if (levelTwo) {
            source = makeLevelTwo(source);
            target = makeLevelTwo(target);
        }
        String changeIdPropertyString = (transEntry.changer != null && !"".equals(transEntry.changer) ? " changeid=\"" + transEntry.changer + "\"" : "");
        String changeDatePropertyString = (transEntry.changeDate != 0 ? " changedate=\"" + dateParser.getTMXDate(transEntry.changeDate) + "\"" : "");
        out.println("    <tu>");
        out.println("      <tuv " + langAttr + "=\"" + sourceLocale + "\">");
        out.println("        <seg>" + source + "</seg>");
        out.println("      </tuv>");
        out.println("      <tuv " + langAttr + "=\"" + targetLocale + "\"" + changeDatePropertyString + changeIdPropertyString + ">");
        out.println("        <seg>" + target + "</seg>");
        out.println("      </tuv>");
        if (note != null) {
            out.println("      <note>" + note + "</note>");
        }
        out.println("    </tu>");
    }
    // Write TMX footer
    out.println("  </body>");
    out.println("</tmx>");
    // Close output stream
    out.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) PrintWriter(java.io.PrintWriter)

Example 7 with PrepareTMXEntry

use of org.omegat.core.data.PrepareTMXEntry in project omegat by omegat-org.

the class POFilterTest method testLoad.

@Test
public void testLoad() throws Exception {
    String f = "test/data/filters/po/file-POFilter-multiple.po";
    Map<String, String> options = new TreeMap<String, String>();
    options.put("skipHeader", "true");
    TestFileInfo fi = loadSourceFiles(new PoFilter(), f, options);
    String comment = OStrings.getString("POFILTER_TRANSLATOR_COMMENTS") + "\n" + "A valid comment\nAnother valid comment\n\n" + OStrings.getString("POFILTER_EXTRACTED_COMMENTS") + "\n" + "Some extracted comments\nMore extracted comments\n\n" + OStrings.getString("POFILTER_REFERENCES") + "\n" + "/my/source/file\n/my/source/file2\n\n";
    checkMultiStart(fi, f);
    checkMulti("source1", null, "some context", null, null, comment);
    checkMulti("source2", null, "", null, null, null);
    checkMulti("source3", null, "", null, null, null);
    checkMulti("source1", null, "", null, null, null);
    checkMulti("source1", null, "other context", null, null, null);
    checkMulti("source4", null, "one more context", null, null, null);
    checkMulti("source4", null, "one more context[1]", null, null, StringUtil.format(OStrings.getString("POFILTER_PLURAL_FORM_COMMENT"), 1) + "\n");
    checkMulti("source4", null, "one more context[2]", null, null, StringUtil.format(OStrings.getString("POFILTER_PLURAL_FORM_COMMENT"), 2) + "\n");
    checkMulti("source5", null, "", null, null, null);
    checkMulti("source6", null, "", null, null, null);
    checkMultiEnd();
    ExternalTMX tmEntries = fi.referenceEntries;
    assertEquals(2, tmEntries.getEntries().size());
    {
        PrepareTMXEntry entry = tmEntries.getEntries().get(0);
        assertEquals("True fuzzy!", entry.source);
        assertEquals("trans5", entry.translation);
    }
    {
        PrepareTMXEntry entry = tmEntries.getEntries().get(1);
        assertEquals("True fuzzy 2!", entry.source);
        assertEquals("trans6", entry.translation);
    }
}
Also used : PoFilter(org.omegat.filters2.po.PoFilter) ExternalTMX(org.omegat.core.data.ExternalTMX) TreeMap(java.util.TreeMap) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) Test(org.junit.Test)

Example 8 with PrepareTMXEntry

use of org.omegat.core.data.PrepareTMXEntry in project omegat by omegat-org.

the class Main method runConsoleAlign.

public static int runConsoleAlign() throws Exception {
    Log.log("Console alignment mode");
    Log.log("");
    if (projectLocation == null) {
        System.out.println(OStrings.getString("PP_ERROR_UNABLE_TO_READ_PROJECT_FILE"));
        return 1;
    }
    String dir = PARAMS.get(CLIParameters.ALIGNDIR);
    if (dir == null) {
        System.out.println(OStrings.getString("CONSOLE_TRANSLATED_FILES_LOC_UNDEFINED"));
        return 1;
    }
    System.out.println(OStrings.getString("CONSOLE_INITIALIZING"));
    Core.initializeConsole(PARAMS);
    RealProject p = selectProjectConsoleMode(true);
    validateTagsConsoleMode();
    System.out.println(StringUtil.format(OStrings.getString("CONSOLE_ALIGN_AGAINST"), dir));
    Map<String, TMXEntry> data = p.align(p.getProjectProperties(), new File(dir));
    Map<String, PrepareTMXEntry> result = new TreeMap<>();
    for (Map.Entry<String, TMXEntry> en : data.entrySet()) {
        result.put(en.getKey(), new PrepareTMXEntry(en.getValue()));
    }
    String tmxFile = p.getProjectProperties().getProjectInternal() + "align.tmx";
    TMXWriter.buildTMXFile(tmxFile, false, false, p.getProjectProperties(), result);
    p.closeProject();
    System.out.println(OStrings.getString("CONSOLE_FINISHED"));
    return 0;
}
Also used : RealProject(org.omegat.core.data.RealProject) TreeMap(java.util.TreeMap) File(java.io.File) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) TMXEntry(org.omegat.core.data.TMXEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry)

Example 9 with PrepareTMXEntry

use of org.omegat.core.data.PrepareTMXEntry in project omegat by omegat-org.

the class ReplaceFilter method replaceAll.

/**
 * Replace all occurrences in all entries.
 */
public void replaceAll() {
    for (SourceTextEntry ste : entries.values()) {
        TMXEntry en = Core.getProject().getTranslationInfo(ste);
        String trans = getEntryText(ste, en);
        if (trans == null) {
            continue;
        }
        // Avoid to replace more than once with variables when entries have duplicates
        if ((en.defaultTranslation) && (ste.getDuplicate() == SourceTextEntry.DUPLICATE.NEXT)) {
            // Already replaced when we parsed the first entry
            continue;
        }
        List<SearchMatch> found = getReplacementsForEntry(trans);
        if (found != null) {
            int off = 0;
            StringBuilder o = new StringBuilder(trans);
            for (SearchMatch m : found) {
                o.replace(m.getStart() + off, m.getEnd() + off, m.getReplacement());
                off += m.getReplacement().length() - m.getLength();
            }
            PrepareTMXEntry prepare = new PrepareTMXEntry(en);
            prepare.translation = o.toString();
            Core.getProject().setTranslation(ste, prepare, en.defaultTranslation, null);
        }
    }
    EditorController ec = (EditorController) Core.getEditor();
    ec.refreshEntries(entries.keySet());
}
Also used : SearchMatch(org.omegat.core.search.SearchMatch) EditorController(org.omegat.gui.editor.EditorController) SourceTextEntry(org.omegat.core.data.SourceTextEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) PrepareTMXEntry(org.omegat.core.data.PrepareTMXEntry) TMXEntry(org.omegat.core.data.TMXEntry)

Aggregations

PrepareTMXEntry (org.omegat.core.data.PrepareTMXEntry)9 SourceTextEntry (org.omegat.core.data.SourceTextEntry)5 TMXEntry (org.omegat.core.data.TMXEntry)5 Map (java.util.Map)4 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 TreeMap (java.util.TreeMap)2 EntryKey (org.omegat.core.data.EntryKey)2 ExternalTMX (org.omegat.core.data.ExternalTMX)2 ProjectProperties (org.omegat.core.data.ProjectProperties)2 RealProject (org.omegat.core.data.RealProject)2 Language (org.omegat.util.Language)2 Point (java.awt.Point)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1