Skip to content

Commit

Permalink
add an option to use quick tmx importing
Browse files Browse the repository at this point in the history
  • Loading branch information
xulihang committed Oct 30, 2021
1 parent e62fdd9 commit c1f3a4e
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 22 deletions.
25 changes: 13 additions & 12 deletions BasicCAT/BasicCAT.b4j
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,23 @@ Module68=TMDB
Module69=TMEditor
Module7=editDistance
Module70=TMManager
Module71=TMX
Module72=txtFilter
Module73=Utils
Module74=viewSegment
Module75=xliffFilter
Module76=Xml2Map
Module77=XMLBuilder2
Module78=XmlNode
Module79=XmlParser
Module71=TMXExporter
Module72=TMXImporter
Module73=txtFilter
Module74=Utils
Module75=viewSegment
Module76=xliffFilter
Module77=Xml2Map
Module78=XMLBuilder2
Module79=XmlNode
Module8=EnumClass
Module80=XMLUtils
Module81=zip4j
Module80=XmlParser
Module81=XMLUtils
Module82=zip4j
Module9=ErrorReporter
NumberOfFiles=88
NumberOfLibraries=26
NumberOfModules=81
NumberOfModules=82
Version=8.9
@EndOfDesignText@
#Region Project Attributes
Expand Down
7 changes: 5 additions & 2 deletions BasicCAT/BasicCAT.b4j.meta
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ ModuleBookmarks79=
ModuleBookmarks8=
ModuleBookmarks80=
ModuleBookmarks81=
ModuleBookmarks82=
ModuleBookmarks9=
ModuleBreakpoints0=
ModuleBreakpoints1=
Expand Down Expand Up @@ -161,6 +162,7 @@ ModuleBreakpoints79=
ModuleBreakpoints8=
ModuleBreakpoints80=
ModuleBreakpoints81=
ModuleBreakpoints82=
ModuleBreakpoints9=
ModuleClosedNodes0=
ModuleClosedNodes1=
Expand Down Expand Up @@ -243,7 +245,8 @@ ModuleClosedNodes79=
ModuleClosedNodes8=
ModuleClosedNodes80=
ModuleClosedNodes81=
ModuleClosedNodes82=
ModuleClosedNodes9=
NavigationStack=opennlp,Initialize,36,6,Term,termsInASentenceUsingIteration,323,0,Term,termsInASentenceUsingHashMap,287,4,xliffFilter,createWorkFile,66,0,xliffFilter,generateFile,296,0,xliffFilter,updateTransUnit,391,0,xliffFilter,updateNode,381,0,xliffFilter,insertTranslation,375,0,xliffFilter,buildMrk,439,0,Project,updateWithWorkfileMI_Action,893,0,Project,updateSegmentsWithWorkfile,931,6
NavigationStack=Main,Process_Globals,71,0,TM,importExternalTranslationMemory,196,6,TMXImporter,importedList,77,6,TMXImporter,importedListQuick,92,0,TMXImporter,Parser_StartElement,35,0,TMManager,exportToFile,211,0,importDialog,loadTerm,115,0,ProjectSettings,Class_Globals,0,0,Main,MenuBar1_Action,398,0,Project,saveSettings,182,6
SelectedBuild=0
VisibleModules=10,45,51,72,60,25,46,42,41,47,75
VisibleModules=10,45,51,73,60,25,46,42,41,47,72,71,70,20,81,67
2 changes: 1 addition & 1 deletion BasicCAT/Files/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.10.3
1.10.4
11 changes: 10 additions & 1 deletion BasicCAT/Project.bas
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,20 @@ End Sub

Public Sub saveSettings(newsettings As Map)
projectFile.Put("settings",newsettings)
settings = newsettings
Log(newsettings)
save
If newsettings.Get("tmListChanged")="yes" Then
projectTM.deleteExternalTranslationMemory
wait for (projectTM.importExternalTranslationMemory(settings.Get("tmList"),projectFile)) complete (result As Boolean)
Dim tmList As List = settings.Get("tmList")
If tmList.Size>0 Then
Dim response As Int = fx.Msgbox2(Main.MainForm,"Please select a TMX import method:","","Quick (for pure text)","","Accurate (for tagged text)",fx.MSGBOX_CONFIRMATION)
Dim quickMode As Boolean = True
If response = fx.DialogResponse.NEGATIVE Then
quickMode = False
End If
wait for (projectTM.importExternalTranslationMemory(settings.Get("tmList"),projectFile,quickMode)) complete (result As Boolean)
End If
End If
If newsettings.Get("termListChanged")="yes" Then
projectTerm.deleteExternalTerminology
Expand Down
6 changes: 4 additions & 2 deletions BasicCAT/TM.bas
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ Public Sub deleteExternalTranslationMemory
externalTranslationMemory.DeleteAll
End Sub

Public Sub importExternalTranslationMemory(tmList As List,projectFile As Map) As ResumableSub
Public Sub importExternalTranslationMemory(tmList As List,projectFile As Map,quickMode As Boolean) As ResumableSub
progressDialog.Show("Loading external memory","loadtm")
Dim segments As List
segments.Initialize
Expand All @@ -198,7 +198,9 @@ Public Sub importExternalTranslationMemory(tmList As List,projectFile As Map) As
If tmfileLowercase.EndsWith(".txt") Then
segments.AddAll(importedTxt(tmfile))
Else if tmfileLowercase.EndsWith(".tmx") Then
segments.AddAll(TMX.importedList(File.Combine(Main.currentProject.path,"TM"),tmfile,projectFile.Get("source"),projectFile.Get("target")))
Dim importer As TMXImporter
importer.Initialize
segments.AddAll(importer.importedList(File.Combine(Main.currentProject.path,"TM"),tmfile,projectFile.Get("source"),projectFile.Get("target"),quickMode))
else if tmfileLowercase.EndsWith(".xlsx") Then
segments.AddAll(importedXlsx(tmfile))
End If
Expand Down
9 changes: 6 additions & 3 deletions BasicCAT/TMManager.bas
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ Sub exportToFile
segments.Add(bitext)

Next

Dim exporter As TMXExporter
exporter.Initialize
Dim result As Int
result=fx.Msgbox2(frm,"Include tags?","","Yes","Cancel","No",fx.MSGBOX_CONFIRMATION)
If result=fx.DialogResponse.CANCEL Then
Expand All @@ -203,15 +206,15 @@ Sub exportToFile
If path.EndsWith(".tmx") Then
Select result
Case fx.DialogResponse.NEGATIVE
TMX.export(segments,Main.currentProject.projectFile.Get("source"),Main.currentProject.projectFile.Get("target"),path,False,False)
exporter.export(segments,Main.currentProject.projectFile.Get("source"),Main.currentProject.projectFile.Get("target"),path,False,False)
Case fx.DialogResponse.POSITIVE
Dim result2 As Int
result2=fx.Msgbox2(frm,"How to handle tags?","","Keep the original format","Cancel","Conform to TMX Specification",fx.MSGBOX_CONFIRMATION)
Select result2
Case fx.DialogResponse.NEGATIVE
TMX.export(segments,Main.currentProject.projectFile.Get("source"),Main.currentProject.projectFile.Get("target"),path,True,True)
exporter.export(segments,Main.currentProject.projectFile.Get("source"),Main.currentProject.projectFile.Get("target"),path,True,True)
Case fx.DialogResponse.POSITIVE
TMX.export(segments,Main.currentProject.projectFile.Get("source"),Main.currentProject.projectFile.Get("target"),path,True,False)
exporter.export(segments,Main.currentProject.projectFile.Get("source"),Main.currentProject.projectFile.Get("target"),path,True,False)
Case fx.DialogResponse.CANCEL
Return
End Select
Expand Down
156 changes: 156 additions & 0 deletions BasicCAT/TMXExporter.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=8.9
@EndOfDesignText@
Sub Class_Globals
Private fx As JFX
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub


public Sub export(segments As List,sourceLang As String,targetLang As String,path As String,includeTag As Boolean,isTMXTags As Boolean)
Dim tmxNode As XmlNode
tmxNode=CreateNode("tmx")
tmxNode.Attributes.Put("version","1.4")
Dim header As XmlNode
header=CreateNode("header")
header.Attributes.Put("creationtool","BasicCAT")
header.Attributes.Put("creationtoolversion","1.0.0")
header.Attributes.put("adminlang",sourceLang)
header.Attributes.put("srclang",sourceLang)
header.Attributes.put("segtype","sentence")
header.Attributes.put("o-tmf","BasicCAT")
Dim body As XmlNode
body=CreateNode("body")
Dim tuList As List
tuList.Initialize
For Each segment As List In segments
Dim tu As XmlNode
tu=CreateNode("tu")
Dim tuvList As List
tuvList.Initialize
Dim targetMap As Map
targetMap=segment.Get(2)
For i=0 To 1
Dim seg As String=segment.Get(i)
If includeTag=False Then
seg=XMLUtils.TagsRemoved(seg,False)
End If
If i = 1 Then
Dim targetTuv As XmlNode
targetTuv=CreateNode("tuv")
targetTuv.Attributes.Put("xml:lang",targetLang)
If targetMap.ContainsKey("creator") Then
targetTuv.attributes.Put("creationid",targetMap.Get("creator"))
End If
If targetMap.ContainsKey("createdTime") Then
Dim creationDate As String
DateTime.DateFormat="yyyyMMdd"
DateTime.TimeFormat="HHmmss"
creationDate=DateTime.Date(targetMap.Get("createdTime"))&"T"&DateTime.Time(targetMap.Get("createdTime"))&"Z"
targetTuv.attributes.Put("creationdate",creationDate)
End If
Dim segNode As XmlNode
segNode=CreateNode("seg")
setNodeText(segNode,seg,isTMXTags)
targetTuv.Children.Add(segNode)
tuvList.Add(targetTuv)
Else if i = 0 Then
Dim sourceTuv As XmlNode
sourceTuv=CreateNode("tuv")
sourceTuv.Attributes.Put("xml:lang",sourceLang)
Dim segNode As XmlNode
segNode=CreateNode("seg")
setNodeText(segNode,seg,isTMXTags)
sourceTuv.Children.Add(segNode)
tuvList.Add(sourceTuv)
End If
Next
If targetMap.ContainsKey("note") Then
If targetMap.Get("note")<>"" Then
Dim note As XmlNode
note=CreateNode("note")
Dim textNode As XmlNode
textNode=CreateNode("text")
textNode.Text=targetMap.Get("note")
note.Children.Add(textNode)
tu.Children.InsertAt(0,note)
End If
End If
tu.Children.AddAll(tuvList)
tuList.Add(tu)
Next
body.Children=tuList
tmxNode.Children.Add(header)
tmxNode.Children.Add(body)
File.WriteString(path,"",XMLUtils.asString(tmxNode))
End Sub

private Sub setNodeText(node As XmlNode,text As String,isTMXTags As Boolean)
If isTMXTags=True Then
Try
text=XMLUtils.HandleXMLEntities(text,True)
text=Regex.Replace2("`(&lt;.*?&gt;)`",32,text,"$1")
node.innerXML=convertToTMXTags(text)
Return
Catch
Log(LastException)
End Try
End If
node.Children.Clear
Dim textNode As XmlNode
textNode.Initialize
textNode.Name="text"
textNode.Text=text
node.Children.Add(textNode)
End Sub

private Sub convertToTMXTags(xml As String) As String
Dim sb As StringBuilder
sb.Initialize
Dim matcher As Matcher
matcher=Regex.Matcher("</*(.*?)(\d+) */*>",xml)
Dim previousEndIndex As Int=0
Do While matcher.Find
sb.Append(xml.SubString2(previousEndIndex,matcher.GetStart(0)))
previousEndIndex=matcher.GetEnd(0)
If matcher.Group(1).StartsWith("g") Then
Dim id As Int
id=matcher.Group(2)
If matcher.match.Contains("/") Then
sb.Append($"<ept i="${id}">"$)
sb.Append(XMLUtils.EscapeXml(matcher.match))
sb.Append("</ept>")
Else
sb.Append($"<bpt i="${id}">"$)
sb.Append(XMLUtils.EscapeXml(matcher.match))
sb.Append("</bpt>")
End If
Else If matcher.Group(1).StartsWith("x") Then
sb.Append("<ph>")
sb.Append(XMLUtils.EscapeXml(matcher.Match))
sb.Append("</ph>")
Else
sb.Append(matcher.Match)
End If
Loop
If previousEndIndex<>xml.Length-1 Then
sb.Append(xml.SubString2(previousEndIndex,xml.Length))
End If
Return sb.ToString
End Sub

private Sub CreateNode(name As String) As XmlNode
Dim node As XmlNode
node.Initialize
node.Name=name
node.Attributes.Initialize
node.Children.Initialize
Return node
End Sub
Loading

0 comments on commit c1f3a4e

Please sign in to comment.