HowTo install a language pack using a custom action, based on a condition - Advanced Installer Community
Catalin
Posts: 7847
Joined: Wed Jun 13, 2018 7:49 am

HowTo install a language pack using a custom action, based on a condition

Hello everyone,

A client recently asked me whether an MSI built with Advanced installer can install Windows 11 language packs, including the Text-To-Speech (TTS) voices, but only on machines where a specific registry key exists.

This is really an interesting scenario and I decided to create this article, in case more users will be facing this in the future.

In this article, I will walk you through how to set that up, with a working example script.

The conditioning of the custom action will happen directly in the Custom Action code.

Here is a summary of what we'll do:

1. A PowerShell custom action
2. The script will test if a registry key exists and, based on that, do something

Here would be a general code snippet for that:

Code: Select all

# Registry key to check
$registryPath = "HKLM:\SOFTWARE\YourCompany\YourRegistryKey"

# Check whether the registry key exists
if (Test-Path -Path $registryPath) {

    # Registry key exists
    # Execute PowerShell commands to install the required
    # Windows 11 Language Pack and Text-to-Speech voices

    Install-LanguagePack # Replace with your actual commands

} else {

    # Registry key does not exist
    # Skip the Language Pack installation

}
The above example showcases a general execution of an action based on whether a registry key exists or not.

In the sample today, we will be using a different checking method, as for this specific scenario of checking whether a language is installed we have a predefined cmdlet in PowerShell that does just that - convenient, isn't it? :D

Here is the sample PowerShell code for this specific task:

Code: Select all

$ErrorActionPreference = 'Stop'
$lang = 'fr-FR'
$log  = Join-Path $env:ProgramData 'LangPackInstall.log'

try {
    # Step 1: what is already there?
    $fr = Get-InstalledLanguage -Language $lang -ErrorAction SilentlyContinue
    $packInstalled = $fr -and ("$($fr.LanguagePacks)" -match 'LpCab')

    $cap = Get-WindowsCapability -Online -Name "Language.TextToSpeech~~~$lang~0.0.1.0"
    $ttsInstalled = $cap.State -eq 'Installed'

    if ($packInstalled -and $ttsInstalled) { exit 0 }

    # Language pack
    if (-not $packInstalled) {
        Add-Content $log "$(Get-Date -Format s) Installing language pack $lang"
        Install-Language -Language $lang
    }

    # Make sure the TTS voice capability is there
    if ((Get-WindowsCapability -Online -Name $cap.Name).State -ne 'Installed') {
        Add-Content $log "$(Get-Date -Format s) Installing TTS capability $($cap.Name)"
        Add-WindowsCapability -Online -Name $cap.Name | Out-Null
    }

    Add-Content $log "$(Get-Date -Format s) Done"
    exit 0
}
catch {
    Add-Content $log "$(Get-Date -Format s) FAILED: $($_.Exception.Message)"
    exit 1
}
In addition to adding the language and enabling TextToSpeech, this custom action also creates a log file where it logs the actions.

Now, here is how to configure this inside your Advanced Installer project to ensure the language is installed together with your setup.

1. open Advanced Installer
2. go to the "Custom Actions" page
3. add a "PowerShell script inline" custom action, with sequence (the highlighted button)
Screenshot_184.png
Screenshot_184.png (25.44 KiB) Viewed 1841 times
4. after doing so, copy the code from above in the custom action:
Screenshot_185.png
Screenshot_185.png (134.79 KiB) Viewed 1841 times

Now, here is what the highlighted options do:

- the "When the system is being modified" + "Run under the LocalSystem account with full privileges (no imeprsonation)" options are enabled to ensure your custom action runs elevated, in case it needs admin rights to perform an operation

- the "Uninstall" and "Maintenance" being unchecked are to make sure the custom action only runs on Install. We wouldn't want the custom action to try and re-install the language pack when the end user uninstalls your product, right?

That would be all! :)

Now, after the installation of your setup, you should be able to see the language is installed.
Screenshot_187.png
Screenshot_187.png (61.46 KiB) Viewed 1841 times


Attached below you can also find the sample project, in case it is more convenient for you to download and use it.
PowerShell Sample to install Language Packs.aip
(17.16 KiB) Downloaded 21 times

Hope you will find this useful!

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Sample Projects”