Search in sources :

Example 1 with ITranslateCallback

use of org.omegat.filters2.ITranslateCallback in project omegat by omegat-org.

the class FilterMaster method translateFile.

/**
 * OmegaT core calls this method to translate a source file.
 * <ul>
 * <li>OmegaT first looks through registered filter instances to find filter(s) that can handle this file.
 * <li>Tests if filter(s) want to handle it.
 * <li>If the filter accepts the file,
 * <li>Filter is asked to process the file.
 * </ul>
 * If no filter is found, that processes this file, we simply copy it to target folder.
 *
 * @param sourcedir
 *            The folder of the source inFile.
 * @param filename
 *            The name of the source inFile to process (only the part, relative to source folder).
 * @param targetdir
 *            The folder to place the translated inFile to.
 * @param fc
 *            Filter context.
 */
public void translateFile(String sourcedir, String filename, String targetdir, FilterContext fc, ITranslateCallback translateCallback) throws IOException, TranslationException {
    LookupInformation lookup = lookupFilter(new File(sourcedir, filename), fc);
    if (lookup == null) {
        // The file is not supported by any of the filters.
        // Copying it
        FileUtils.copyFile(new File(sourcedir, filename), new File(targetdir, filename));
        return;
    }
    File inFile = new File(sourcedir, filename).getCanonicalFile();
    File outFile = new File(targetdir, getTargetForSource(filename, lookup, fc.getTargetLang())).getCanonicalFile();
    if (inFile.equals(outFile)) {
        throw new TranslationException(StringUtil.format(OStrings.getString("FILTERMASTER_ERROR_SRC_TRG_SAME_FILE"), inFile.getPath()));
    }
    fc.setInEncoding(lookup.outFilesInfo.getSourceEncoding());
    fc.setOutEncoding(lookup.outFilesInfo.getTargetEncoding());
    IFilter filterObject = lookup.filterObject;
    try {
        filterObject.translateFile(inFile, outFile, lookup.config, fc, translateCallback);
    } catch (Exception ex) {
        Log.log(ex);
    }
}
Also used : IFilter(org.omegat.filters2.IFilter) TranslationException(org.omegat.filters2.TranslationException) File(java.io.File) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException)

Example 2 with ITranslateCallback

use of org.omegat.filters2.ITranslateCallback in project omegat by omegat-org.

the class FilterMaster method loadFile.

/**
 * OmegaT core calls this method to load a source file.
 *
 * @param filename
 *            The name of the source file to load.
 * @return Whether the file was handled by one of OmegaT filters.
 * @see #translateFile(String, String, String, FilterContext,
 *      ITranslateCallback)
 */
public IFilter loadFile(String filename, FilterContext fc, IParseCallback parseCallback) throws IOException, TranslationException {
    IFilter filterObject = null;
    try {
        LookupInformation lookup = lookupFilter(new File(filename), fc);
        if (lookup == null) {
            return null;
        }
        File inFile = new File(filename);
        fc.setInEncoding(lookup.outFilesInfo.getSourceEncoding());
        fc.setOutEncoding(lookup.outFilesInfo.getTargetEncoding());
        filterObject = lookup.filterObject;
        filterObject.parseFile(inFile, lookup.config, fc, parseCallback);
    } catch (Exception ioe) {
        throw new IOException(filename + "\n" + ioe, ioe);
    }
    return filterObject;
}
Also used : IFilter(org.omegat.filters2.IFilter) IOException(java.io.IOException) File(java.io.File) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException)

Example 3 with ITranslateCallback

use of org.omegat.filters2.ITranslateCallback in project omegat by omegat-org.

the class TmxComplianceBase method translateUsingTmx.

protected void translateUsingTmx(IFilter filter, Map<String, String> config, final String fileTextIn, String inCharset, String fileTMX, String outCharset, ProjectProperties props, Map<String, TMXEntry> tmxPatch) throws Exception {
    final ProjectTMX tmx = new ProjectTMX(props.getSourceLanguage(), props.getTargetLanguage(), props.isSentenceSegmentingEnabled(), new File("test/data/tmx/TMXComplianceKit/" + fileTMX), orphanedCallback);
    if (tmxPatch != null) {
        tmx.defaults.putAll(tmxPatch);
    }
    FilterContext fc = new FilterContext(props);
    fc.setInEncoding(inCharset);
    fc.setOutEncoding(outCharset);
    ITranslateCallback cb = new TranslateEntry(props) {

        @Override
        protected String getSegmentTranslation(String id, int segmentIndex, String segmentSource, String prevSegment, String nextSegment, String path) {
            TMXEntry e = tmx.getDefaultTranslation(segmentSource);
            assertNotNull(e);
            return e.translation;
        }

        @Override
        String getCurrentFile() {
            return fileTextIn;
        }
    };
    filter.translateFile(new File("test/data/tmx/TMXComplianceKit/" + fileTextIn), outFile, config, fc, cb);
}
Also used : ITranslateCallback(org.omegat.filters2.ITranslateCallback) File(java.io.File) FilterContext(org.omegat.filters2.FilterContext)

Example 4 with ITranslateCallback

use of org.omegat.filters2.ITranslateCallback in project omegat by omegat-org.

the class XLIFFFilterTest method testTags.

@Test
public void testTags() throws Exception {
    String f = "test/data/filters/xliff/file-XLIFFFilter-tags.xlf";
    IProject.FileInfo fi = loadSourceFiles(filter, f);
    SourceTextEntry ste;
    checkMultiStart(fi, f);
    // #1988732
    checkMultiNoPrevNext("Link to <m0>http://localhost</m0>.", null, null, null);
    // #1988732
    checkMultiNoPrevNext("About <b0>Gandalf</b0>", null, null, "7");
    checkMultiNoPrevNext("<i0>Tags</i0> translation zz<i1>2</i1>z <b2>-NONTRANSLATED", null, null, null);
    checkMultiNoPrevNext("one <a0> two </b1> three <c2> four </d3> five", null, null, null);
    ste = checkMultiNoPrevNext("About <m0>Gandalf</m0> and <m1>other</m1>.", null, null, null);
    assertEquals(3, ste.getProtectedParts().length);
    assertEquals("<m0>Gandalf</m0>", ste.getProtectedParts()[0].getTextInSourceSegment());
    assertEquals("<mrk mtype=\"protected\">Gandalf</mrk>", ste.getProtectedParts()[0].getDetailsFromSourceFile());
    assertEquals("Gandalf", ste.getProtectedParts()[0].getReplacementMatchCalculation());
    assertEquals("<m1>", ste.getProtectedParts()[1].getTextInSourceSegment());
    assertEquals("<mrk mtype=\"other\">", ste.getProtectedParts()[1].getDetailsFromSourceFile());
    assertEquals(StaticUtils.TAG_REPLACEMENT, ste.getProtectedParts()[1].getReplacementMatchCalculation());
    assertEquals("</m1>", ste.getProtectedParts()[2].getTextInSourceSegment());
    assertEquals("</mrk>", ste.getProtectedParts()[2].getDetailsFromSourceFile());
    assertEquals(StaticUtils.TAG_REPLACEMENT, ste.getProtectedParts()[2].getReplacementMatchCalculation());
    checkMultiNoPrevNext("one <o0>two</o0> three", null, null, null);
    checkMultiNoPrevNext("one <t0/> three", null, null, null);
    checkMultiNoPrevNext("one <w0/> three", null, null, null);
    checkMultiNoPrevNext("Nested tags: before <g0><g1><x2/></g1></g0> after", null, null, null);
    checkMultiNoPrevNext("<m0>Check protected-only tag reading</m0>", null, null, null);
    checkMultiEnd();
    File inFile = new File("test/data/filters/xliff/file-XLIFFFilter-tags.xlf");
    filter.translateFile(inFile, outFile, new TreeMap<String, String>(), context, new ITranslateCallback() {

        public String getTranslation(String id, String source, String path) {
            return source.replace("NONTRANSLATED", "TRANSLATED");
        }

        public String getTranslation(String id, String source) {
            return source.replace("NONTRANSLATED", "TRANSLATED");
        }

        public void linkPrevNextSegments() {
        }

        public void setPass(int pass) {
        }
    });
    File trFile = new File(outFile.getPath() + "-translated");
    List<String> lines = Files.lines(inFile.toPath()).map(line -> line.replace("NONTRANSLATED", "TRANSLATED")).collect(Collectors.toList());
    Files.write(trFile.toPath(), lines);
    compareXML(trFile, outFile);
}
Also used : SourceTextEntry(org.omegat.core.data.SourceTextEntry) URISyntaxException(java.net.URISyntaxException) StatCount(org.omegat.core.statistics.StatCount) PatternConsts(org.omegat.util.PatternConsts) StaticUtils(org.omegat.util.StaticUtils) TranslationException(org.omegat.filters2.TranslationException) Core(org.omegat.core.Core) Assert.fail(org.junit.Assert.fail) XLIFFFilter(org.omegat.filters3.xml.xliff.XLIFFFilter) Before(org.junit.Before) IProject(org.omegat.core.data.IProject) ITranslateCallback(org.omegat.filters2.ITranslateCallback) Files(java.nio.file.Files) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) XLIFFOptions(org.omegat.filters3.xml.xliff.XLIFFOptions) Collectors(java.util.stream.Collectors) File(java.io.File) StatisticsSettings(org.omegat.core.statistics.StatisticsSettings) List(java.util.List) XLIFFDialect(org.omegat.filters3.xml.xliff.XLIFFDialect) TreeMap(java.util.TreeMap) Assert.assertFalse(org.junit.Assert.assertFalse) SAXException(org.xml.sax.SAXException) Preferences(org.omegat.util.Preferences) Assert.assertEquals(org.junit.Assert.assertEquals) ITranslateCallback(org.omegat.filters2.ITranslateCallback) SourceTextEntry(org.omegat.core.data.SourceTextEntry) File(java.io.File) IProject(org.omegat.core.data.IProject) Test(org.junit.Test)

Aggregations

File (java.io.File)4 TranslationException (org.omegat.filters2.TranslationException)3 IOException (java.io.IOException)2 IFilter (org.omegat.filters2.IFilter)2 ITranslateCallback (org.omegat.filters2.ITranslateCallback)2 URISyntaxException (java.net.URISyntaxException)1 Files (java.nio.file.Files)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 Collectors (java.util.stream.Collectors)1 FileUtils (org.apache.commons.io.FileUtils)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Assert.fail (org.junit.Assert.fail)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Core (org.omegat.core.Core)1 IProject (org.omegat.core.data.IProject)1 SourceTextEntry (org.omegat.core.data.SourceTextEntry)1