TranslationHandling/Import-ITIObjectTranslation.ps1

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

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
    $DatabaseName = Get-NAVServerInstance | Get-NAVServerConfiguration -KeyName DatabaseName
    
    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