This repository was archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Allow CLI UI language to be overridden by an environment variable #7021
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
60e2637
Allow CLI UI language to be overridden by an environment variable
nguerrera a1c423c
Honor UI language override in test runs
nguerrera 85573db
Respond to PR feedback: use method names not comments
nguerrera 3685317
Fix VSLANG handling typo
nguerrera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Globalization; | ||
|
|
||
| namespace Microsoft.DotNet.Cli | ||
| { | ||
| internal static class UILanguageOverride | ||
| { | ||
| private const string DOTNET_CLI_UI_LANGUAGE = nameof(DOTNET_CLI_UI_LANGUAGE); | ||
| private const string VSLANG = nameof(VSLANG); | ||
| private const string PreferredUILang = nameof(PreferredUILang); | ||
|
|
||
| public static void Setup() | ||
| { | ||
| CultureInfo language = GetOverriddenUILanguage(); | ||
| if (language != null) | ||
| { | ||
| ApplyOverrideToCurrentProcess(language); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| FlowOverrideToChildProcesses(language); | ||
| } | ||
| } | ||
|
|
||
| private static void ApplyOverrideToCurrentProcess(CultureInfo language) | ||
| { | ||
| CultureInfo.DefaultThreadCurrentUICulture = language; | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| } | ||
|
|
||
| private static void FlowOverrideToChildProcesses(CultureInfo language) | ||
| { | ||
| // Do not override any environment variables that are already set as we do not want to clobber a more granular setting with our global setting. | ||
| SetIfNotAlreadySet(DOTNET_CLI_UI_LANGUAGE, language.Name); | ||
| SetIfNotAlreadySet(VSLANG, language.LCID); // for tools following VS guidelines to just work in CLI | ||
| SetIfNotAlreadySet(PreferredUILang, language.Name); // for C#/VB targets that pass $(PreferredUILang) to compiler | ||
| } | ||
|
|
||
| private static CultureInfo GetOverriddenUILanguage() | ||
| { | ||
| // DOTNET_CLI_UI_LANGUAGE=<culture name> is the main way for users to customize the CLI's UI language. | ||
| string dotnetCliLanguage = Environment.GetEnvironmentVariable(DOTNET_CLI_UI_LANGUAGE); | ||
| if (dotnetCliLanguage != null) | ||
| { | ||
| try | ||
| { | ||
| return new CultureInfo(dotnetCliLanguage); | ||
| } | ||
| catch (CultureNotFoundException) { } | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| } | ||
|
|
||
| // VSLANG=<lcid> is set by VS and we respect that as well so that we will respect the VS | ||
| // language preference if we're invoked by VS. | ||
| string vsLang = Environment.GetEnvironmentVariable(VSLANG); | ||
| if (vsLang != null && int.TryParse(VSLANG, out int vsLcid)) | ||
| { | ||
| try | ||
| { | ||
| return new CultureInfo(vsLcid); | ||
| } | ||
| catch (ArgumentOutOfRangeException) { } | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| catch (CultureNotFoundException) { } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private static void SetIfNotAlreadySet(string environmentVariableName, string value) | ||
| { | ||
| string currentValue = Environment.GetEnvironmentVariable(environmentVariableName); | ||
| if (currentValue == null) | ||
| { | ||
| Environment.SetEnvironmentVariable(environmentVariableName, value); | ||
| } | ||
| } | ||
|
|
||
| private static void SetIfNotAlreadySet(string environmentVariableName, int value) | ||
| { | ||
| SetIfNotAlreadySet(environmentVariableName, value.ToString()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.