about_Script_Internationalization

应用到: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0

主题

about_Script_Internationalization

简短说明

介绍 Windows PowerShell® 2.0 的脚本国际化功能,可使脚本轻松使用用户界面 (UI) 语言向用户显示消息和说明。

详细说明

借助 Windows PowerShell 脚本国际化功能,通过采用用户的 UI 语言显示用于脚本和函数的帮助和用户消息,你可以更好地服为全球用户提供服务。

脚本国际化功能将在执行期间查询操作系统的 UI 区域性、导入相应的已翻译文本字符串,并向用户显示它们。借助数据部分,你可以将文本字符串与代码分开存储,以便轻松识别和提取它们。新的 cmdlet (ConvertFrom-StringData) 将文本字符串转换为类似字典的哈希表以便进行翻译。

脚本国际化中使用的 Windows PowerShell 2.0 功能不受 Windows PowerShell 1.0 支持。包含这些功能的脚本在未修改的情况下将不会在 Windows PowerShell 1.0 中运行。

为支持国际帮助文本,Windows PowerShell 2.0 包括以下功能:

  • - 将文本字符串与代码指令分隔开的数据部分。有关数据部分的详细信息,请参阅 about_Data_Sections。

  • - 新的自动变量:$PSCulture 和 $PSUICulture。$PSCulture 存储系统上用于元素(如日期、时间和货币)的 UI 语言的名称。$PSUICulture 变量存储系统上用于用户界面元素(如菜单和文本字符串)的 UI 语言的名称。

  • - 将文本字符串转换为类似字典的哈希表以便进行翻译的 cmdlet (ConvertFrom-StringData)。有关详细信息,请参阅 ConvertFrom-StringData。

  • - 存储已翻译文本字符串的新的文件类型 (.psd1)。该 .psd1 文件存储在脚本目录的特定于语言的子目录中。

  • - 在运行时将制定语言的已翻译文本字符串导入脚本的 cmdlet (Import-LocalizedData)。此 cmdlet 将以任何支持 Windows 的语言识别并导入字符串。有关详细信息,请参阅 Import-LocalizedData。

数据部分:存储默认字符串

使用脚本中的数据部分以默认语言存储文本字符串。在 here-string 的键/值对中排列字符串。每个键/值对必须单独一行。如果包含注释,注释必须单独一行。

ConvertFrom-StringData cmdlet 将 here-string 中的键/值对转换为存储在数据部分变量值中的类似字典的哈希表。

在下面的示例中,World.ps1 脚本的数据部分包含用于脚本的一组美国英语 (en-US) 提示消息。ConvertFrom-StringData cmdlet 将字符串转换为哈希表,并将它们存储在 $msgtable 变量中。

        $msgTable = Data {
            # culture=\"en-US\"
            ConvertFrom-StringData @'
                helloWorld = Hello, World.
                errorMsg1 = You cannot leave the user name field blank.
                promptMsg = Please enter your user name.
            '@
        }

有关 here-string 的详细信息,请参阅 about_Quoting_Rules。

PSD1 文件:存储已翻译的字符串

将每个 UI 语言的脚本消息保存在单独的文本文件中,并且名称与该脚本相同,文件扩展名为 .psd1。将文件存储在脚本目录的子目录中,并且区域性名称的格式如下所示:

        <language>–<region>

示例:de-DE、ar-SA 和 zh-Hans

例如,如果 World.ps1 脚本存储在 C:\Scripts 目录中,将创建如下所示的文件目录结构:

    C:\Scripts
        C:\Scripts\World.ps1
            C:\Scripts\de-DE\World.psd1
            C:\Scripts\ar-SA\World.psd1
            C:\Scripts\zh-CN\World.psd1
        ...

脚本目录的 de-DE 子目录中的 World.psd1 文件可能包括以下语句:

        ConvertFrom-StringData @'
            helloWorld = Hello, World (in German).
            errorMsg1 = You cannot leave the user name field blank (in German).
            promptMsg = Please enter your user name (in German).
        '@

同样,脚本目录的 ar-SA 子目录中的 World.psd1 文件可能包括以下语句:

        ConvertFrom-StringData @'
            helloWorld = Hello, World (in Arabic).
            errorMsg1 = You cannot leave the user name field blank (in Arabic).
            promptMsg = Please enter your user name (in Arabic).
        '@

IMPORT-LOCALIZEDDATA:已翻译字符串的动态检索

若要采用当前用户的 UI 语言检索字符串,请使用 Import-LocalizedData cmdlet。在此处插入部分正文。

Import-LocalizedData 查找 $PSUICulture 自动变量的值,并导入匹配 $PSUICulture 值的子目录中的 <script-name>.psd1 文件内容。然后,它将导入的内容保存在 BindingVariable 参数的值指定的变量中。

        import-localizeddata -bindingVariable msgTable

例如,如果 Import-LocalizedData 命令显示在 C:\Scripts\World.ps1 脚本中并且 $PSUICulture 的值是“ar-SA”,Import-LocalizedData 将查找以下文件:

         C:\Scripts\ar-SA\World.psd1

然后,它将来自该文件的阿拉伯语文本字符串导入 $msgTable 变量中,从而替换可能在 World.ps1 脚本的数据部分中定义的任何默认字符串。

结果是,当此脚本使用 $msgTable 变量显示用户消息时,将使用阿拉伯语显示该消息。

例如,以下脚本将使用阿拉伯语显示“请输入你的用户名”消息:

        if (!($username)) { $msgTable.promptMsg }

如果 Import-LocalizedData 找不到匹配 $PSUIculture 的值的 .psd1 文件,将不会替换 $msgTable 的值,并且对 $msgTable.promptMsg 的调用将显示回退 en-US 字符串。

示例

此示例演示如何在脚本中使用脚本国际化功能,以使用在计算机上设置的语言向用户显示一周的某一天。

下面是 Sample1.ps1 脚本文件的完整列表。

该脚本以包含 ConvertFrom-StringData 命令的名为 Day ($Day) 的数据部分开始。提交给 ConvertFrom-StringData 的表达式是 here-string,它在键/值对中以默认 UI 区域性 (en-US) 包含日名称。ConvertFrom-StringData cmdlet 将 here-string 中的键/值对转换为哈希表,然后将它保存在 $Day 变量的值中。

Import-LocalizedData 命令导入匹配 $PSUICulture 自动变量的值的目录中的 .psd1 文件内容,然后将其保存在 $Day 变量中,从而替换在数据部分中定义的 $Day 的值。

其余命令将字符串加载到某个数组中并显示它们。

        $Day = DATA {
        # culture=\"en-US\"
        ConvertFrom-StringData @'
        messageDate = Today is
            d0 = Sunday
            d1 = Monday
            d2 = Tuesday
            d3 = Wednesday
            d4 = Thursday
            d5 = Friday
            d6 = Saturday
        '@
        }


        Import-LocalizedData -BindingVariable Day

        # Build an array of weekdays.
        $a = $Day.d0, $Day.d1, $Day.d2, $Day.d3, $Day.d4, $Day.d5, $Day.d6


        # Get the day of the week as a number (Monday = 1).
        # Index into $a to get the name of the day.
        # Use string formatting to build a sentence.

        "{0} {1}" –f $Day.messageDate, $a[(get-date -uformat %u)] | Out-Host

支持脚本的 .psd1 文件保存在脚本目录(其名称与 $PSUICulture 值匹配)的子目录中。

下面是 .\de-DE\sample1.psd1 的完整列表:

        # culture=\"de-DE\"
        ConvertFrom-StringData @'
        messageDate = Today is 
            d0 = Sunday (in German)
            d1 = Monday (in German)
            d2 = Tuesday (in German)
            d3 = Wednesday (in German)
            d4 = Thursday (in German)
            d5 = Friday (in German)
            d6 = Saturday (in German)
        '@

结果是,当在 $PSUICulture 的值为 de-DE 的系统上运行 Sample.ps1 时,该脚本的输出如下所示:

        Today is Friday (in German)

另请参阅

about_Data_Sections

about_Automatic_Variables

about_Hash_Tables

about_Quoting_Rules

ConvertFrom-StringData

Import-LocalizedData