File: LocalizationFunctions.ps1

package info (click to toggle)
azure-devops-cli-extension 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,680 kB
  • sloc: python: 160,797; xml: 198; sh: 61; makefile: 56
file content (150 lines) | stat: -rw-r--r-- 5,394 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
$script:resourceStrings = @{ }

<#
.SYNOPSIS
Gets a localized resource string.

.DESCRIPTION
Gets a localized resource string and optionally formats the string with arguments.

If the format fails (due to a bad format string or incorrect expected arguments in the format string), then the format string is returned followed by each of the arguments (delimited by a space).

If the lookup key is not found, then the lookup key is returned followed by each of the arguments (delimited by a space).

.PARAMETER Require
Writes an error to the error pipeline if the endpoint is not found.
#>
function Get-LocString {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 1)]
        [string]$Key,
        [Parameter(Position = 2)]
        [object[]]$ArgumentList = @( ))

    # Due to the dynamically typed nature of PowerShell, a single null argument passed
    # to an array parameter is interpreted as a null array.
    if ([object]::ReferenceEquals($null, $ArgumentList)) {
        $ArgumentList = @( $null )
    }

    # Lookup the format string.
    $format = ''
    if (!($format = $script:resourceStrings[$Key])) {
        # Warn the key was not found. Prevent recursion if the lookup key is the
        # "string resource key not found" lookup key.
        $resourceNotFoundKey = 'PSLIB_StringResourceKeyNotFound0'
        if ($key -ne $resourceNotFoundKey) {
            Write-Warning (Get-LocString -Key $resourceNotFoundKey -ArgumentList $Key)
        }

        # Fallback to just the key itself if there aren't any arguments to format.
        if (!$ArgumentList.Count) { return $key }

        # Otherwise fallback to the key followed by the arguments.
        $OFS = " "
        return "$key $ArgumentList"
    }

    # Return the string if there aren't any arguments to format.
    if (!$ArgumentList.Count) { return $format }

    try {
        [string]::Format($format, $ArgumentList)
    } catch {
        Write-Warning (Get-LocString -Key 'PSLIB_StringFormatFailed')
        $OFS = " "
        "$format $ArgumentList"
    }
}

<#
.SYNOPSIS
Imports resource strings for use with Get-VstsLocString.

.DESCRIPTION
Imports resource strings for use with Get-VstsLocString. The imported strings are stored in an internal resource string dictionary. Optionally, if a separate resource file for the current culture exists, then the localized strings from that file then imported (overlaid) into the same internal resource string dictionary.

Resource strings from the SDK are prefixed with "PSLIB_". This prefix should be avoided for custom resource strings.

.Parameter LiteralPath
JSON file containing resource strings.

.EXAMPLE
Import-VstsLocStrings -LiteralPath $PSScriptRoot\Task.json

Imports strings from messages section in the JSON file. If a messages section is not defined, then no strings are imported. Example messages section:
{
    "messages": {
        "Hello": "Hello you!",
        "Hello0": "Hello {0}!"
    }
}

.EXAMPLE
Import-VstsLocStrings -LiteralPath $PSScriptRoot\Task.json

Overlays strings from an optional separate resource file for the current culture.

Given the task variable System.Culture is set to 'de-DE'. This variable is set by the agent based on the current culture for the job.
Given the file Task.json contains:
{
    "messages": {
        "GoodDay": "Good day!",
    }
}
Given the file resources.resjson\de-DE\resources.resjson:
{
    "loc.messages.GoodDay": "Guten Tag!"
}

The net result from the import command would be one new key-value pair added to the internal dictionary: Key = 'GoodDay', Value = 'Guten Tag!'
#>
function Import-LocStrings {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string]$LiteralPath)

    # Validate the file exists.
    if (!(Test-Path -LiteralPath $LiteralPath -PathType Leaf)) {
        Write-Warning (Get-LocString -Key PSLIB_FileNotFound0 -ArgumentList $LiteralPath)
        return
    }

    # Load the json.
    Write-Verbose "Loading resource strings from: $LiteralPath"
    $count = 0
    if ($messages = (Get-Content -LiteralPath $LiteralPath -Encoding UTF8 | Out-String | ConvertFrom-Json).messages) {
        # Add each resource string to the hashtable.
        foreach ($member in (Get-Member -InputObject $messages -MemberType NoteProperty)) {
            [string]$key = $member.Name
            $script:resourceStrings[$key] = $messages."$key"
            $count++
        }
    }

    Write-Verbose "Loaded $count strings."

    # Get the culture.
    $culture = Get-TaskVariable -Name "System.Culture" -Default "en-US"

    # Load the resjson.
    $resjsonPath = "$([System.IO.Path]::GetDirectoryName($LiteralPath))\Strings\resources.resjson\$culture\resources.resjson"
    if (Test-Path -LiteralPath $resjsonPath) {
        Write-Verbose "Loading resource strings from: $resjsonPath"
        $count = 0
        $resjson = Get-Content -LiteralPath $resjsonPath -Encoding UTF8 | Out-String | ConvertFrom-Json
        foreach ($member in (Get-Member -Name loc.messages.* -InputObject $resjson -MemberType NoteProperty)) {
            if (!($value = $resjson."$($member.Name)")) {
                continue
            }

            [string]$key = $member.Name.Substring('loc.messages.'.Length)
            $script:resourceStrings[$key] = $value
            $count++
        }

        Write-Verbose "Loaded $count strings."
    }
}