Modules/businessdev.ALbuild.Apps/Public/New-BcTranslationFile.ps1
|
function New-BcTranslationFile { <# .SYNOPSIS Creates a new AL XLIFF target-language file from a generated base file. .DESCRIPTION Clones the generated base XLIFF (.g.xlf), sets the target language, and marks every unit needs-translation, producing a ready-to-translate target file. .PARAMETER BaseFile The generated base XLIFF file. .PARAMETER TargetLanguage The target language code (e.g. de-DE). .PARAMETER OutputPath Output path. Default: the base file with the language inserted (App.de-DE.xlf). .EXAMPLE New-BcTranslationFile -BaseFile .\App.g.xlf -TargetLanguage de-DE .OUTPUTS System.String - the created file path. #> [CmdletBinding(SupportsShouldProcess)] [OutputType([string])] param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $BaseFile, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $TargetLanguage, [string] $OutputPath ) if (-not (Test-Path -LiteralPath $BaseFile)) { throw "Base XLIFF file not found: '$BaseFile'." } if (-not $OutputPath) { $dir = Split-Path -Path $BaseFile -Parent $base = [System.IO.Path]::GetFileName($BaseFile) -replace '\.g\.xlf$', '' -replace '\.xlf$', '' $OutputPath = Join-Path $dir "$base.$TargetLanguage.xlf" } [xml] $doc = Get-Content -LiteralPath $BaseFile -Raw -Encoding UTF8 $fileNode = $doc.SelectSingleNode("//*[local-name()='file']") if ($fileNode) { $attr = $fileNode.Attributes | Where-Object { $_.Name -ieq 'target-language' } | Select-Object -First 1 if (-not $attr) { $attr = $fileNode.SetAttributeNode($doc.CreateAttribute('target-language')) } $attr.Value = $TargetLanguage } foreach ($unit in (Get-BcXliffUnit -Document $doc)) { if (-not $unit.SourceNode) { continue } $ns = $unit.SourceNode.NamespaceURI $targetNode = $unit.TargetNode if (-not $targetNode) { $targetNode = if ($ns) { $doc.CreateElement('target', $ns) } else { $doc.CreateElement('target') } $null = $unit.SourceNode.ParentNode.InsertAfter($targetNode, $unit.SourceNode) } $targetNode.InnerText = '' $stateAttr = $targetNode.Attributes | Where-Object { $_.Name -ieq 'state' } | Select-Object -First 1 if (-not $stateAttr) { $stateAttr = $targetNode.SetAttributeNode($doc.CreateAttribute('state')) } $stateAttr.Value = 'needs-translation' } if ($PSCmdlet.ShouldProcess($OutputPath, "Create translation file for $TargetLanguage")) { $doc.Save($OutputPath) Write-ALbuildLog -Level Success "Created translation file '$OutputPath'." } return $OutputPath } |