Repository navigation
Closed
Labels
Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime
Description
Note: Perhaps this is by design (although there's at least a buggy inconsistency), given that there's currently no PowerShell mechanism for in-session switching to another culture. If so, it should at least be documented.
Steps to reproduce
[pscustomobject] @{ Test = 'initial'; Name = (Get-Culture).Name}
[cultureinfo]::currentculture = 'fr-FR'; [pscustomobject] @{ Test = '$PSCulture'; Name =$PSCulture }, [pscustomobject] @{ Test = 'GetCulture'; Name = (Get-Culture).Name }, [pscustomobject] @{ Test = '$host.CurrentCulture'; Name = $host.CurrentCulture.Name }, [pscustomobject] @{ Test = '.NET'; Name = [cultureinfo]::currentculture.name }Note:
- If you try the above interactively, be sure to execute the 2nd line above on a single line.
- The output below:
- assumes that
en-USis the initial culture at session startup time. - differs between PowerShell Core and Windows PowerShell: in Core,
Get-Cultureand$host.CurrentCulturereflect the switch, but$PSCulturedoesn't, whereas in Windows PowerShell none of the PS cmdlets/properties do.
- assumes that
Expected behavior
Test Name
---- ----
initial en-US
$PSCulture fr-FR
GetCulture fr-FR
$host.CurrentCulture fr-FR
.NET fr-FR
Actual behavior
- PowerShell Core: Windows, macOS, Ubuntu
Test Name
---- ----
initial en-US
$PSCulture en-US
GetCulture fr-FR
$host.CurrentCulture fr-FR
.NET fr-FR
- Windows PowerShell 5.1
Test Name
---- ----
initial en-US
$PSCulture en-US
GetCulture en-US
$host.CurrentCulture en-US
.NET fr-FR
Environment data
PowerShell Core v6.0.0-beta (v6.0.0-beta.1) on macOS 10.12.5
PowerShell Core v6.0.0-beta (v6.0.0-beta.1) on Ubuntu 16.04.1 LTS
PowerShell Core v6.0.0-beta (v6.0.0-beta.1) on Microsoft Windows 10 Pro (64-bit; v10.0.14393)
Windows PowerShell v5.1.14393.1198 on Microsoft Windows 10 Pro (64-bit; v10.0.14393)Reacted by Brian Scholer
Metadata
Metadata
Assignees
Labels
Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime
Activity
iSazonov commentedon May 20, 2017
PowerShell does not follow the changes in .Net. This is true for changing current directory too.
mklement0 commentedon May 20, 2017
Well, as you can see, in Core it does reflect .NET-level changes in
Get-Culture, but not in$PSCulture.Also, with respect to the current directory: there's justification for PowerShell "doing its own thing", because it has its own system of drives, the majority of which have no .NET counterpart.
The same justification doesn't apply to culture settings, which are shared by both worlds, especially given that cmdlets do already respect in-session switching; e.g.:
iSazonov commentedon May 20, 2017
That means we have to make sure PowerShell "not doing its own things" for Culture.
mklement0 commentedon May 20, 2017
Ilya (@iSazonov) - yes, and that condition appears to be met (I haven't dug into the source). Do you know something to the contrary? Or are you just saying that an investigation / official feedback is needed?
iSazonov commentedon May 20, 2017
Michael Klement (@mklement0) Please see Get-Culture code.
mklement0 commentedon May 21, 2017
Looking at the source code, it looks like
Get-Culturesimply delegates to$host.CurrentCulture, which is consistent with the (updated) findings above.In the case of a console host, this seems to lead to
ConsoleHost.cs, which has the following:That explains why Core's
Get-Culture/$host.CurrentCultureis always in sync with[cultureinfo]::CurrentCulture.For Windows PowerShell, the value is cached in
NativeCultureResolver.cs, which explains why it can get out of sync with the thread's changed-after-session-startup culture.So we can explain the disparity, but the question is what the design intent was and, therefore, how the inconsistency should be resolved (my vote is to always reflect the thread's then-current culture).
iSazonov commentedon May 21, 2017
I agree that a consistency should be. After quickly view codes, it seems PowerShell use Cultureinfo.CurrentCulture without caching.
[-]Get-Culture and $PSCulture do not (consistently) reflect in-session switches to another culture[/-][+]Get-Culture and $PSCulture do not (consistently) reflect in-session changes to another culture[/+]iSazonov commentedon Oct 21, 2018
Michael Klement (@mklement0) Could you please update a status of the issue? Should we consider a fix for $PSCulture?
mklement0 commentedon Oct 21, 2018
Ilya (@iSazonov):
The status hasn't changed. Yes, I think we should fix
$PSCultureto reflect the culture in effect for the thread (session) at hand, to be consistent withGet-Cultureand$host.CurrentCulture.It is a departure from Windows PowerShell, however, so it needs to be documented.
(Note that backward compatibility is already broken, due to
Get-Cultureand$host.CurrentCultureacting differently than in Windows PowerShell).iSazonov commentedon Oct 21, 2018
Steve Lee (@SteveL-MSFT) We need PowerShell Committee conclusion.
mklement0 commentedon Oct 22, 2018
See also: the related discussion in #3833 (comment)
1 remaining item
mklement0 commentedon Oct 24, 2018
Steve Lee (@SteveL-MSFT):
In Windows PowerShell:
Get-Culturereflects the current user's persistently defined culture, irrespective of (transient) in-session changes.$PSCultureand$host.CurrentCulturereflect the same value.In PowerShell Core:
Get-Culturealways reflects the current thread's culture, which may or may not have been changed in-session.$host.CurrentCulturereflects the same value, whereas$PSCulturereflects the culture that was in effect at session-startup time.The suggestion is to correct this inconsistency and have
$PSCulturereflect the same value asGet-Cultureand$host.CurrentCulture.As an aside: The above shows that we've already broken backward compatibility, though it won't typically surface, as changing the culture in-session is not common.
When it comes to introducing
Set-Cultureto PS Core, the difference becomes more problematic - see #3833 (comment).BrucePay commentedon Nov 26, 2018
$PSCultureand$PSUICultureare implemented as specific subclasses ofPSVariable. For example$PSCultureis defined in CultureVariable.cs and looks like:As you can see, it's not a cached value and should reflect the current culture.
mklement0 commentedon Nov 26, 2018
Bruce Payette (@BrucePay): That can't be the whole story, as the following test demonstrates:
BrucePay commentedon Nov 26, 2018
I suspect there's a bug where the special binding for
$PSCultureis being replaced by a regularPSVariable. I'm still investigating to what's going on.BrucePay commentedon Jan 22, 2019
And yes there is a bug right in the initialization code. In SessionStateScope.cs:SetVariable(), the code takes the carefully constructed custom-type object, extracts the basic information, creates a new vanilla PSVariable and inserts that into session state instead of using the custom variable. I would guess this has been broken for forever. Fixing it to use the original variable is technically a breaking change but it's probably worth it.
SteveL-MSFT commentedon Jan 23, 2019
@PowerShell/powershell-committee reviewed this. We agree the intent is that
Get-Cultureand$PSCulturehave the same behavior and reflects the current thread culture. This looks like a long standing bug.xjcl commentedon Oct 13, 2023
If anyone is stuck on an older version of PowerShell and wants a workaround for this problem, simply insert a subprocess call, e.g. replacing
(Get-Culture).Namewithpowershell "(Get-Culture).Name").For example at my workplace, the culture constantly get changed to
en-USfor no apparent reason. But for me it is essential that culture is set tode-DEso I can view;-separated CSV files in Excel (if the culture is set incorrectly, I would need to close all open Excel workbooks, open up PowerShell, issue aSet-Culturecommand, then re-open all Excel workbooks). Hence I added a task in Task Scheduler to fix this issue, the problem is thatSet-Culturecauses Windows Explorer to do a re-load to sort files alphabetically which is quite jarring. Hence why I need this workaround."Buggy" code:
Fixed code: