Identifying Multi-Language Online Documents (Part 1)

by Jun 13, 2022

In the previous tip we pointed you to the official PowerShell Language Definition which is available online in many different languages. Which raises the question about the supported languages.

Typically, multi-language online documents use the same URL and just change the language ID. For example, the English and the German edition just differ in their language ID:

https://docs.microsoft.com/de-de/powershell/scripting/lang-spec/chapter-01?view=powershell-7.2

https://docs.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-01?view=powershell-7.2

To automatically find out all the available translations, we first need a list of potential language IDs and construct hypothetical URLs. We then need a way to check whether these URLs actually exist. Let’s tackle the first part:

A list of all available language IDs can be retrieved like this:

[CultureInfo]::GetCultures([Globalization.CultureTypes]::SpecificCultures)

This creates a hash table for easy lookup with IDs that have a hyphen only (as web URLs typically use IDs with hyphens only):

$h = [CultureInfo]::GetCultures([Globalization.CultureTypes]::SpecificCultures) |
Where-Object {$_ -like '*-*' } | 
Group-Object -Property Name -AsHashTable 

We can now get a list of available language IDs, and we can look up its display name, too:

# list all languages:
$h.Keys
 
PS C:\> $h['de-de'].DisplayName 
German (Germany) 
 

To get a list of hypothetical URLs, simply iterate through all available language IDs and insert them one by one into the URL.

Note how I replaced the specific language ID such as “en-us” by a placeholder ({0}): the operator -f then inserts the language ID at this position inside the loop:

$URL = 'https://docs.microsoft.com/{0}/powershell/scripting/lang-spec/chapter-01'

$list = $h.Keys |
  ForEach-Object { $URL -f $_ } 

$list

We now have a list of potential URLs. In part 2 we’ll check this list to see which URLs actually exist.


Twitter This Tip! ReTweet this Tip!