Advertisement
KosIvantsov

write_xliff.groovy

Nov 27th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 4.25 KB | None | 0 0
  1. /*
  2.  * @author: Kos Ivantsov
  3.  * @date:   2014-01-15
  4.  * @version:    0.3
  5.  *
  6.  */
  7.  
  8. import static javax.swing.JOptionPane.*
  9. import static org.omegat.util.Platform.*
  10. import org.omegat.util.StaticUtils
  11.  
  12. def prop = project.projectProperties
  13. if (!prop) {
  14.     final def title = 'Export project to XLIFF file(s)'
  15.     final def msg   = 'Please try again after you open a project.'
  16.     showMessageDialog null, msg, title, INFORMATION_MESSAGE
  17.     return
  18. }
  19. def folder = prop.projectRoot+'script_output/'
  20. xliff_file = new File(folder+'project.xlf')
  21. // create folder if it doesn't exist
  22. if (! (new File (folder)).exists()) {
  23.     (new File(folder)).mkdir()
  24.     }
  25. count = 0
  26. ignorecount = 0
  27. transcount = 0
  28. writecount = 0
  29.  
  30. def sourceLocale = prop.getSourceLanguage().toString().toLowerCase()
  31. def targetLocale = prop.getTargetLanguage().toString().toLowerCase()
  32. xliff_file.write("""<?xml version="1.0" encoding="UTF-8"?>
  33. <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
  34. """, 'UTF-8')
  35.  
  36. files = project.projectFiles
  37.     for (i in 0 ..< files.size())
  38.     {
  39.         fi = files[i]
  40.         xliff_file.append("""  <file original="$fi.filePath" source-language="$sourceLocale" target-language="$targetLocale" datatype="x-application/x-tmx+xml">
  41.    <body>
  42.      <trans-unit id="0" approved="yes">
  43.        <source xml:lang="$sourceLocale"><ph id="filename">==FILENAME: "$fi.filePath"==</ph>
  44.        </source>
  45.        <target xml:lang="$targetLocale" state="final"><ph id="filename">==FILENAME: "$fi.filePath"==</ph>
  46.        </target>
  47.      </trans-unit>
  48. """, 'UTF-8')
  49.         for (j in 0 ..< fi.entries.size())
  50.         {
  51.             def state
  52.             def approved = ''
  53.             def unitnote = ''
  54.             def ignore = ''
  55.             ste = fi.entries[j]
  56.             seg_num = ste.entryNum()
  57.             source = ste.getSrcText()
  58.             info = project.getTranslationInfo(ste)
  59.             target = info ? info.translation : null
  60.             if (target == null){
  61.             state = 'state="needs-translation"'
  62.             target = "$source"
  63.             }else{
  64.             approved = ' approved="yes"'
  65.             state = 'state="final" state-qualifier="exact-match"'
  66.             transcount++
  67.             }
  68.             if (target.size() == 0 ){
  69.             target = "<EMPTY>"
  70.             }
  71.             if (info.hasNote()) {
  72.             unitnote = "\n        <note>${StaticUtils.makeValidXML(info.note)}</note>"
  73.             }
  74.             if (source ==~ /(<\/?[a-z]+[0-9]* ?\/?>){1,5}/ ){
  75.             ignoresource = source
  76.             ignore = 'yes'
  77.             }
  78.             source = source.replaceAll(/(<)(\/?[a-z]+[0-9]* ?\/?)(>)/, /zzz$2zzz/).replaceAll(/</, /zzz#LESSTHEN#zzz/).replaceAll(/>/, /zzz#GREATERTHEN#zzz/).replaceAll(/(zzz)(\/?[a-z]+[0-9]* ?\/?)(zzz)/, /<$2>/)
  79.            
  80.             target = target.replaceAll(/(<)(\/?[a-z]+[0-9]* ?\/?)(>)/, /zzz$2zzz/).replaceAll(/</, /zzz#LESSTHEN#zzz/).replaceAll(/>/, /zzz#GREATERTHEN#zzz/).replaceAll(/(zzz)(\/?[a-z]+[0-9]* ?\/?)(zzz)/, /<$2>/)
  81.            
  82.             source = StaticUtils.makeValidXML(source).replaceAll(/&lt;/, /<ph>&lt;/).replaceAll(/&gt;/, /&gt;<\/ph>/).replaceAll(/zzz#LESSTHEN#zzz/, /&lt;/).replaceAll(/zzz#GREATERTHEN#zzz/, /&gt;/)
  83.            
  84.             target = StaticUtils.makeValidXML(target).replaceAll(/&lt;/, /<ph>&lt;/).replaceAll(/&gt;/, /&gt;<\/ph>/).replaceAll(/zzz#LESSTHEN#zzz/, /&lt;/).replaceAll(/zzz#GREATERTHEN#zzz/, /&gt;/)
  85.            
  86.             tagnumber = source.findAll(/<ph>/).size()
  87.             if (tagnumber > 0) {
  88.                 tgnum = 0
  89.                 while (tgnum++ <= tagnumber) {
  90.                 source = source.replaceFirst(/<ph>/, "<ph id=\"$tgnum\">")
  91.                 target = target.replaceFirst(/<ph>/, "<ph id=\"$tgnum\">")
  92.                 //console.println "count: "+tagnumber+"\n"+source
  93.                 }
  94.             }
  95.            
  96.             if (ignore != 'yes'){
  97.             xliff_file.append("""\
  98.      <trans-unit id="$seg_num"$approved>
  99.        <source xml:lang="$sourceLocale">$source</source>
  100.        <seg-source><mrk mid="0" mtype="seg">$source</mrk></seg-source>
  101.        <target $state xml:lang="$targetLocale"><mrk mid="0" mtype="seg">$target</mrk></target>$unitnote
  102.      </trans-unit>
  103. """, 'UTF-8')
  104.             writecount++
  105.             }else{
  106.                 ignorecount++
  107.             }
  108.         count++
  109.         }
  110.         xliff_file.append("    </body>\n  </file>\n", 'UTF-8')
  111.     }
  112. xliff_file.append("</xliff>", 'UTF-8')
  113. console.println """
  114. ${'*'*(xliff_file.toString().size()+12)}
  115. Output file:   $xliff_file
  116. ${'*'*(xliff_file.toString().size()+12)}
  117. Segments processed: $count
  118. Segments written:   $writecount
  119. Segments not written:   $ignorecount
  120. Translated segments written:    $transcount
  121. Untranslated segments written:  ${writecount-transcount}
  122. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement