Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vdelachaux committed Dec 19, 2024
1 parent af29b97 commit be14fdc
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 958 deletions.
15 changes: 8 additions & 7 deletions Project/Sources/Classes/Preferences.4dm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Class extends _classCore

property version : Text
property file : 4D:C1709.File
property data : Object

Expand All @@ -19,8 +20,7 @@ Class constructor($version : Text)

Else

This:C1470.data:=New object:C1471(\
"version"; This:C1470.version)
This:C1470.data:={version: This:C1470.version}

End if

Expand All @@ -34,10 +34,10 @@ Function set($key : Text; $value)

This:C1470.data[$key]:=$value

This:C1470._save()
This:C1470.save()

// === === === === === === === === === === === === === === === === === === ===
Function _save()
Function save()

This:C1470.file.setText(JSON Stringify:C1217(This:C1470.data; *))

Expand All @@ -62,7 +62,7 @@ Function _update()
$version:="3.0"

//______________________________________________________
: ($version="3.0")
: ($version="3.0") // Update to 3.1 or higher

fixIDsWithSlashes

Expand All @@ -71,14 +71,15 @@ Function _update()
//______________________________________________________
: ($version="3.1") // Update to 3.2 or higher

//

//______________________________________________________
End case

Until ($version=This:C1470.version)

If (This:C1470.data.version#$version)

This:C1470.data.version:=$version
This:C1470._save()
This:C1470.save()

End if
128 changes: 64 additions & 64 deletions Project/Sources/Classes/Xliff.4dm
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
Class extends xml

// MARK: Default values ⚙️
property allUnits : Collection:=[]
property groups : Collection:=[]

// MARK: Constants 🔐
// Extensions
property FILE_EXTENSION : Text:=".xlf"
property FOLDER_EXTENSION : Text:=".lproj"

// Common XPATH
property XPATH_HEADER : Text:="/xliff/header"
property XPATH_PROP_GROUP : Text:="/xliff/file/header/prop-group"
property XPATH_FILE : Text:="/xliff/file"
property XPATH_BODY : Text:="/xliff/file/body"

// State values
property NEW : Text:="new" // The item is new (not in a previous version of the document)
property NEEDS_TRANSLATION : Text:="needs-translation" // The item needs to be translated.
property NEEDS_REVIEW : Text:="needs-review-translation" // Only the text of the item needs to be reviewed.

// MARK: Other 💾
property attributes; language : Object
property groups : Collection
property datatype : Text

property duplicateResname : Boolean
property noTranslate : Boolean
property note : Boolean

property duplicateID; duplicateResname : Boolean

Class constructor($file : 4D:C1709.File)

Expand All @@ -13,25 +36,6 @@ Class constructor($file : 4D:C1709.File)
// Do not close the XML tree when they are saved
This:C1470.autoClose:=False:C215

// MARK:Delegates 📦
This:C1470.group:=cs:C1710.xliffGroup
This:C1470.transunit:=cs:C1710.xliffUnit

// MARK: Common XPATH
This:C1470.headerPath:="/xliff/header"
This:C1470.propPath:="/xliff/file/header/prop-group"
This:C1470.filePath:="/xliff/file"
This:C1470.bodyPath:="/xliff/file/body"

// MARK: Extensions
This:C1470.fileExtension:=".xlf"
This:C1470.folderExtension:=".lproj"

// Mark:State values
This:C1470.new:="new" // The item is new (not in a previous version of the document)
This:C1470.needsTranslation:="needs-translation" // The item needs to be translated.
This:C1470.needsReviewTranslation:="needs-review-translation" // Only the text of the item needs to be reviewed.

// === === === === === === === === === === === === === === === === === === ===
/// Creating and returning a new group
Function createGroup($resname : Text) : cs:C1710.xliffGroup
Expand All @@ -46,8 +50,8 @@ Function createGroup($resname : Text) : cs:C1710.xliffGroup
$node:=This:C1470.create(This:C1470.bodyNode; "group"; $attributes)

// Update me
$attributes.XPATH:=This:C1470.bodyPath+"/group[@resname=\""+$resname+"\"]"
$group:=This:C1470.group.new($node; $attributes)
$attributes.xpath:=This:C1470.XPATH_BODY+"/group[@resname=\""+$resname+"\"]"
$group:=cs:C1710.xliffGroup.new($node; $attributes)

This:C1470.groups.push($group)
This:C1470.groups:=This:C1470.groups.orderBy("resname")
Expand All @@ -61,13 +65,13 @@ Function deleteUnit($unit : cs:C1710.xliffUnit) : cs:C1710.xliffGroup
var $c : Collection
var $group : cs:C1710.xliffGroup

//delete unit
// Delete unit
This:C1470.remove($unit.node)

//update group
$c:=Split string:C1554($unit.XPATH; "/")
// Update group
$c:=Split string:C1554($unit.xpath; "/")
$c.pop()
$group:=This:C1470.groups.query("XPATH = :1"; $c.join("/")).pop()
$group:=This:C1470.groups.query("xpath = :1"; $c.join("/")).pop()

$indx:=$group.transunits.indices("id = :1"; $unit.id)[0]
$group.transunits.remove($indx)
Expand All @@ -88,13 +92,13 @@ Function createUnit($group : cs:C1710.xliffGroup; $resname : Text; $id : Text) :
$node:=This:C1470.create($group.node; "trans-unit"; $attributes)

$unit:=cs:C1710.xliffUnit.new($node; $attributes)
$unit.XPATH:=$group.XPATH+"/trans-unit[@id=\""+$unit.id+"\"]"
$unit.xpath:=$group.xpath+"/trans-unit[@id=\""+$unit.id+"\"]"

$unit.source.node:=This:C1470.create($node; "source")
$unit.source.XPATH:=$unit.XPATH+"/source"
$unit.source.xpath:=$unit.xpath+"/source"

$unit.target.node:=This:C1470.create($node; "target")
$unit.target.XPATH:=$unit.XPATH+"/target"
$unit.target.xpath:=$unit.xpath+"/target"

$group.transunits.push($unit)

Expand All @@ -109,7 +113,7 @@ Function get fileNode() : Text

If (This:C1470.root#Null:C1517)

return This:C1470.findByXPath(This:C1470.filePath)
return This:C1470.findByXPath(This:C1470.XPATH_FILE)

End if

Expand All @@ -118,7 +122,7 @@ Function get bodyNode() : Text

If (This:C1470.root#Null:C1517)

return This:C1470.findByXPath(This:C1470.bodyPath)
return This:C1470.findByXPath(This:C1470.XPATH_BODY)

End if

Expand Down Expand Up @@ -226,18 +230,16 @@ Function todoNodes() : Collection
// === === === === === === === === === === === === === === === === === === ===
Function setState($node : Text; $state : Text)

var $currentState : Text

$node:=This:C1470.getTarget($node)

If (This:C1470.isReference($node))

If (Length:C16($state)>0)

$currentState:=String:C10(This:C1470.getAttributes($node).state)
var $currentState : Text:=String:C10(This:C1470.getAttributes($node).state)

If (Length:C16($currentState)>0)\
&& (($currentState=This:C1470.new) | ($state=This:C1470.needsTranslation) | ($state=This:C1470.needsReviewTranslation))
&& (($currentState=This:C1470.NEW) | ($state=This:C1470.NEEDS_TRANSLATION) | ($state=This:C1470.NEEDS_REVIEW))

return // Don't modify

Expand Down Expand Up @@ -325,23 +327,23 @@ Function parse() : cs:C1710.Xliff

For each ($node; This:C1470.groupNodes())

$group:=This:C1470.group.new($node; This:C1470.getAttributes($node))
$group.XPATH:=This:C1470.bodyPath+"/group[@resname=\""+$group.resname+"\"]"
$group:=cs:C1710.xliffGroup.new($node; This:C1470.getAttributes($node))
$group.xpath:=This:C1470.XPATH_BODY+"/group[@resname=\""+$group.resname+"\"]"

// Get the trans-units
For each ($node; This:C1470.groupUnitNodes($group.node))

$unit:=This:C1470.transunit.new($node; This:C1470.getAttributes($node))
$unit.XPATH:=$group.XPATH+"/trans-unit[@id=\""+$unit.id+"\"]"
$unit:=cs:C1710.xliffUnit.new($node; This:C1470.getAttributes($node))
$unit.xpath:=$group.xpath+"/trans-unit[@id=\""+$unit.id+"\"]"

$unit.source.value:=This:C1470.sourceValue($node)
$unit.source.XPATH:=$unit.XPATH+"/source"
$unit.source.xpath:=$unit.xpath+"/source"

If ($unit.attributes.translate#Null:C1517)\
&& ($unit.attributes.translate#"no")

$unit.target.value:=This:C1470.targetValue($node)
$unit.target.XPATH:=$unit.XPATH+"/target"
$unit.target.xpath:=$unit.xpath+"/target"

Else

Expand Down Expand Up @@ -445,7 +447,7 @@ Function findUnitNode($unit : cs:C1710.xliffUnit) : Text
var $nodes : Collection

// Search first with the id
$nodes:=This:C1470.find(This:C1470.root; $unit.XPATH)
$nodes:=This:C1470.find(This:C1470.root; $unit.xpath)

If ($nodes.length>1)

Expand Down Expand Up @@ -515,13 +517,13 @@ Function localizedFile($file : 4D:C1709.File; $masterLanguage : Text; $language
// Manage the target language in upper case (EN, FR, ...) as a suffix of the name
$path:=$file.platformPath

If (Match regex:C1019("(?m-si)(.*)"+Uppercase:C13(Substring:C12($masterLanguage; 1; 2))+"\\"+This:C1470.fileExtension+"$"; $path; 1; $pos; $len))
If (Match regex:C1019("(?m-si)(.*)"+Uppercase:C13(Substring:C12($masterLanguage; 1; 2))+"\\"+This:C1470.FILE_EXTENSION+"$"; $path; 1; $pos; $len))

$path:=Substring:C12($path; 1; $len{1})+Uppercase:C13(Substring:C12($language; 1; 2))+This:C1470.fileExtension
$path:=Substring:C12($path; 1; $len{1})+Uppercase:C13(Substring:C12($language; 1; 2))+This:C1470.FILE_EXTENSION

End if

return File:C1566(Replace string:C233($path; $masterLanguage+This:C1470.folderExtension; $language+This:C1470.folderExtension); fk platform path:K87:2)
return File:C1566(Replace string:C233($path; $masterLanguage+This:C1470.FOLDER_EXTENSION; $language+This:C1470.FOLDER_EXTENSION); fk platform path:K87:2)

// === === === === === === === === === === === === === === === === === === ===
Function synchronize($file : 4D:C1709.File; $targetLanguage : Text)
Expand Down Expand Up @@ -557,7 +559,7 @@ Function synchronize($file : 4D:C1709.File; $targetLanguage : Text)

If (Not:C34(Bool:C1537($unit.noTranslate)))

$xliff.setState($unit.node; This:C1470.new)
$xliff.setState($unit.node; This:C1470.NEW)

End if
End for each
Expand All @@ -583,7 +585,7 @@ Function synchronize($file : 4D:C1709.File; $targetLanguage : Text)
If ($localizedGroup=Null:C1517)

// Create the group
$groupNode:=$xliff.append($xliff.findByXPath($xliff.bodyPath); $group.node)
$groupNode:=$xliff.append($xliff.findByXPath($xliff.XPATH_BODY); $group.node)

// Set all target states as "new"
For each ($node; $xliff.childrens($groupNode))
Expand All @@ -592,7 +594,7 @@ Function synchronize($file : 4D:C1709.File; $targetLanguage : Text)

If (String:C10($attributes.translate)#"no")

$xliff.setState($node; This:C1470.new)
$xliff.setState($node; This:C1470.NEW)

End if
End for each
Expand All @@ -602,7 +604,7 @@ Function synchronize($file : 4D:C1709.File; $targetLanguage : Text)
End if

// Ensure that all units are synchronized
$groupNode:=$xliff.findByXPath($group.XPATH)
$groupNode:=$xliff.findByXPath($group.xpath)

For each ($unit; $group.transunits.orderBy("resname"))

Expand All @@ -624,7 +626,7 @@ Function synchronize($file : 4D:C1709.File; $targetLanguage : Text)
If (String:C10($unit.attributes.translate)#"no")

// Set the target state as "new"
$xliff.setState($node; This:C1470.new)
$xliff.setState($node; This:C1470.NEW)

End if
End if
Expand All @@ -639,34 +641,34 @@ Function updateHeader($name : Text; $version : Text)

var $node : Text

$node:=This:C1470.findByXPath(This:C1470.propPath)
$node:=This:C1470.findByXPath(This:C1470.XPATH_PROP_GROUP)

If (This:C1470.isReference($node))

This:C1470.setAttribute($node; "name"; $name)

Else

$node:=This:C1470.create(This:C1470.root; This:C1470.propPath; New object:C1471("name"; $name))
$node:=This:C1470.create(This:C1470.root; This:C1470.XPATH_PROP_GROUP; New object:C1471("name"; $name))

End if

$node:=This:C1470.findByXPath(This:C1470.propPath+"/prop[@prop-type=\"version\"")
$node:=This:C1470.findByXPath(This:C1470.XPATH_PROP_GROUP+"/prop[@prop-type=\"version\"")

If (Not:C34(This:C1470.isReference($node)))

$node:=This:C1470.create(This:C1470.root; This:C1470.propPath+"/prop"; New object:C1471("prop-type"; "version"))
$node:=This:C1470.create(This:C1470.root; This:C1470.XPATH_PROP_GROUP+"/prop"; New object:C1471("prop-type"; "version"))

End if

This:C1470.setValue($node; $version)

// Delete old properties
This:C1470.remove(This:C1470.findByXPath(This:C1470.propPath+"/prop[@prop-type=\"resname-autofill\""))
This:C1470.remove(This:C1470.findByXPath(This:C1470.propPath+"/prop[@prop-type=\"resname-prefix\""))
This:C1470.remove(This:C1470.findByXPath(This:C1470.propPath+"/prop[@prop-type=\"resname-separator\""))
This:C1470.remove(This:C1470.findByXPath(This:C1470.propPath+"/prop[@prop-type=\"prefix\""))
This:C1470.remove(This:C1470.findByXPath(This:C1470.propPath+"/prop[@prop-type=\"separator\""))
This:C1470.remove(This:C1470.findByXPath(This:C1470.XPATH_PROP_GROUP+"/prop[@prop-type=\"resname-autofill\""))
This:C1470.remove(This:C1470.findByXPath(This:C1470.XPATH_PROP_GROUP+"/prop[@prop-type=\"resname-prefix\""))
This:C1470.remove(This:C1470.findByXPath(This:C1470.XPATH_PROP_GROUP+"/prop[@prop-type=\"resname-separator\""))
This:C1470.remove(This:C1470.findByXPath(This:C1470.XPATH_PROP_GROUP+"/prop[@prop-type=\"prefix\""))
This:C1470.remove(This:C1470.findByXPath(This:C1470.XPATH_PROP_GROUP+"/prop[@prop-type=\"separator\""))

This:C1470.save()

Expand Down Expand Up @@ -701,13 +703,13 @@ Function addCopyright($copyright : Text)
$node:=DOM Append XML child node:C1080(DOM Get XML document ref:C1088(This:C1470.root); XML comment:K45:8; $copyright)

// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// $item could be a XPATH or a unit object
// $item could be a xpath or a unit object
Function _child($item; $element : Text) : Text

If (Value type:C1509($item)=Is object:K8:27)

var $node : Text
$node:=This:C1470.findByXPath($item.XPATH+"/"+$element)
$node:=This:C1470.findByXPath($item.xpath+"/"+$element)

If (This:C1470.success & This:C1470.isReference($node))

Expand All @@ -725,6 +727,4 @@ Function _child($item; $element : Text) : Text
Function _uid() : Text

return Replace string:C233(Delete string:C232(Generate digest:C1147(Generate UUID:C1066; _o_4D REST digest:K66:3); 23; 2); "/"; "_")



Loading

0 comments on commit be14fdc

Please sign in to comment.