Advertisement
KosIvantsov

Write XLIFF (new)

Apr 2nd, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 7.89 KB | None | 0 0
  1. /* :name=Write XLIFF (new) :description=
  2.  * @Purpose:    Export the whole project to XLF file for other CAT tools
  3.  * @author:     Kos Ivantsov
  4.  * @date:       2016-04-02
  5.  * @version:    1.0
  6.  */
  7.  
  8.  
  9. /* set to true to write a settings file for Okapi Rainbow that can be
  10.  * used to convert the XLF file produced by this script, to TMX.
  11.  * Otherwise set to false    */
  12. rainbow = true
  13.  
  14. /* set to true to output only approved entries from XLF to TMX during
  15.  * conversion in Rainbow    */
  16. get_only_approved = true
  17.  
  18. /* add xml:space='preserve' to each TU in the resultant XLF */
  19. preserve_spaces = true
  20.  
  21. /* Changing this variable lets the user specify
  22.  * what to do with untranslated segments
  23.  * "copy" for copying source to target
  24.  * "empty" for leaving the target empty
  25.  * "ignore" for not including target element    */
  26. xliffUntranslated = "copy"
  27.  
  28. /* The value set for the following variable will
  29.  * be inserted in empty translations as target   */
  30. emptyTranslation = ''
  31.  
  32.  
  33. import static javax.swing.JOptionPane.*
  34. import static org.omegat.util.Platform.*
  35. import org.omegat.util.StringUtil
  36. import org.omegat.core.data.ProtectedPart
  37.  
  38. def prop = project.projectProperties
  39. if (!prop) {
  40.     final def title = 'Export project to XLIFF file(s)'
  41.     final def msg   = 'Please try again after you open a project.'
  42.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  43.     return
  44. }
  45. def folder = prop.projectRoot+'script_output'+File.separator
  46. projname = new File(prop.getProjectRoot()).getName()
  47. // create folder if it doesn't exist
  48. if (! (new File (folder)).exists()) {
  49.     (new File(folder)).mkdir()
  50.     }
  51. filename = folder + projname +'.xlf'
  52. xliff_file = new File(filename).newWriter('UTF-8')
  53.  
  54. count = transcount = untranscount = emptytranscount = ignorecount = writecount = 0
  55.  
  56. sourceLocale = prop.getSourceLanguage().toString().toLowerCase()
  57. targetLocale = prop.getTargetLanguage().toString().toLowerCase()
  58. xmlspace = preserve_spaces ? "xml:space=\'preserve\'" : ""
  59.  
  60. xliff_file.write("""<?xml version="1.0" encoding="UTF-8"?>
  61. <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
  62. """)
  63.  
  64. files = project.projectFiles
  65. //files = project.projectFiles.subList(editor.@displayedFileIndex, editor.@displayedFileIndex + 1);
  66.     for (i in 0 ..< files.size())
  67.     {
  68.         fi = files[i]
  69.         xliff_file.append("""  <file original="$fi.filePath" source-language="$sourceLocale" target-language="$targetLocale" datatype="x-application/x-tmx+xml">
  70.    <body>
  71.      <trans-unit id="0" approved="yes">
  72.        <source xml:lang="$sourceLocale"><ph id="filename">==FILENAME: "$fi.filePath"==</ph>
  73.        </source>
  74.        <target xml:lang="$targetLocale" state="final"><ph id="filename">==FILENAME: "$fi.filePath"==</ph>
  75.        </target>
  76.      </trans-unit>\n""")
  77.         for (j in 0 ..< fi.entries.size()) {
  78.             def state
  79.             def approved = ''
  80.             def unitnote = ''
  81.             def ignore = ''
  82.             ste = fi.entries[j]
  83.             seg_num = ste.entryNum()
  84.             source = ste.getSrcText()
  85.             info = project.getTranslationInfo(ste)
  86.             target = info ? info.translation : null
  87.             def alltags = ''
  88.             List xmlTags = []
  89.             for (ProtectedPart pp : ste.getProtectedParts()) {
  90.                 alltags += pp.getTextInSourceSegment()
  91.                 xmlTags.add(StringUtil.makeValidXML(pp.getTextInSourceSegment()))
  92.             }
  93.             if (source == alltags) {
  94.                 count++
  95.                 ignorecount++
  96.                 continue
  97.             }
  98.             if (target != null) {
  99.                 approved = 'approved="yes"'
  100.                 state = 'state="final" state-qualifier="exact-match"'
  101.                 transcount++
  102.                 if (target.size() == 0 ) {
  103.                     target = emptyTranslation
  104.                     emptytranscount++
  105.                     }
  106.             }else{
  107.                 state = 'state="needs-translation"'
  108.                 switch (xliffUntranslated) {
  109.                     case "copy" :
  110.                         target = source
  111.                         break
  112.                     case ["empty", "ignore"] :
  113.                         target = ''
  114.                         break
  115.                     default :
  116.                         target =  source
  117.                         break
  118.                 }
  119.                 untranscount++
  120.             }
  121.             unitnote = (info.hasNote()) ? "\n        <note>${StringUtil.makeValidXML(info.note)}</note>" : ""
  122.             if (source != alltags){
  123.                 source = StringUtil.makeValidXML(source)
  124.                 target = StringUtil.makeValidXML(target)
  125.                 xmlTags = xmlTags.unique()
  126.                 for (x in 0 ..< xmlTags.size()){
  127.                     source =  source.replace(xmlTags[x], "<ph id=\"$x\">" + xmlTags[x].toString() + "</ph>")
  128.                     target =  target.replace(xmlTags[x], "<ph id=\"$x\">" + xmlTags[x].toString() + "</ph>")
  129.                 }
  130.                 targetLine = (target == '' && xliffUntranslated == "ignore") ? '' : "\n        <target $state xml:lang=\"$targetLocale\"><mrk mid=\"0\" mtype=\"seg\">$target</mrk></target>"
  131.                 xliff_file.append("""\
  132.      <trans-unit id="$seg_num" $xmlspace $approved>
  133.        <source xml:lang="$sourceLocale">$source</source>
  134.        <seg-source><mrk mid="0" mtype="seg">$source</mrk></seg-source>\
  135. $targetLine\
  136. $unitnote
  137.      </trans-unit>\n""")
  138.                 writecount++
  139.             }
  140.         count++
  141.         }
  142.         xliff_file.append("    </body>\n  </file>\n")
  143.     }
  144. xliff_file.append("</xliff>")
  145. xliff_file.close()
  146. console.clear()
  147. console.println """\
  148. ${'*'*(filename.toString().size()+15)}
  149. Output file:   $filename
  150. ${'*'*(filename.toString().size()+15)}
  151. Segments processed: $count
  152. Translated: $transcount
  153. Untranslated: $untranscount
  154. Empty translation: $emptytranscount
  155. Segments written: $writecount
  156. Segments ignored: $ignorecount
  157. """
  158. mainWindow.statusLabel.setText("XLIFF Export: $filename written")
  159. Timer timer = new Timer().schedule({mainWindow.statusLabel.setText(null); console.clear()} as TimerTask, 10000)
  160.  
  161. if (rainbow == true) {
  162.     def approved = get_only_approved ? 'true' : 'false'
  163.     rainbowfile = new File(folder + projname +'.xlf2tmx.rnb')
  164.     rainbowfile.write("""\
  165. <?xml version="1.0" encoding="UTF-8"?>
  166. <rainbowProject version="4">
  167.    <fileSet id="1">
  168.        <root useCustom="0"></root>
  169.    </fileSet>
  170.    <fileSet id="2">
  171.        <root useCustom="0"></root>
  172.    </fileSet>
  173.    <fileSet id="3">
  174.        <root useCustom="0"></root>
  175.    </fileSet>
  176.    <output>
  177.        <root use="0"></root>
  178.        <subFolder use="0"></subFolder>
  179.        <extension use="1" style="0">.out</extension>
  180.        <replace use="0" oldText="" newText=""></replace>
  181.        <prefix use="0"></prefix>
  182.        <suffix use="0"></suffix>
  183.    </output>
  184.    <options sourceLanguage="$sourceLocale" sourceEncoding="UTF-8" targetLanguage="$targetLocale" targetEncoding="UTF-8"></options>
  185.    <parametersFolder useCustom="0"></parametersFolder>
  186.    <utilities xml:spaces="preserve"><params id="currentProjectPipeline">&lt;?xml version="1.0" encoding="UTF-8"?>
  187. &lt;rainbowPipeline version="1">&lt;step class="net.sf.okapi.steps.common.RawDocumentToFilterEventsStep">&lt;/step>
  188. &lt;step class="net.sf.okapi.steps.codesremoval.CodesRemovalStep">#v1
  189. stripSource.b=true
  190. stripTarget.b=true
  191. mode.i=0
  192. includeNonTranslatable.b=true
  193. replaceWithSpace.b=false&lt;/step>
  194. &lt;step class="net.sf.okapi.steps.formatconversion.FormatConversionStep">#v1
  195. singleOutput.b=false
  196. autoExtensions.b=true
  197. targetStyle.i=0
  198. outputPath=
  199. outputFormat=tmx
  200. useGenericCodes.b=false
  201. skipEntriesWithoutText.b=true
  202. approvedEntriesOnly.b=$approved
  203. overwriteSameSource.b=false&lt;/step>
  204. &lt;step class="net.sf.okapi.steps.common.FilterEventsToRawDocumentStep">&lt;/step>
  205. &lt;/rainbowPipeline>
  206. </params></utilities>
  207. </rainbowProject>
  208. """, 'UTF-8')
  209.     }
  210. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement