Skip to content

Instantly share code, notes, and snippets.

@kosivantsov
Created November 17, 2023 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosivantsov/dc6e5c6daf4379f356ca1c87394a2278 to your computer and use it in GitHub Desktop.
Save kosivantsov/dc6e5c6daf4379f356ca1c87394a2278 to your computer and use it in GitHub Desktop.
setLatestTranslation.groovy
/* :name = Set Latest Translations :description=
*
* @author Manuel Souto Pico
* @creation 2023.11.06
* @edit 2023.11.17
* @version 0.0.2
*/
import static javax.swing.JOptionPane.*
import static org.omegat.util.Platform.*
//Since gui() is executed anyway, continueScript will change to false when needed
continueScript = true
// path to the folder inside the TM folder
path_to_tmx_dir = "auto/"
props = project.projectProperties
if (!props) {
final def title = "Set Latest Translations"
final def msg = 'No project opened.'
showMessageDialog(null, msg, title, INFORMATION_MESSAGE)
continueScript = false
return
}
def gui() {
curEntry = editor.getCurrentEntryNumber()
//Jump to the dummy file to avoid conflict resolution dialog
dummyFileName = "dummy_file.omegat.xml"
dummyFile = new File(props.getSourceRoot() + dummyFileName)
if (! dummyFile.exists()) {
console.println("Dummy file doesn't exist")
//continueScript = false
//return
} else {
projectFiles = project.getProjectFiles()
dummyFileIndex = projectFiles.findIndexOf { it.filePath == dummyFileName }
dummyEntry = projectFiles[dummyFileIndex].entries[0]
editor.gotoEntry(dummyEntry.entryNum())
}
if (!continueScript) {
return
}
project.transMemories.each { name, tmx ->
name = name.substring(props.getTMRoot().length())
if (name.startsWith(path_to_tmx_dir)) {
console.println("Importing from " + name)
tmx.entries.each { entry ->
// Search which entry in the project corresponds to the one in the tmx
// Note: to be improved, for the moment it works only with default entries
// and it is not optimized, we should use a cache as in ImportFromAutoTMX
def inProject = null
project.allEntries.each { pe ->
if (pe.srcText.equals(entry.source)) inProject = pe;
}
// Now search is done, if we found something we use it
if ((inProject != null) && (entry.source.equals(inProject.srcText))) {
def inProjectEntry = project.getTranslationInfo(inProject)
if ((inProjectEntry != null) && (entry.source.equals(inProjectEntry.source))) {
long inProjectDate = inProjectEntry.creationDate
if (inProjectEntry.changeDate > inProjectEntry.creationDate) {
inProjectDate = inProjectEntry.changeDate
}
long inTmxDate = entry.creationDate
if (entry.changeDate > entry.creationDate) {
inTmxDate = entry.changeDate
}
console.println(entry.source + " " + inTmxDate + " / " + inProjectDate + " => " + (inTmxDate > inProjectDate));
if (inTmxDate > inProjectDate) {
project.setTranslation(inProject, entry, true, null) // org.omegat.core.data.TMXEntry.ExternalLinked.xAUTO);
}
}
}
}
}
}
editor.gotoEntry(curEntry)
org.omegat.gui.main.ProjectUICommands.projectReload()
}
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment