Modules/businessdev.ALbuild.Apps/Private/Get-BcXliffUnit.ps1
|
function Get-BcXliffUnit { <# .SYNOPSIS Reads translation units from an XLIFF document (1.2 trans-unit or 2.0 unit). .DESCRIPTION Internal helper. Returns one descriptor per translation unit with its id, source, target, target state and the Developer / Xliff-Generator notes, plus the underlying XML nodes for editing. Element matching uses local-name() so it is namespace-agnostic and works for both XLIFF 1.2 and 2.0. #> [CmdletBinding()] [OutputType([PSCustomObject])] param( [Parameter(Mandatory)] [System.Xml.XmlDocument] $Document ) $textOf = { param($node) if ($null -eq $node) { return $null } return $node.InnerText } $noteFrom = { param($unit, $fromLike) foreach ($note in $unit.SelectNodes(".//*[local-name()='note']")) { $from = $null if ($note.Attributes) { foreach ($a in $note.Attributes) { if ($a.Name -ieq 'from') { $from = $a.Value } } } if ($from -and ($from -like "*$fromLike*")) { return $note.InnerText } } return $null } $units = foreach ($unit in $Document.SelectNodes("//*[local-name()='trans-unit'] | //*[local-name()='unit']")) { $source = $unit.SelectSingleNode(".//*[local-name()='source']") $target = $unit.SelectSingleNode(".//*[local-name()='target']") $id = $null if ($unit.Attributes) { foreach ($a in $unit.Attributes) { if ($a.Name -ieq 'id') { $id = $a.Value } } } $state = $null if ($target -and $target.Attributes) { foreach ($a in $target.Attributes) { if ($a.Name -ieq 'state') { $state = $a.Value } } } [PSCustomObject]@{ Id = $id Source = (& $textOf $source) Target = (& $textOf $target) TargetState = $state DevNote = (& $noteFrom $unit 'Developer') GenNote = (& $noteFrom $unit 'Generator') UnitNode = $unit SourceNode = $source TargetNode = $target } } return @($units) } |