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);
}
}
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;
}
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);
}
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);
}
Aggregations