Zulgrib
Zulgrib

Reputation: 638

PowerShell multilingual function chaos

when defining a variable depending on end user's system language, it is possible to use Get-Culture and use the information to define different text for a variable based on the result, for example

$Lang = get-Culture                                                                 #Get user language
    if ($Lang.TwoLetterISOLanguageName -eq "fr") {                                      #If user is french,
        $Error_Argument = "French error text"                                           #define French text for argument error
        }
    else  {                                                                            #Else define English text
        $Error_Argument = "English error text"
    }

Now what happens with my method is, if there's more than two language, it starts to be an "if, else" hell, is there a better method for achieving this ? The point is, by priority : remain easy to read for function maintainer, using as less resources as possible.

Methods allowing adding translations on external files are welcome too.

Upvotes: 1

Views: 265

Answers (1)

nimizen
nimizen

Reputation: 3419

Perhaps look into using 'switch'. With switch we can check multiple values against a given value and also have a default value when the supplied value is not matched at all. For example:

$suppliedValue = 'FR'
switch ($suppliedValue ){
    "ES" {
        $Error_Argument = "Spanish error text"     
    }
    "FR" {
        $Error_Argument = "French error text"
    }
    default {
        $Error_Argument = "English error text"
    }
}

Upvotes: 2

Related Questions