5. Februar 2016 11:24
MeinText #1#######
#1####### MeinText
Function MTfromNAVtranslateFile
{
Function WriteToFile
{
out-file $TranslatedfileName -inputobject $args -force -append -Width 1024 -Encoding utf8
}
$NAVtransfile = Read-host "Existing NAV translation file with ENU -A1033- Captions"
$WorkingFolder = Split-Path -Parent $NAVtransfile
$lines = Get-Content $NAVtransfile | Measure-Object –Line
$Nooflines = $lines.Lines
write-host "File has $Nooflines lines"
$ENUlines = Get-Content $NAVtransfile | select-string -pattern "-A1033-" -Allmatches -CaseSensitive | Measure-Object –Line
$NoofENUlines = $ENUlines.Lines
write-host "File has $NoofENUlines ENU lines (Code A1033)"
$SourceLangCode = '-A1033-' # en = ENU
$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}
$convertedBaseName = Split-Path -Leaf $convertedFileName
$convertedfile = New-Item -path "$WorkingFolder\$convertedBaseName" -type file -Force
$textfile = [System.IO.File]::ReadAllText($NAVtransfile, $sourceencoding)
[System.IO.File]::WriteAllText($convertedfile, $textfile, $targetencoding)
Write-host $NAVtransfile 'OEM850 converted to UTF8' $convertedFileName
$TargetLanguage = Read-host "Target Language: de,es,fi,fr,nl,no,pt,sv,en-gb,..."
# Supported Translator Codes: https://msdn.microsoft.com/en-us/library/hh456380.aspx
Switch ($TargetLanguage)
{
"de" {$NAVTargetlangCode = '-A1031-'}
"es" {$NAVTargetlangCode = '-A1034-'}
"fi" {$NAVTargetlangCode = '-A1035-'}
"fr" {$NAVTargetlangCode = '-A1036-'}
"nl" {$NAVTargetlangCode = '-A1043-'}
"no" {$NAVTargetlangCode = '-A1044-'}
"pt" {$NAVTargetlangCode = '-A1046-'}
"sv" {$NAVTargetlangCode = '-A1053-'}
# "gsw" {$NAVTargetlangCode = '-A2055-'} Swiss German currently not supported
"en-gb" {$NAVTargetlangCode = '-A2057-'}
# "de" {$NAVlangCode = '-A3079-'} # activate in AT and deactivate de -> A1031
default {Throw "Please assign a NAV translation code for $NAVTargetlangCode in the script"}
}
$TranslatedFileName = [System.IO.Path]::GetDirectoryName($NAVtransfile) + "\"+ [System.IO.Path]::GetFileNameWithoutExtension($NAVtransfile) +"_$TargetLanguage" + [System.IO.Path]::GetExtension($NAVtransfile)
foreach ($line in [System.IO.File]::ReadLines($convertedFileName))
{
$SourceTextLine = $line.ToString()
if ($SourceTextLine.contains($SourceLangCode))
{
#region Construct Azure Datamarket access_token
#Get ClientId and Client_Secret from https://datamarket.azure.com/developer/applications/
#Refer obtaining AccessToken (http://msdn.microsoft.com/en-us/library/hh454950.aspx)
### Use your own account credentials here #########
# Note ClientID is the registered application id !
# Use the app's designated client_Secret
#################################################
$ClientID = '<---------------------->'
$client_Secret = '<---------------------->'
# If ClientId or Client_Secret has special characters, UrlEncode before sending request
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($ClientID)
$client_SecretEncoded = [System.Web.HttpUtility]::UrlEncode($client_Secret)
#Define uri for Azure Data Market
$Uri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
#Define the body of the request
$Body = "grant_type=client_credentials&client_id=$clientIDEncoded&client_secret=$client_SecretEncoded&scope=http://api.microsofttranslator.com"
#Define the content type for the request
$ContentType = "application/x-www-form-urlencoded"
#Invoke REST method. This handles the deserialization of the JSON result. Less effort than invoke-webrequest
$admAuth=Invoke-RestMethod -Uri $Uri -Body $Body -ContentType $ContentType -Method Post
#Construct the header value with the access_token just recieved
$HeaderValue = "Bearer " + $admauth.access_token
#endregion
$TargetLine = ''
Write-host "Source line $SourceTextLine"
$pos1 = $SourceTextLine.IndexOf(":")
# Start-Sleep -Seconds 1 -verbose
$SourceCodestringID = $SourceTextLine.Substring(0,$pos1)
$SourceCaption = $SourceTextLine.Substring($pos1 + 1)
Write-host "$SourceCaption" -ForegroundColor Green
# Cut off trailing #1###,#2### etc,as these are battered in the translation process, but not if at first pos)
$posHashKey = $SourceCaption.IndexOf("#")
$TrailingSourceCaption = '';
if ($posHashKey -gt 1)
{
$TrailingSourceCaption = $SourceCaption.Substring($posHashKey)
$SourceCaption = $SourceCaption.Substring(0,$posHashKey)
}
$TargetCodestringID = $SourceCodestringID -replace $SourceLangCode,$NAVTargetLangCode
$TargetTextLine = ''
Write-host "$TargetCodestringID" -ForegroundColor Yellow
#region Construct and invoke REST request to Microsoft Translator Service
[string] $text = $SourceCaption;
[string] $textEncoded = [System.Web.HttpUtility]::UrlEncode($text)
[string] $from = "en";
[string] $to = $TargetLanguage;
# Use category only if translation language pair has been deployed in the hub, otherwise without (Bing standard is used then)
[string] $uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + $textEncoded + "&from=" + $from + "&to=" + $to
#[string] $uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + $textEncoded + "&from=" + $from + "&to=" + $to + "&category=xxxxInsertYourCodeHerexxxx"
$result = Invoke-RestMethod -Uri $uri -Headers @{Authorization = $HeaderValue}
#endregion
$result.string.'#text'
$TargetTextLine = $result.string.'#text'
$TranslatedLine = $TargetCodestringID + ':' + $TargetTextLine + $TrailingSourceCaption
Write-host "$TargetTextLine$TrailingSourceCaption" -ForegroundColor Yellow
WriteToFile $TranslatedLine
#write-host "Waiting 2 seconds..."
# Start-Sleep -Seconds 2
}
}
if (Test-Path $translatedfilename)
{
$translatedfilename = resolve-path $translatedfilename
$WorkingFolder = Split-Path -Parent $translatedfilename
$sourceEncoding = [System.Text.Encoding]::GetEncoding(65001)
$targetEncoding = [System.Text.Encoding]::GetEncoding(850)
$OEM850FileName = [System.IO.Path]::GetFileNameWithoutExtension($translatedfilename) +"_OEM850" + [System.IO.Path]::GetExtension($translatedfilename)
$OEM850file = New-Item -path "$WorkingFolder\$OEM850FileName" -type file
$UTF8file = [System.IO.File]::ReadAllText($translatedfilename, $sourceencoding)
[System.IO.File]::WriteAllText($OEM850file, $UTF8file, $targetencoding)
Write-host $translatedfilename 'converted to' $OEM850FileName
}
}
get-content C:\temp\translations.txt -encoding oem | select-string -pattern "-A1033-" -allmatches | Out-File C:\temp\translations_ENU.txt -Width 1024 -Encoding oem
get-content C:\temp\translations.txt -encoding utf8 | select-string -pattern "-A1033-" -allmatches | Out-File C:\temp\translations_ENU.txt -Width 1024 -Encoding utf8
1. August 2017 16:18
$accountKey = "xxxxxxxxxxxxxxxxxxxxxxxx" # <- Enter your own key here !
[string] $uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + $textEncoded + "&from=" + $from + "&to=" + $to + "&category=<EnterYourOwnCategoryIDhere...>
Function MTfromNAVtranslateFileV2
{
Add-Type -AssemblyName System.Web
Function WriteToFile
{
out-file $TranslatedfileName -inputobject $args -force -append -Width 1024 -Encoding utf8
}
$NAVtransfile = Read-host "Existing NAV translation file with ENU -A1033- Captions"
$WorkingFolder = Split-Path -Parent $NAVtransfile
$lines = Get-Content $NAVtransfile | Measure-Object -Line
$Nooflines = $lines.Lines
write-host "File has $Nooflines lines"
$ENUlines = Get-Content $NAVtransfile | select-string -pattern "-A1033-" -Allmatches -CaseSensitive | Measure-Object -Line
$NoofENUlines = $ENUlines.Lines
write-host "File has $NoofENUlines ENU lines (Code A1033)"
$SourceLangCode = '-A1033-' # en = ENU
$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}
$convertedBaseName = Split-Path -Leaf $convertedFileName
$convertedfile = New-Item -path "$WorkingFolder\$convertedBaseName" -type file -Force
$textfile = [System.IO.File]::ReadAllText($NAVtransfile, $sourceencoding)
[System.IO.File]::WriteAllText($convertedfile, $textfile, $targetencoding)
Write-host $NAVtransfile 'OEM850 converted to UTF8' $convertedFileName
$TargetLanguage = Read-host "Target Language: de,es,fi,fr,nl,no,pt,sv,en-gb,..."
# Supported Translator Codes: https://msdn.microsoft.com/en-us/library/hh456380.aspx
Switch ($TargetLanguage)
{
"de" {$NAVTargetlangCode = '-A1031-'}
"es" {$NAVTargetlangCode = '-A1034-'}
"fi" {$NAVTargetlangCode = '-A1035-'}
"fr" {$NAVTargetlangCode = '-A1036-'}
"nl" {$NAVTargetlangCode = '-A1043-'}
"no" {$NAVTargetlangCode = '-A1044-'}
"pl" {$NAVTargetlangCode = '-A1045-'}
"pt" {$NAVTargetlangCode = '-A1046-'}
"ru" {$NAVTargetlangCode = '-A1049-'}
"sv" {$NAVTargetlangCode = '-A1053-'}
# "gsw" {$NAVTargetlangCode = '-A2055-'} Swiss German currently not supported
"en-gb" {$NAVTargetlangCode = '-A2057-'}
# "de" {$NAVlangCode = '-A3079-'} # activate in AT and deactivate de -> A1031
default {Throw "Please assign a NAV translation code for $NAVTargetlangCode in the script"}
}
$TranslatedFileName = [System.IO.Path]::GetDirectoryName($NAVtransfile) + "\"+ [System.IO.Path]::GetFileNameWithoutExtension($NAVtransfile) +"_$TargetLanguage" + [System.IO.Path]::GetExtension($NAVtransfile)
foreach ($line in [System.IO.File]::ReadLines($convertedFileName))
{
$SourceTextLine = $line.ToString()
if ($SourceTextLine.contains($SourceLangCode))
{
$accountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # <- Enter your own key here !
$tokenServiceURL = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
$query = "?Subscription-Key=$accountKey"
$uri = $tokenServiceUrl + $query
#Invoke REST method. This handles the deserialization of the JSON result. Less effort than invoke-webrequest
$token = Invoke-RestMethod -Uri $Uri -Method Post
#Construct the header value with the access_token just received
$auth = "Bearer " + $token
$header = @{Authorization = $auth}
$TargetLine = ''
Write-host "Source line $SourceTextLine"
$pos1 = $SourceTextLine.IndexOf(":")
# Start-Sleep -Seconds 1 -verbose
$SourceCodestringID = $SourceTextLine.Substring(0,$pos1)
$SourceCaption = $SourceTextLine.Substring($pos1 + 1)
Write-host "$SourceCaption" -ForegroundColor Green
# Cut off trailing #1###,#2### etc,as these are battered in the translation process, but not if at first pos)
$posHashKey = $SourceCaption.IndexOf("#")
$TrailingSourceCaption = ''
if ($posHashKey -gt 1)
{
$TrailingSourceCaption = $SourceCaption.Substring($posHashKey)
$SourceCaption = $SourceCaption.Substring(0,$posHashKey)
}
$TargetCodestringID = $SourceCodestringID -replace $SourceLangCode,$NAVTargetLangCode
$TargetTextLine = ''
Write-host "$TargetCodestringID" -ForegroundColor Yellow
#region Construct and invoke REST request to Microsoft Translator Service
[string] $text = $SourceCaption
[string] $textEncoded = [System.Web.HttpUtility]::UrlEncode($text)
[string] $from = "en"
[string] $to = $TargetLanguage
# Use category only if translation language pair has been deployed in the hub, otherwise without (Bing standard is used then)
[string] $uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + $textEncoded + "&from=" + $from + "&to=" + $to + "&contentType=text/plain"
#[string] $uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + $textEncoded + "&from=" + $from + "&to=" + $to + "&category=xxxxInsertYourCodeHerexxxx"
# $result = Invoke-RestMethod -Uri $uri -Headers @{Authorization = $HeaderValue}
$result = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
#endregion
$result.string.'#text'
$TargetTextLine = $result.string.'#text'
$TranslatedLine = $TargetCodestringID + ':' + $TargetTextLine + $TrailingSourceCaption
$TranslatedLine
Write-host "$TargetTextLine$TrailingSourceCaption" -ForegroundColor Yellow
WriteToFile $TranslatedLine
#write-host "Waiting 2 seconds..."
# Start-Sleep -Seconds 2
}
}
if (Test-Path $translatedfilename)
{
$translatedfilename = resolve-path $translatedfilename
$WorkingFolder = Split-Path -Parent $translatedfilename
$sourceEncoding = [System.Text.Encoding]::GetEncoding(65001)
# Check if your local code page matches, and modify if necessary
# 850 is for Western Europe, 852 for Central Europe, 866 for Russia etc.
$targetEncoding = [System.Text.Encoding]::GetEncoding(850)
$OEM850FileName = [System.IO.Path]::GetFileNameWithoutExtension($translatedfilename) +"_OEM850" + [System.IO.Path]::GetExtension($translatedfilename)
$OEM850file = New-Item -path "$WorkingFolder\$OEM850FileName" -type file
$UTF8file = [System.IO.File]::ReadAllText($translatedfilename, $sourceencoding)
[System.IO.File]::WriteAllText($OEM850file, $UTF8file, $targetencoding)
Write-host $translatedfilename 'converted to' $OEM850FileName
}
}
# Check if your local code page matches, and modify if necessary
# 850 is for Western Europe, 852 for Central Europe, 866 for Russia etc.