TranslationHandling/Import-ITIObjectTranslation.ps1

<#
.SYNOPSIS
    Imports translation to BCContainer Database from a translation TXT file.
.EXAMPLE
    Import-ITIObjectTranslation -ContainerName BC14PL -Destination ~/MyApp/Translations/PLK.txt -LangaugeId "PLK"
#>

function Import-ITIObjectTranslation {
  param (
    [Parameter(Mandatory = $true)]
    [string]$ContainerName,
    [Parameter(Mandatory = $false)]
    [string]$LanguageId = "PLK",
    [string]$TranslationFile = ".\Translations\PLK.txt",
    [string]$filter = "Id=1..1999999999"
  )

  if ($filter) {
    if (!($filter.StartsWith('Id=', 'CurrentCultureIgnoreCase') -or $filter.ToLowerInvariant().Contains(';id='))) {
        $filter += ";Id=1..1999999999"
    }
  }
  else {
      $filter = "Id=1..1999999999"
  }

  $TranslationFile = (Get-ChildItem $TranslationFile).FullName

  #Copy Language File to container
  Invoke-ScriptInBcContainer -containerName $ContainerName -scriptblock {
    New-Item -ItemType Directory "C:\temp" -Force -ErrorAction Ignore | Out-Null
  }

  Copy-FileToBcContainer -containerName $ContainerName -localPath $TranslationFile -containerPath "C:\temp\$LanguageId.txt"
  
  Invoke-ScriptInBcContainer -containerName $ContainerName -argumentList $LanguageId,$filter -scriptblock {
    param($LanguageId,$filter)
    # Export-Objects
    $customConfigFile = Join-Path (Get-Item "C:\Program Files\Microsoft Dynamics NAV\*\Service").FullName "CustomSettings.config"
    [xml]$customConfig = [System.IO.File]::ReadAllText($customConfigFile)
    $DatabaseName = $customConfig.SelectSingleNode("//appSettings/add[@key='DatabaseName']").Value
    
    Write-Host "Exporting objects with filter $filter to translate" -ForegroundColor Yellow
    Export-NAVApplicationObject -DatabaseName $DatabaseName -Path "C:\temp\objects.txt" -Filter $filter -Force | Out-Null
    # Import Language to Objects
    Import-NAVApplicationObjectLanguage -Source "C:\temp\objects.txt" -LanguagePath "C:\temp\$LanguageId.txt" -LanguageId $LanguageId -Destination "C:\temp\objects_$LanguageId.txt" -Force
    
    #Import Translated Objects and compile
    Write-Host "Importing translated objects" -ForegroundColor Yellow 
    Import-NAVApplicationObject -DatabaseName $DatabaseName -Path "C:\temp\objects_$LanguageId.txt" -Confirm:$false -ImportAction "Overwrite"
    
    Write-Host "Compiling objects with filter $filter" -ForegroundColor Yellow
    Compile-NavApplicationObject -DatabaseName $DatabaseName -Filter $filter 
    
    Remove-Item -Path "C:\temp" -Recurse -Force -ErrorAction Ignore | Out-Null
  }
}

Export-ModuleMember -Function Import-ITIObjectTranslation