Repository: microsoft/terminal
File: tools/ConvertTo-PseudoLocalization.ps1 (PowerShell)

Code before:
    $hash = [System.Text.Encoding]::UTF8.GetBytes($key)
    $hash = [System.Security.Cryptography.SHA1]::Create().ComputeHash($hash)
    $hash = [System.BitConverter]::ToInt32($hash)
    $rng = [System.Random]::new($hash)

    $lines = $value -split '\r?\n'
    $lines = $lines | ForEach-Object {
        # Replace all characters with pseudo-localized characters
        $newValue = ''
        foreach ($char in $_.ToCharArray()) {
            if ($m = $mapping[$char]) {
                $newValue += $m[$rng.Next(0, $mapping[$char].Length)]
            }
            else {
                $newValue += $char
            }
        }

        # Replace all placeholders with their original values
        foreach ($kv in $placeholders.GetEnumerator()) {
            $newValue = $newValue.Replace($kv.Key, $kv.Value)
        }

        # Add 40% padding to the end of the string
        $paddingLength = [System.Math]::Round(0.4 * $newValue.Length)
        $padding = ' !!!' * ($paddingLength / 4 + 1)
        $newValue + $padding.Substring(0, $paddingLength)
    }
    $lines = $lines -join "`r`n"
    return $lines
}

if ($path.EndsWith(".resw")) {
    foreach ($entry in $content.SelectNodes('/root/data')) {
        $comment = $entry.SelectSingleNode('comment')?.'#text' ?? ''
        $entry.value = GetPseudoLocalization $entry.name $entry.value $comment
    }
}
elseif ($path.EndsWith(".xml")) {
    foreach ($parent in $content.DocumentElement.SelectNodes('//*[@_locID]')) {
        $locID = $parent.GetAttribute('_locID')
        $comment = $parent.SelectSingleNode('comment()[contains(., "_locComment_text")]')?.Value ?? ''

        foreach ($entry in $parent.SelectNodes('text()')) {
            $value = $entry.Value
            if ($value.Trim().Length -ne 0) {
                $entry.Value = GetPseudoLocalization $locID $value $comment
            }
        }
    }


Review comment (suggestion):
`$_` is the string with the single-character placeholders. The number of `!!!` at the end however corresponds to the original string length, so we need to use `$newValue` here (its length should be identical to the original `$value` length.

Code after:
    # because in the future PowerShell may enable UseRandomizedStringHashAlgorithm.
    $hash = [System.Text.Encoding]::UTF8.GetBytes($key)
    $hash = [System.Security.Cryptography.SHA1]::Create().ComputeHash($hash)
    $hash = [System.BitConverter]::ToInt32($hash)
    $rng = [System.Random]::new($hash)

    $lines = $value -split '\r?\n'
    $lines = $lines | ForEach-Object {
        # Replace all characters with pseudo-localized characters
        $newValue = ''
        foreach ($char in $_.ToCharArray()) {
            if ($m = $mapping[$char]) {
                $newValue += $m[$rng.Next(0, $mapping[$char].Length)]
            }
            else {
                $newValue += $char
            }
        }

        # Replace all placeholders with their original values
        foreach ($kv in $placeholders.GetEnumerator()) {
            $newValue = $newValue.Replace($kv.Key, $kv.Value)
        }

        # Add 40% padding to the end of the string
        $paddingLength = [System.Math]::Round(0.4 * $newValue.Length)
        $padding = ' !!!' * ($paddingLength / 4 + 1)
        $newValue + $padding.Substring(0, $paddingLength)
    }
    $lines = $lines -join "`r`n"
    return $lines
}

if ($path.EndsWith(".resw")) {
    foreach ($entry in $content.SelectNodes('/root/data')) {
        $comment = $entry.SelectSingleNode('comment')?.'#text' ?? ''
        $entry.value = GetPseudoLocalization $entry.name $entry.value $comment
    }
}
elseif ($path.EndsWith(".xml")) {
    foreach ($parent in $content.DocumentElement.SelectNodes('//*[@_locID]')) {
        $locID = $parent.GetAttribute('_locID')
        $comment = $parent.SelectSingleNode('comment()[contains(., "_locComment_text")]')?.Value ?? ''

        foreach ($entry in $parent.SelectNodes('text()')) {
            $value = $entry.Value
            if ($value.Trim().Length -ne 0) {
                $entry.Value = GetPseudoLocalization $locID $value $comment
            }
        }
    }