16. April 2015 16:08
"A1045" {$ISOTargetCode = 'pl'}
$sourceEncoding = [System.Text.Encoding]::GetEncoding(852)
function NAVTransToXLF_v2
{
#Declare ordered hashtables
$NAVUTF8sourcehash = [ordered]@{}
$NAVUTF8targethash = [ordered]@{}
function WriteToFile
{
out-file $XLFoutfile -inputobject $args -force -append -Width 500 -Encoding utf8
}
function WriteXLFHeader
{
$CurrLine = "<xliff version='1.2' xmlns='urn:oasis:names:tc:xliff:document:1.2'>"
# out-file C:\Temp\Translations.xlf -inputobject $Currline -force -append -Width 500 -Encoding utf8
writetofile $CurrLine
$OrigFileName = [IO.Path]::GetFileName($NAVtransfile)
$CurrLine = " <file original='$OrigFileName' source-language='$ISOsourceCode' target-language='$ISOTargetCode' datatype='plaintext'>"
writetofile $CurrLine
$CurrLine = " <body>"
writetofile $CurrLine
}
function WriteBodyLines
{
$SourceCaption = $SourceCaption -replace '&','&'
$TargetCaption = $TargetCaption -replace '&','&'
$SourceCaption = $SourceCaption -replace '<','<'
$TargetCaption = $TargetCaption -replace '<','<'
$SourceCaption = $SourceCaption -replace '>','>'
$TargetCaption = $TargetCaption -replace '>','>'
$SourceCaption = $SourceCaption -replace '"','"'
$TargetCaption = $TargetCaption -replace '"','"'
$SourceCaption = $SourceCaption -replace $([char]39),'''
$TargetCaption = $TargetCaption -replace $([char]39),'''
$CurrLine = " <trans-unit id=$([char]34)$SourceCodestringID$([char]34)>"
writetofile $CurrLine
$CurrLine = " <source xml:lang=$([char]34)$ISOsourceCode$([char]34)>$SourceCaption</source>"
writetofile $CurrLine
$CurrLine = " <target xml:lang=$([char]34)$ISOtargetCode$([char]34)>$TargetCaption</target>"
writetofile $CurrLine
$CurrLine = ' </trans-unit>'
writetofile $CurrLine
}
function WriteXLFfooter
{
$CurrLine = ' </body>'
writetofile $CurrLine
$CurrLine = ' </file>'
writetofile $CurrLine
$CurrLine = '</xliff>'
writetofile $CurrLine
}
$NAVtransfile = Read-host "Existing NAV translation file"
$XLFoutfile = Read-host "Name of new XLF translation file"
if ($NAVtransfile -eq $XLFoutfile)
{
Throw "Source and target file are identical. Source: $NAVtransfile Target: $XLFoutfile"
}
if (Test-Path $XLFoutfile) {Remove-Item $XLFoutfile}
$lines = Get-Content $NAVtransfile | Measure-Object -Line
$Nooflines = $lines.Lines
write-host "File has $Nooflines lines"
$sourceEncoding = [Text.Encoding]::GetEncoding(850)
$targetEncoding = [Text.Encoding]::GetEncoding(65001)
$convertedFileName = [IO.Path]::GetDirectoryName($NAVtransfile) + "\"+ [IO.Path]::GetFileNameWithoutExtension($NAVtransfile) +"_UTF8" + [IO.Path]::GetExtension($NAVtransfile)
if (Test-Path $convertedFileName) {Remove-Item $convertedFileName}
$convertedfile = New-Item -path $convertedFileName -type file
$textfile = [IO.File]::ReadAllText($NAVtransfile, $sourceencoding)
[IO.File]::WriteAllText($convertedfile, $textfile, $targetencoding)
Write-host $NAVtransfile 'converted to' $convertedFileName
$SourceLangCode = Read-host "NAV-Code for source language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
$TargetLangCode = Read-host "NAV-Code for target language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
if ($SourceLangCode -eq $TargetLangCode)
{
Throw "Source and target language code are identical. Source: $SourceLangCode Target: $TargetLangCode"
}
Switch ($SourceLangCode)
{
"A1031" {$ISOsourceCode = 'de'}
"A1033" {$ISOsourceCode = 'en'}
"A1034" {$ISOsourceCode = 'es'}
"A1036" {$ISOsourceCode = 'fr'}
"A1040" {$ISOsourceCode = 'it'}
"A1046" {$ISOsourceCode = 'pt'}
"A2055" {$ISOsourceCode = 'gsw'}
"A2057" {$ISOsourceCode = 'en-gb'}
"A3079" {$ISOsourceCode = 'de'}
default {Throw "Please assign an ISO code for $SourceLangCode in the script"}
}
Switch ($TargetLangCode)
{
"A1030" {$ISOTargetCode = 'dk'}
"A1031" {$ISOTargetCode = 'de'}
"A1033" {$ISOTargetCode = 'en'}
"A1034" {$ISOTargetCode = 'es'}
"A1035" {$ISOTargetCode = 'fi'}
"A1036" {$ISOTargetCode = 'fr'}
"A1040" {$ISOTargetCode = 'it'}
"A1043" {$ISOTargetCode = 'nl'}
"A1044" {$ISOTargetCode = 'no'}
"A1045" {$ISOTargetCode = 'pl'}
"A1046" {$ISOTargetCode = 'pt'}
"A1053" {$ISOTargetCode = 'se'}
"A2055" {$ISOTargetCode = 'gsw'}
"A2057" {$ISOTargetCode = 'en-gb'}
"A3079" {$ISOTargetCode = 'de'}
default {Throw "Please assign an ISO code for $TargetLangCode in the script"}
}
Write-host "ISO 639 language codes source: $ISOsourceCode, target: $ISOTargetCode"
$SourceLangCode = "-"+ $SourceLangCode + "-"
$TargetLangCode = "-"+ $TargetLangCode + "-"
# Write-host "Writing XLF-Header…"
WriteXLFHeader
Write-host "Building hash, this may take a while…"
foreach ($line in [IO.File]::ReadLines($convertedFileName))
{
$k += 1
if ($k % 1000 -eq 0) {Write-host "Line $k of $Nooflines"}
$SourceTextLine = $line.ToString()
if ($SourceTextLine.contains($SourceLangCode))
{
$pos1 = $SourceTextLine.IndexOf(":")
$SourceCodestringID = $SourceTextLine.Substring(0,$pos1)
$SourceCaption = $SourceTextLine.Substring($pos1 + 1)
$NAVUTF8sourcehash.add($SourceCodestringID,$SourceCaption)
}
if ($SourceTextLine.contains($TargetLangCode))
{
$pos1 = $SourceTextLine.IndexOf(":")
$TargetCodestringID = $SourceTextLine.Substring(0,$pos1)
$TargetCaption = $SourceTextLine.Substring($pos1 + 1)
$NAVUTF8targethash.add($TargetCodestringID,$TargetCaption)
}
}
foreach ($key in $NAVUTF8sourcehash.keys)
{
$SourceCodestringID = $key
$SourceCaption = $NAVUTF8sourcehash[$key]
$TargetCodestringID = $SourceCodestringID -replace $SourceLangCode,$TargetLangCode
$TargetCaption = $NAVUTF8targethash[$TargetCodestringID]
IF (![string]::IsNullOrEmpty($TargetCaption))
{
IF ($TargetCaption -eq 'System.Object[]') {$TargetCaption = ''}
WriteBodyLines
$i += 1
if ($i % 10 -eq 0) {Write-Host "Writing line $i"}
}
}
WriteXLFfooter
}
NAVTransToXLF_v2
function NAVTransToXLF
{
function WriteToFile
{
out-file $XLFoutfile -inputobject $args -force -append -Width 500 -Encoding utf8
}
function WriteXLFHeader
{
$CurrLine = "<xliff version='1.2' xmlns='urn:oasis:names:tc:xliff:document:1.2'>"
# out-file C:\Temp\Translations.xlf -inputobject $Currline -force -append -Width 500 -Encoding utf8
writetoFile $CurrLine
$OrigFileName = [System.IO.Path]::GetFileName($NAVtransfile)
$CurrLine = " <file original='$OrigFileName' source-language='$ISOsourceCode' target-language='$ISOTargetCode' datatype='plaintext'>"
writetoFile $CurrLine
$CurrLine = " <body>"
writetoFile $CurrLine
}
function WriteBodyLines
{
$SourceCaption = $SourceCaption -replace '&','&'
$TargetCaption = $TargetCaption -replace '&','&'
$SourceCaption = $SourceCaption -replace '<','<'
$TargetCaption = $TargetCaption -replace '<','<'
$SourceCaption = $SourceCaption -replace '>','>'
$TargetCaption = $TargetCaption -replace '>','>'
$SourceCaption = $SourceCaption -replace '"','"'
$TargetCaption = $TargetCaption -replace '"','"'
$SourceCaption = $SourceCaption -replace $([char]39),'''
$TargetCaption = $TargetCaption -replace $([char]39),'''
$CurrLine = " <trans-unit id=$([char]34)$SourceCodestringID$([char]34)>"
writetoFile $CurrLine
$CurrLine = " <source xml:lang=$([char]34)$ISOsourceCode$([char]34)>$SourceCaption</source>"
writetoFile $CurrLine
$CurrLine = " <target xml:lang=$([char]34)$ISOtargetCode$([char]34)>$TargetCaption</target>"
writetoFile $CurrLine
$CurrLine = ' </trans-unit>'
writetoFile $CurrLine
}
function WriteXLFfooter
{
$CurrLine = ' </body>'
writetoFile $CurrLine
$CurrLine = ' </file>'
writetoFile $CurrLine
$CurrLine = '</xliff>'
writetoFile $CurrLine
}
$NAVtransfile = Read-host "Existing NAV translation file"
$XLFoutfile = Read-host "Name of new XLF translation file"
if ($NAVtransfile -eq $XLFoutfile)
{
Throw "Source and target file are identical. Source: $NAVtransfile Target: $XLFoutfile"
}
if (Test-Path $XLFoutfile) {Remove-Item $XLFoutfile}
$lines = Get-Content $NAVtransfile | Measure-Object –Line
$Nooflines = $lines.Lines
write-host "File has $Nooflines lines"
$sourceEncoding = [System.Text.Encoding]::GetEncoding(850)
$targetEncoding = [System.Text.Encoding]::GetEncoding(65001)
$convertedFileName = [System.IO.Path]::GetDirectoryName($NAVtransfile) + "\"+ [System.IO.Path]::GetFileNameWithoutExtension($NAVtransfile) +"_UTF8" + [System.IO.Path]::GetExtension($NAVtransfile)
if (Test-Path $convertedFileName) {Remove-Item $convertedFileName}
$convertedfile = New-Item -path $convertedFileName -type file
$textfile = [System.IO.File]::ReadAllText($NAVtransfile, $sourceencoding)
[System.IO.File]::WriteAllText($convertedfile, $textfile, $targetencoding)
Write-host $NAVtransfile 'converted to' $convertedFileName
$SourceLangCode = Read-host "NAV-Code for source language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
$TargetLangCode = Read-host "NAV-Code for target language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
if ($SourceLangCode -eq $TargetLangCode)
{
Throw "Source and target language code are identical. Source: $SourceLangCode Target: $TargetLangCode"
}
Switch ($SourceLangCode)
{
"A1031" {$ISOsourceCode = 'de'}
"A1033" {$ISOsourceCode = 'en'}
"A1034" {$ISOsourceCode = 'es'}
"A1036" {$ISOsourceCode = 'fr'}
"A1040" {$ISOsourceCode = 'it'}
"A1046" {$ISOsourceCode = 'pt'}
"A2055" {$ISOsourceCode = 'gsw'}
"A2057" {$ISOsourceCode = 'en-gb'}
"A3079" {$ISOsourceCode = 'de'}
default {Throw "Please assign an ISO code for $SourceLangCode in the script"}
}
Switch ($TargetLangCode)
{
"A1030" {$ISOTargetCode = 'dk'}
"A1031" {$ISOTargetCode = 'de'}
"A1033" {$ISOTargetCode = 'en'}
"A1034" {$ISOTargetCode = 'es'}
"A1035" {$ISOTargetCode = 'fi'}
"A1036" {$ISOTargetCode = 'fr'}
"A1040" {$ISOTargetCode = 'it'}
"A1043" {$ISOTargetCode = 'nl'}
"A1044" {$ISOTargetCode = 'no'}
"A1045" {$ISOTargetCode = 'pl'}
"A1046" {$ISOTargetCode = 'pt'}
"A1053" {$ISOTargetCode = 'se'}
"A2055" {$ISOTargetCode = 'gsw'}
"A2057" {$ISOTargetCode = 'en-gb'}
"A3079" {$ISOTargetCode = 'de'}
default {Throw "Please assign an ISO code for $TargetLangCode in the script"}
}
Write-host "ISO 639 language codes source: $ISOsourceCode, target: $ISOTargetCode"
$SourceLangCode = "-"+ $SourceLangCode + "-"
$TargetLangCode = "-"+ $TargetLangCode + "-"
# Write-host "Writing XLF-Header…"
WriteXLFHeader
# Write-host "UTF8 file: $convertedFileName"
foreach ($line in [System.IO.File]::ReadLines($convertedFileName))
{
$SourceTextLine = $line.ToString()
if ($SourceTextLine.contains($SourceLangCode))
{
$TargetLine = ''
# Write-host "Source line $SourceTextLine"
$pos1 = $SourceTextLine.IndexOf(":")
$SourceCodestringID = $SourceTextLine.Substring(0,$pos1)
$SourceCaption = $SourceTextLine.Substring($pos1 + 1)
# Write-host "Source Caption $SourceCaption"
$TargetCodestringID = $SourceCodestringID -replace $SourceLangCode,$TargetLangCode
# Write-host "Token Target $TargetCodestringID"
$TargetTextLine = ''
$TargetLine = select-string -path $convertedFileName -pattern $TargetCodestringID -SimpleMatch
IF (![string]::IsNullOrEmpty($TargetLine))
{
$TargetTextLine = $TargetLine.ToString()
$posFileNameEnd = $TargetTextLine.IndexOf(":")
$TargetTextLine = $TargetTextLine.Substring($posFileNameEnd + 1)
$poslineno = $TargetTextLine.IndexOf(":")
$TargetTextLine = $TargetTextLine.Substring($poslineno + 1)
$posNAVcodeEnd = $TargetTextLine.IndexOf(":")
$TargetTextLine = $TargetTextLine.Substring($posNAVcodeEnd + 1)
# Write-host "Target line $TargetTextLine"
IF (![string]::IsNullOrEmpty($TargetTextLine))
{
$pos2 = $TargetTextLine.IndexOf(":")
$TargetCaption = $TargetTextLine.Substring($pos2 + 1)
IF ($TargetCaption -eq 'System.Object[]') {$TargetCaption = ''}
WriteBodyLines
$i = $i + 1
if ($i%10 -eq 0) {Write-Host "Writing line $i"}
}
}
}
}
WriteXLFfooter
}
16. April 2015 17:37
function NAVTransOPPToXLF
{
function IsAddonCaption($Codestring)
{
return($Codestring.contains('51578') -or $Codestring.contains('51579') -or $Codestring.contains('51580') -or $Codestring.StartsWith('M1055') -or $Codestring.StartsWith('M55'))
}
function WriteToFile
{
out-file $XLFoutfile -inputobject $args -force -append -Width 500 -Encoding utf8
}
function WriteXLFHeader
{
$CurrLine = "<xliff version='1.2' xmlns='urn:oasis:names:tc:xliff:document:1.2'>"
# out-file C:\Temp\Translations.xlf -inputobject $Currline -force -append -Width 500 -Encoding utf8
writetoFile $CurrLine
$OrigFileName = [System.IO.Path]::GetFileName($NAVtransfile)
$CurrLine = " <file original='$OrigFileName' source-language='$ISOsourceCode' target-language='$ISOTargetCode' datatype='plaintext'>"
writetoFile $CurrLine
$CurrLine = " <body>"
writetoFile $CurrLine
}
function WriteBodyLines
{
$SourceCaption = $SourceCaption -replace '&','&'
$TargetCaption = $TargetCaption -replace '&','&'
$SourceCaption = $SourceCaption -replace '<','<'
$TargetCaption = $TargetCaption -replace '<','<'
$SourceCaption = $SourceCaption -replace '>','>'
$TargetCaption = $TargetCaption -replace '>','>'
$SourceCaption = $SourceCaption -replace '"','"'
$TargetCaption = $TargetCaption -replace '"','"'
$SourceCaption = $SourceCaption -replace $([char]39),'''
$TargetCaption = $TargetCaption -replace $([char]39),'''
$CurrLine = " <trans-unit id=$([char]34)$SourceCodestringID$([char]34)>"
writetoFile $CurrLine
$CurrLine = " <source xml:lang=$([char]34)$ISOsourceCode$([char]34)>$SourceCaption</source>"
writetoFile $CurrLine
$CurrLine = " <target xml:lang=$([char]34)$ISOtargetCode$([char]34)>$TargetCaption</target>"
writetoFile $CurrLine
$CurrLine = ' </trans-unit>'
writetoFile $CurrLine
}
function WriteXLFfooter
{
$CurrLine = ' </body>'
writetoFile $CurrLine
$CurrLine = ' </file>'
writetoFile $CurrLine
$CurrLine = '</xliff>'
writetoFile $CurrLine
}
$NAVtransfile = Read-host "Existing NAV translation file"
$XLFoutfile = Read-host "Name of new XLF translation file"
if ($NAVtransfile -eq $XLFoutfile)
{
Throw "Source and target file are identical. Source: $NAVtransfile Target: $XLFoutfile"
}
if (Test-Path $XLFoutfile) {Remove-Item $XLFoutfile}
$lines = Get-Content $NAVtransfile | Measure-Object –Line
$Nooflines = $lines.Lines
write-host "File has $Nooflines lines"
$sourceEncoding = [System.Text.Encoding]::GetEncoding(850)
$targetEncoding = [System.Text.Encoding]::GetEncoding(65001)
$convertedFileName = [System.IO.Path]::GetDirectoryName($NAVtransfile) + "\"+ [System.IO.Path]::GetFileNameWithoutExtension($NAVtransfile) +"_UTF8" + [System.IO.Path]::GetExtension($NAVtransfile)
if (Test-Path $convertedFileName) {Remove-Item $convertedFileName}
$convertedfile = New-Item -path $convertedFileName -type file
$textfile = [System.IO.File]::ReadAllText($NAVtransfile, $sourceencoding)
[System.IO.File]::WriteAllText($convertedfile, $textfile, $targetencoding)
Write-host $NAVtransfile 'converted to' $convertedFileName
$SourceLangCode = Read-host "NAV-Code for source language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
$TargetLangCode = Read-host "NAV-Code for target language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
if ($SourceLangCode -eq $TargetLangCode)
{
Throw "Source and target language code are identical. Source: $SourceLangCode Target: $TargetLangCode"
}
Switch ($SourceLangCode)
{
"A1031" {$ISOsourceCode = 'de'}
"A1033" {$ISOsourceCode = 'en'}
"A1034" {$ISOsourceCode = 'es'}
"A1036" {$ISOsourceCode = 'fr'}
"A2055" {$ISOsourceCode = 'gsw'}
"A2057" {$ISOsourceCode = 'en-gb'}
"A3079" {$ISOsourceCode = 'de'}
default {Throw "Please assign an ISO code for $SourceLangCode in the script"}
}
Switch ($TargetLangCode)
{
"A1031" {$ISOTargetCode = 'de'}
"A1033" {$ISOTargetCode = 'en'}
"A1034" {$ISOTargetCode = 'es'}
"A1036" {$ISOTargetCode = 'fr'}
"A2055" {$ISOTargetCode = 'gsw'}
"A2057" {$ISOTargetCode = 'en-gb'}
"A3079" {$ISOTargetCode = 'de'}
default {Throw "Please assign an ISO code for $TargetLangCode in the script"}
}
Write-host "ISO 639 language codes source: $ISOsourceCode, target: $ISOTargetCode"
$SourceLangCode = "-"+ $SourceLangCode + "-"
$TargetLangCode = "-"+ $TargetLangCode + "-"
# Write-host "Writing XLF-Header…"
WriteXLFHeader
# Write-host "UTF8 file: $convertedFileName"
foreach ($line in [System.IO.File]::ReadLines($convertedFileName))
{
$SourceTextLine = $line.ToString()
if ($SourceTextLine.contains($SourceLangCode))
{
$TargetLine = ''
# Write-host "Source line $SourceTextLine"
$pos1 = $SourceTextLine.IndexOf(":")
$SourceCodestringID = $SourceTextLine.Substring(0,$pos1)
IF (IsAddonCaption($SourceCodestringID))
{
$SourceCaption = $SourceTextLine.Substring($pos1 + 1)
# Write-host "Source Caption $SourceCaption"
$TargetCodestringID = $SourceCodestringID -replace $SourceLangCode,$TargetLangCode
# Write-host "Token Target $TargetCodestringID"
$TargetTextLine = ''
$TargetLine = select-string -path $convertedFileName -pattern $TargetCodestringID -SimpleMatch
IF (![string]::IsNullOrEmpty($TargetLine))
{
$TargetTextLine = $TargetLine.ToString()
$posFileNameEnd = $TargetTextLine.IndexOf(":")
$TargetTextLine = $TargetTextLine.Substring($posFileNameEnd + 1)
$poslineno = $TargetTextLine.IndexOf(":")
$TargetTextLine = $TargetTextLine.Substring($poslineno + 1)
$posNAVcodeEnd = $TargetTextLine.IndexOf(":")
$TargetTextLine = $TargetTextLine.Substring($posNAVcodeEnd + 1)
# Write-host "Target line $TargetTextLine"
IF (![string]::IsNullOrEmpty($TargetTextLine))
{
$pos2 = $TargetTextLine.IndexOf(":")
$TargetCaption = $TargetTextLine.Substring($pos2 + 1)
IF ($TargetCaption -eq 'System.Object[]') {$TargetCaption = ''}
WriteBodyLines
$i = $i + 1
if ($i%10 -eq 0) {Write-Host "Writing line $i"}
}
}
}
}
}
WriteXLFfooter
}
19. Mai 2015 17:24
function NAVTransToXLF_v2_AllowEmptyTargetCaptions
{
#Declare ordered hashtables
$NAVUTF8sourcehash = [ordered]@{}
$NAVUTF8targethash = [ordered]@{}
function WriteToFile
{
out-file $XLFoutfile -inputobject $args -force -append -Width 500 -Encoding utf8
}
function WriteXLFHeader
{
$CurrLine = "<xliff version='1.2' xmlns='urn:oasis:names:tc:xliff:document:1.2'>"
# out-file C:\Temp\Translations.xlf -inputobject $Currline -force -append -Width 500 -Encoding utf8
writetofile $CurrLine
$OrigFileName = [IO.Path]::GetFileName($NAVtransfile)
$CurrLine = " <file original='$OrigFileName' source-language='$ISOsourceCode' target-language='$ISOTargetCode' datatype='plaintext'>"
writetofile $CurrLine
$CurrLine = " <body>"
writetofile $CurrLine
}
function WriteBodyLines
{
$SourceCaption = $SourceCaption -replace '&','&'
$TargetCaption = $TargetCaption -replace '&','&'
$SourceCaption = $SourceCaption -replace '<','<'
$TargetCaption = $TargetCaption -replace '<','<'
$SourceCaption = $SourceCaption -replace '>','>'
$TargetCaption = $TargetCaption -replace '>','>'
$SourceCaption = $SourceCaption -replace '"','"'
$TargetCaption = $TargetCaption -replace '"','"'
$SourceCaption = $SourceCaption -replace $([char]39),'''
$TargetCaption = $TargetCaption -replace $([char]39),'''
$CurrLine = " <trans-unit id=$([char]34)$SourceCodestringID$([char]34)>"
writetofile $CurrLine
$CurrLine = " <source xml:lang=$([char]34)$ISOsourceCode$([char]34)>$SourceCaption</source>"
writetofile $CurrLine
$CurrLine = " <target xml:lang=$([char]34)$ISOtargetCode$([char]34)>$TargetCaption</target>"
writetofile $CurrLine
$CurrLine = ' </trans-unit>'
writetofile $CurrLine
}
function WriteXLFfooter
{
$CurrLine = ' </body>'
writetofile $CurrLine
$CurrLine = ' </file>'
writetofile $CurrLine
$CurrLine = '</xliff>'
writetofile $CurrLine
}
$NAVtransfile = Read-host "Existing NAV translation file"
$XLFoutfile = Read-host "Name of new XLF translation file"
[ValidateSet('yes','no')]$AllowEmptyTargetCaptions = Read-Host "Allow empty target captions in XLIFF if no caption translation is available (yes,no) ?"
if ($NAVtransfile -eq $XLFoutfile)
{
Throw "Source and target file are identical. Source: $NAVtransfile Target: $XLFoutfile"
}
if (Test-Path $XLFoutfile) {Remove-Item $XLFoutfile}
$lines = Get-Content $NAVtransfile | Measure-Object -Line
$Nooflines = $lines.Lines
write-host "File has $Nooflines lines"
$sourceEncoding = [Text.Encoding]::GetEncoding(850)
$targetEncoding = [Text.Encoding]::GetEncoding(65001)
$convertedFileName = [IO.Path]::GetDirectoryName($NAVtransfile) + "\"+ [IO.Path]::GetFileNameWithoutExtension($NAVtransfile) +"_UTF8" + [IO.Path]::GetExtension($NAVtransfile)
if (Test-Path $convertedFileName) {Remove-Item $convertedFileName}
$convertedfile = New-Item -path $convertedFileName -type file
$textfile = [IO.File]::ReadAllText($NAVtransfile, $sourceencoding)
[IO.File]::WriteAllText($convertedfile, $textfile, $targetencoding)
Write-host $NAVtransfile 'converted to' $convertedFileName
$SourceLangCode = Read-host "NAV-Code for source language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
$TargetLangCode = Read-host "NAV-Code for target language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
if ($SourceLangCode -eq $TargetLangCode)
{
Throw "Source and target language code are identical. Source: $SourceLangCode Target: $TargetLangCode"
}
Switch ($SourceLangCode)
{
"A1031" {$ISOsourceCode = 'de'}
"A1033" {$ISOsourceCode = 'en'}
"A1034" {$ISOsourceCode = 'es'}
"A1036" {$ISOsourceCode = 'fr'}
"A1040" {$ISOsourceCode = 'it'}
"A1046" {$ISOsourceCode = 'pt'}
"A2055" {$ISOsourceCode = 'gsw'}
"A2057" {$ISOsourceCode = 'en-gb'}
"A3079" {$ISOsourceCode = 'de'}
default {Throw "Please assign an ISO code for $SourceLangCode in the script"}
}
Switch ($TargetLangCode)
{
"A1030" {$ISOTargetCode = 'dk'}
"A1031" {$ISOTargetCode = 'de'}
"A1033" {$ISOTargetCode = 'en'}
"A1034" {$ISOTargetCode = 'es'}
"A1035" {$ISOTargetCode = 'fi'}
"A1036" {$ISOTargetCode = 'fr'}
"A1040" {$ISOTargetCode = 'it'}
"A1043" {$ISOTargetCode = 'nl'}
"A1044" {$ISOTargetCode = 'no'}
"A1045" {$ISOTargetCode = 'pl'}
"A1046" {$ISOTargetCode = 'pt'}
"A1053" {$ISOTargetCode = 'se'}
"A2055" {$ISOTargetCode = 'gsw'}
"A2057" {$ISOTargetCode = 'en-gb'}
"A3079" {$ISOTargetCode = 'de'}
default {Throw "Please assign an ISO code for $TargetLangCode in the script"}
}
Write-host "ISO 639 language codes source: $ISOsourceCode, target: $ISOTargetCode"
$SourceLangCode = "-"+ $SourceLangCode + "-"
$TargetLangCode = "-"+ $TargetLangCode + "-"
# Write-host "Writing XLF-Header…"
WriteXLFHeader
Write-host "Building hash, this may take a while…"
foreach ($line in [IO.File]::ReadLines($convertedFileName))
{
$k += 1
if ($k % 1000 -eq 0) {Write-host "Line $k of $Nooflines"}
$SourceTextLine = $line.ToString()
if ($SourceTextLine.contains($SourceLangCode))
{
$pos1 = $SourceTextLine.IndexOf(":")
$SourceCodestringID = $SourceTextLine.Substring(0,$pos1)
$SourceCaption = $SourceTextLine.Substring($pos1 + 1)
$NAVUTF8sourcehash.add($SourceCodestringID,$SourceCaption)
}
if ($SourceTextLine.contains($TargetLangCode))
{
$pos1 = $SourceTextLine.IndexOf(":")
$TargetCodestringID = $SourceTextLine.Substring(0,$pos1)
$TargetCaption = $SourceTextLine.Substring($pos1 + 1)
$NAVUTF8targethash.add($TargetCodestringID,$TargetCaption)
}
}
foreach ($key in $NAVUTF8sourcehash.keys)
{
$SourceCodestringID = $key
$SourceCaption = $NAVUTF8sourcehash[$key]
$TargetCodestringID = $SourceCodestringID -replace $SourceLangCode,$TargetLangCode
$TargetCaption = $NAVUTF8targethash[$TargetCodestringID]
IF (![string]::IsNullOrEmpty($TargetCaption) -or ($AllowEmptyTargetCaptions -eq 'yes'))
{
IF ($TargetCaption -eq 'System.Object[]') {$TargetCaption = ''}
WriteBodyLines
$i += 1
if ($i % 100 -eq 0) {Write-Host "Writing line $i"}
}
}
WriteXLFfooter
Write-host $NAVtransfile 'processed to' $XLFoutfile
}
NAVTransToXLF_v2_AllowEmptyTargetCaptions
function NAVTransOPPToXLF_OptionalEmptyTargetCaptions
{
function IsAddonCaption($Codestring)
{
return($Codestring.contains('51578') -or $Codestring.contains('51579') -or $Codestring.contains('51580') -or $Codestring.StartsWith('M1055') -or $Codestring.StartsWith('M55'))
}
function WriteToFile
{
out-file $XLFoutfile -inputobject $args -force -append -Width 500 -Encoding utf8
}
function WriteXLFHeader
{
$CurrLine = "<xliff version='1.2' xmlns='urn:oasis:names:tc:xliff:document:1.2'>"
# out-file C:\Temp\Translations.xlf -inputobject $Currline -force -append -Width 500 -Encoding utf8
writetoFile $CurrLine
$OrigFileName = [System.IO.Path]::GetFileName($NAVtransfile)
$CurrLine = " <file original='$OrigFileName' source-language='$ISOsourceCode' target-language='$ISOTargetCode' datatype='plaintext'>"
writetoFile $CurrLine
$CurrLine = " <body>"
writetoFile $CurrLine
}
function WriteBodyLines
{
$SourceCaption = $SourceCaption -replace '&','&'
$TargetCaption = $TargetCaption -replace '&','&'
$SourceCaption = $SourceCaption -replace '<','<'
$TargetCaption = $TargetCaption -replace '<','<'
$SourceCaption = $SourceCaption -replace '>','>'
$TargetCaption = $TargetCaption -replace '>','>'
$SourceCaption = $SourceCaption -replace '"','"'
$TargetCaption = $TargetCaption -replace '"','"'
$SourceCaption = $SourceCaption -replace $([char]39),'''
$TargetCaption = $TargetCaption -replace $([char]39),'''
$CurrLine = " <trans-unit id=$([char]34)$SourceCodestringID$([char]34)>"
writetoFile $CurrLine
$CurrLine = " <source xml:lang=$([char]34)$ISOsourceCode$([char]34)>$SourceCaption</source>"
writetoFile $CurrLine
$CurrLine = " <target xml:lang=$([char]34)$ISOtargetCode$([char]34)>$TargetCaption</target>"
writetoFile $CurrLine
$CurrLine = ' </trans-unit>'
writetoFile $CurrLine
}
Function WriteXLFfooter
{
$CurrLine = ' </body>'
writetoFile $CurrLine
$CurrLine = ' </file>'
writetoFile $CurrLine
$CurrLine = '</xliff>'
writetoFile $CurrLine
}
$NAVtransfile = Read-host "Existing NAV translation file"
$XLFoutfile = Read-host "Name of new XLF translation file"
[ValidateSet('yes','no')]$AllowEmptyTargetCaptions = Read-Host "Allow empty target captions in XLIFF if no caption translation is available (yes,no) ?"
if ($NAVtransfile -eq $XLFoutfile)
{
Throw "Source and target file are identical. Source: $NAVtransfile Target: $XLFoutfile"
}
if (Test-Path $XLFoutfile) {Remove-Item $XLFoutfile}
$lines = Get-Content $NAVtransfile | Measure-Object –Line
$Nooflines = $lines.Lines
write-host "File has $Nooflines lines"
$sourceEncoding = [System.Text.Encoding]::GetEncoding(850)
$targetEncoding = [System.Text.Encoding]::GetEncoding(65001)
$convertedFileName = [System.IO.Path]::GetDirectoryName($NAVtransfile) + "\"+ [System.IO.Path]::GetFileNameWithoutExtension($NAVtransfile) +"_UTF8" + [System.IO.Path]::GetExtension($NAVtransfile)
if (Test-Path $convertedFileName) {Remove-Item $convertedFileName}
$convertedfile = New-Item -path $convertedFileName -type file
$textfile = [System.IO.File]::ReadAllText($NAVtransfile, $sourceencoding)
[System.IO.File]::WriteAllText($convertedfile, $textfile, $targetencoding)
Write-host $NAVtransfile 'converted to' $convertedFileName
$SourceLangCode = Read-host "NAV-Code for source language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
$TargetLangCode = Read-host "NAV-Code for target language (e.g. A1031 for DEU, A1033 for ENU, A1036 for FRA)"
if ($SourceLangCode -eq $TargetLangCode)
{
Throw "Source and target language code are identical. Source: $SourceLangCode Target: $TargetLangCode"
}
Switch ($SourceLangCode)
{
"A1031" {$ISOsourceCode = 'de'}
"A1033" {$ISOsourceCode = 'en'}
"A1034" {$ISOsourceCode = 'es'}
"A1036" {$ISOsourceCode = 'fr'}
"A1040" {$ISOsourceCode = 'it'}
"A1046" {$ISOsourceCode = 'pt'}
"A2055" {$ISOsourceCode = 'gsw'}
"A2057" {$ISOsourceCode = 'en-gb'}
"A3079" {$ISOsourceCode = 'de'}
default {Throw "Please assign an ISO code for $SourceLangCode in the script"}
}
Switch ($TargetLangCode)
{
"A1030" {$ISOTargetCode = 'dk'}
"A1031" {$ISOTargetCode = 'de'}
"A1033" {$ISOTargetCode = 'en'}
"A1034" {$ISOTargetCode = 'es'}
"A1035" {$ISOTargetCode = 'fi'}
"A1036" {$ISOTargetCode = 'fr'}
"A1040" {$ISOTargetCode = 'it'}
"A1043" {$ISOTargetCode = 'nl'}
"A1044" {$ISOTargetCode = 'no'}
"A1045" {$ISOTargetCode = 'pl'}
"A1046" {$ISOTargetCode = 'pt'}
"A1053" {$ISOTargetCode = 'se'}
"A2055" {$ISOTargetCode = 'gsw'}
"A2057" {$ISOTargetCode = 'en-gb'}
"A3079" {$ISOTargetCode = 'de'}
default {Throw "Please assign an ISO code for $TargetLangCode in the script"}
}
Write-host "ISO 639 language codes source: $ISOsourceCode, target: $ISOTargetCode"
$SourceLangCode = "-"+ $SourceLangCode + "-"
$TargetLangCode = "-"+ $TargetLangCode + "-"
# Write-host "Writing XLF-Header…"
WriteXLFHeader
# Write-host "UTF8 file: $convertedFileName"
foreach ($line in [System.IO.File]::ReadLines($convertedFileName))
{
$SourceTextLine = $line.ToString()
if ($SourceTextLine.contains($SourceLangCode))
{
$TargetLine = ''
# Write-host "Source line $SourceTextLine"
$pos1 = $SourceTextLine.IndexOf(":")
$SourceCodestringID = $SourceTextLine.Substring(0,$pos1)
IF (IsAddonCaption($SourceCodestringID))
{
$SourceCaption = $SourceTextLine.Substring($pos1 + 1)
# Write-host "Source Caption $SourceCaption"
$TargetCodestringID = $SourceCodestringID -replace $SourceLangCode,$TargetLangCode
# Write-host "Token Target $TargetCodestringID"
$TargetTextLine = ''
$TargetLine = select-string -path $convertedFileName -pattern $TargetCodestringID -SimpleMatch
if (([string]::IsNullOrEmpty($TargetLine)) -and ($AllowEmptyTargetCaptions -eq 'yes'))
{
$TargetLine = '::::'
}
if (![string]::IsNullOrEmpty($TargetLine))
{
$TargetTextLine = $TargetLine.ToString()
$posFileNameEnd = $TargetTextLine.IndexOf(":")
$TargetTextLine = $TargetTextLine.Substring($posFileNameEnd + 1)
$poslineno = $TargetTextLine.IndexOf(":")
$TargetTextLine = $TargetTextLine.Substring($poslineno + 1)
$posNAVcodeEnd = $TargetTextLine.IndexOf(":")
$TargetTextLine = $TargetTextLine.Substring($posNAVcodeEnd + 1)
# Write-host "Target line $TargetTextLine"
if (![string]::IsNullOrEmpty($TargetTextLine))
{
$pos2 = $TargetTextLine.IndexOf(":")
$TargetCaption = $TargetTextLine.Substring($pos2 + 1)
If ($TargetCaption -eq 'System.Object[]') {$TargetCaption = ''}
WriteBodyLines
$i = $i + 1
if ($i%10 -eq 0) {Write-Host "Writing line $i"}
}
}
}
}
}
WriteXLFfooter
}
27. Januar 2021 22:09
27. April 2023 14:38