jdhitsolutions / PSScriptTools

:wrench: :hammer: A set of PowerShell functions you might use to enhance your own functions and scripts or to facilitate working in the console. Most should work in both Windows PowerShell and PowerShell 7, even cross-platform. Any operating system limitations should be handled on a per command basis. The Samples folder contains demonstration script files
MIT License
875 stars 110 forks source link

[Question] Mystical Test-WithCulture #128

Closed scriptingstudio closed 2 years ago

scriptingstudio commented 2 years ago

The function works wonderfully, but how it is not clear. I am trying to set new culture

[System.Threading.Thread]::CurrentThread.CurrentUICulture

LCID             Name             DisplayName
----             ----             -----------
1033             en-US            English (United States)

[System.Threading.Thread]::CurrentThread.CurrentUICulture = [cultureinfo]'fr-fr'

# looks like nothing happens
[System.Threading.Thread]::CurrentThread.CurrentUICulture

LCID             Name             DisplayName
----             ----             -----------
1033             en-US            English (United States)

# getting object info
[System.Threading.Thread]::CurrentThread.CurrentUICulture | gm

   TypeName: Microsoft.PowerShell.VistaCultureInfo

Name                           MemberType Definition
----                           ---------- ----------
ClearCachedData                Method     void ClearCachedData()
Clone                          Method     System.Object Clone(), System.Object ICloneable.Clone()
Equals                         Method     bool Equals(System.Object value)
GetConsoleFallbackUICulture    Method     cultureinfo GetConsoleFallbackUICulture()
GetFormat                      Method     System.Object GetFormat(type formatType), System.Object IFormatProvider.Ge...
GetHashCode                    Method     int GetHashCode()
GetType                        Method     type GetType()
ToString                       Method     string ToString()
Calendar                       Property   System.Globalization.Calendar Calendar {get;}
CompareInfo                    Property   System.Globalization.CompareInfo CompareInfo {get;}
CultureTypes                   Property   System.Globalization.CultureTypes CultureTypes {get;}
DateTimeFormat                 Property   System.Globalization.DateTimeFormatInfo DateTimeFormat {get;set;}
DisplayName                    Property   string DisplayName {get;}
EnglishName                    Property   string EnglishName {get;}
IetfLanguageTag                Property   string IetfLanguageTag {get;}
IsNeutralCulture               Property   bool IsNeutralCulture {get;}
IsReadOnly                     Property   bool IsReadOnly {get;}
KeyboardLayoutId               Property   int KeyboardLayoutId {get;}
LCID                           Property   int LCID {get;}
Name                           Property   string Name {get;}
NativeName                     Property   string NativeName {get;}
NumberFormat                   Property   System.Globalization.NumberFormatInfo NumberFormat {get;set;}
OptionalCalendars              Property   System.Globalization.Calendar[] OptionalCalendars {get;}
Parent                         Property   cultureinfo Parent {get;}
TextInfo                       Property   System.Globalization.TextInfo TextInfo {get;}
ThreeLetterISOLanguageName     Property   string ThreeLetterISOLanguageName {get;}
ThreeLetterWindowsLanguageName Property   string ThreeLetterWindowsLanguageName {get;}
TwoLetterISOLanguageName       Property   string TwoLetterISOLanguageName {get;}
UseUserOverride                Property   bool UseUserOverride {get;}

All properties but DateTimeFormat are readonly. I admit I don't understand how it works but it really does. How?

The same happens when I am playing with this

[cultureinfo]::CurrentUICulture = [cultureinfo]'fr-fr'

Where am I doing wrong?

jdhitsolutions commented 2 years ago

Test-WithCulture is designed to run a scriptblock on a new thread with a different culture.

image

image

You cannot use it to permanently change the culture on your computer.

scriptingstudio commented 2 years ago

Got it. That was the point I was confused. An expression like [System.Threading.Thread]::CurrentThread.CurrentUICulture = [cultureinfo]'fr-fr' does not work directly from console. Thanks.