Skip to content

Get-Culture and $PSCulture do not (consistently) reflect in-session changes to another culture #3831

Closed

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-US is the initial culture at session startup time.
    • differs between PowerShell Core and Windows PowerShell: in Core, Get-Culture and $host.CurrentCulture reflect the switch, but $PSCulture doesn't, whereas in Windows PowerShell none of the PS cmdlets/properties do.

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)

Activity

  1. added
    WG-Engine
    Issue-Question
    on May 20, 2017
  2. iSazonov commented on May 20, 2017

    @iSazonov
    Collaborator

    PowerShell does not follow the changes in .Net. This is true for changing current directory too.

  3. mklement0 commented on May 20, 2017

    @mklement0
    ContributorAuthor

    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.:

    > [cultureinfo]::currentculture = 'fr-FR'; Get-Date
    
    samedi 20 mai 2017 13:39:08
    
  4. iSazonov commented on May 20, 2017

    @iSazonov
    Collaborator

    That means we have to make sure PowerShell "not doing its own things" for Culture.

  5. mklement0 commented on May 20, 2017

    @mklement0
    ContributorAuthor

    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?

  6. iSazonov commented on May 20, 2017

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) Please see Get-Culture code.

  7. mklement0 commented on May 21, 2017

    @mklement0
    ContributorAuthor

    Looking at the source code, it looks like Get-Culture simply 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:

            public override System.Globalization.CultureInfo CurrentCulture
            {
                get
                {
                    lock (hostGlobalLock)
                    {
    #if !CORECLR
                        return NativeCultureResolver.Culture;
    #else
                        return CultureInfo.CurrentCulture;
    #endif
                    }
                }
            }
    • That explains why Core's Get-Culture / $host.CurrentCulture is 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).

  8. iSazonov commented on May 21, 2017

    @iSazonov
    Collaborator

    I agree that a consistency should be. After quickly view codes, it seems PowerShell use Cultureinfo.CurrentCulture without caching.

  9. changed the title [-]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[/+] on Jul 26, 2017
  10. iSazonov commented on Oct 21, 2018

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) Could you please update a status of the issue? Should we consider a fix for $PSCulture?

  11. mklement0 commented on Oct 21, 2018

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov):

    The status hasn't changed. Yes, I think we should fix $PSCulture to reflect the culture in effect for the thread (session) at hand, to be consistent with Get-Culture and $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-Culture and $host.CurrentCulture acting differently than in Windows PowerShell).

  12. iSazonov commented on Oct 21, 2018

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) We need PowerShell Committee conclusion.

  13. added
    Review - Committee
    on Oct 21, 2018
  14. mklement0 commented on Oct 22, 2018

    @mklement0
    ContributorAuthor

    See also: the related discussion in #3833 (comment)

  15. 1 remaining item

  16. mklement0 commented on Oct 24, 2018

    @mklement0
    ContributorAuthor

    Steve Lee (@SteveL-MSFT):

    In Windows PowerShell:

    • Get-Culture reflects the current user's persistently defined culture, irrespective of (transient) in-session changes.
    • $PSCulture and $host.CurrentCulture reflect the same value.

    In PowerShell Core:

    • Get-Culture always reflects the current thread's culture, which may or may not have been changed in-session.

    • $host.CurrentCulture reflects the same value, whereas $PSCulture reflects the culture that was in effect at session-startup time.

    The suggestion is to correct this inconsistency and have $PSCulture reflect the same value as Get-Culture and $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-Culture to PS Core, the difference becomes more problematic - see #3833 (comment).

  17. BrucePay commented on Nov 26, 2018

    @BrucePay
    Collaborator

    $PSCulture and $PSUICulture are implemented as specific subclasses of PSVariable . For example $PSCulture is defined in CultureVariable.cs and looks like:

        /// <summary>
        /// A variable that represents $PSCulture
        /// </summary>
        internal class PSCultureVariable : PSVariable
        {
            /// <summary>
            /// Constructs an instance of the variable.
            /// </summary>
            internal PSCultureVariable()
                : base(SpecialVariables.PSCulture, true, ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope,
                       RunspaceInit.DollarPSCultureDescription)
            {
            }
    
            /// <summary>
            /// Gets or sets the value of the variable.
            /// </summary>
            ///
            public override object Value
            {
                get
                {
                    DebuggerCheckVariableRead();
                    return System.Globalization.CultureInfo.CurrentCulture.Name;
                }
            }
        }

    As you can see, it's not a cached value and should reflect the current culture.

  18. mklement0 commented on Nov 26, 2018

    @mklement0
    ContributorAuthor

    Bruce Payette (@BrucePay): That can't be the whole story, as the following test demonstrates:

    # With en-US in effect *at session startup*:
    PS> [cultureinfo]::CurrentCulture = 'de-dE'; [cultureinfo]::CurrentCulture.Name; $PSCulture
    de-DE
    en-US  # !! session-startup value is still in effect
  19. BrucePay commented on Nov 26, 2018

    @BrucePay
    Collaborator

    I suspect there's a bug where the special binding for $PSCulture is being replaced by a regular PSVariable. I'm still investigating to what's going on.

  20. added
    Issue-Bug
    and removed
    Issue-Question
    on Nov 27, 2018
  21. BrucePay commented on Jan 22, 2019

    @BrucePay
    Collaborator

    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.

  22. added
    Breaking-Change
    on Jan 23, 2019
  23. SteveL-MSFT commented on Jan 23, 2019

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this. We agree the intent is that Get-Culture and $PSCulture have the same behavior and reflects the current thread culture. This looks like a long standing bug.

  24. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Jan 23, 2019
  25. xjcl commented on Oct 13, 2023

    @xjcl

    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).Name with powershell "(Get-Culture).Name").

    For example at my workplace, the culture constantly get changed to en-US for no apparent reason. But for me it is essential that culture is set to de-DE so 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 a Set-Culture command, then re-open all Excel workbooks). Hence I added a task in Task Scheduler to fix this issue, the problem is that Set-Culture causes Windows Explorer to do a re-load to sort files alphabetically which is quite jarring. Hence why I need this workaround.

    "Buggy" code:

    while (1) { if ((Get-Culture).Name -ne 'de-DE') { echo "Set-Culture"; Set-Culture de-DE }; sleep 10 }

    Fixed code:

    while (1) { if ((powershell "(Get-Culture).Name") -ne 'de-DE') { echo "Set-Culture"; Set-Culture de-DE }; sleep 10 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-ChangeCommittee-ReviewedIssue-BugResolution-FixedWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions