Scripting. Powershell, VMware, Windows, Active Directory & Exchange. All that kind of stuff…..
RSS icon Email icon Home icon
  • PowerShell 2.0: One Cmdlet at a Time #106 Import-LocalizedData

    Posted on June 28th, 2010 Jonathan Medd No comments

    Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Import-LocalizedData cmdlet.

    What can I do with it?

    Enable text in scripts displayed to users to be presented in their own language. The cmdlet uses the automatic variable $PSUICulture to determine the language to use and alternate text is stored within .psd1 files in subdirectories of the folder that the script is stored.

    Example:

    In a script called RegionalTest.ps1 use the ConvertFrom-StringData cmdlet to create a series of text messages to display to the user. Import-LocalizedData will retrieve the value of the $PSUICulture automatic variable, get the contents of the RegionalTest.psd1 file in the es-ES directory (assume the user is Spanish) and store the data within the variable designated by the BindingVariable parameter. Then display the Welcome text.

    $UserMessages = Data {
        # culture="en-US"
        ConvertFrom-StringData @'
        Welcome = Welcome to the application
        Error1 = You have entered an incorrect username
        Error2 = You have entered an incorrect password
    '@
        }
    
    Import-LocalizedData -BindingVariable $UserMessages
    
    $UserMessages.Welcome

    The contents of the RegionalTest.psd1 file for Spanish would look like (apologies for any bad translation!)

    ConvertFrom-StringData @'
        Welcome = Recepción al uso
        Error1 = Usted ha incorporado un username incorrecto
        Error2 = Usted ha incorporado una contraseƱa incorrecta
    '@

    and be stored in the es-ES folder below C:\Scripts where RegionalTest.ps1 lives

    Import-LocalizedData

    When run on the Spanish user’s machine the Spanish text would be displayed rather than the original English.

    How could I have done this in PowerShell 1.0?

    Script Internationalisation features were introduced in PowerShell 2.0 and not supported in version 1.0 – more info here.

    1000 things 1% better!

    Leave a reply