Skip to content

Commit 3eb329e

Browse files
Implement UICulture configuration and command palette support
Co-authored-by: HeyItsGilbert <615265+HeyItsGilbert@users.noreply.github.com>
1 parent 289cafb commit 3eb329e

File tree

7 files changed

+149
-6
lines changed

7 files changed

+149
-6
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ to structure this file.
99
## [Unreleased]
1010

1111
### Added
12+
- UICulture configuration support for PowerShell localization
13+
- New `powershellLocalization.uiCulture` setting to specify default UI culture (e.g., 'en-US', 'fr-FR')
14+
- Setting validates culture format using pattern `^[a-z]{2}(-[A-Z]{2})?$`
15+
- Command palette commands for UICulture switching
16+
- `PowerShell Localization: Switch UI Culture` - Interactive input box for custom culture
17+
- `PowerShell Localization: Set UI Culture to English (en-US)` - Quick switch to English
18+
- `PowerShell Localization: Set UI Culture to French (fr-FR)` - Quick switch to French
19+
- Automatic cache clearing when UICulture changes
20+
- Decorations refresh automatically when culture is changed via settings or commands
21+
- Ensures localization values are immediately updated to reflect new culture
1222
- Windows PowerShell fallback support for better compatibility
1323
- Extension now automatically detects and uses available PowerShell executable
1424
- Tries PowerShell 7+ (`pwsh`) first, then falls back to Windows PowerShell 5.1 (`powershell`)

package.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,32 @@
7878
],
7979
"default": "info",
8080
"description": "Set the logging level for the PowerShell Localization extension. Debug logs are only shown when set to 'debug'."
81+
},
82+
"powershellLocalization.uiCulture": {
83+
"type": "string",
84+
"default": "en-US",
85+
"pattern": "^[a-z]{2}(-[A-Z]{2})?$",
86+
"description": "Specify the UI culture for PowerShell localization data. Use format like 'en-US', 'fr-FR', 'de-DE', etc."
8187
}
8288
}
83-
}
89+
},
90+
"commands": [
91+
{
92+
"command": "powershellLocalization.switchUICulture",
93+
"title": "Switch UI Culture",
94+
"category": "PowerShell Localization"
95+
},
96+
{
97+
"command": "powershellLocalization.setUICultureToEnUs",
98+
"title": "Set UI Culture to English (en-US)",
99+
"category": "PowerShell Localization"
100+
},
101+
{
102+
"command": "powershellLocalization.setUICultureToFrFr",
103+
"title": "Set UI Culture to French (fr-FR)",
104+
"category": "PowerShell Localization"
105+
}
106+
]
84107
},
85108
"scripts": {
86109
"bootstrap": "pwsh -NoLogo -NoProfile -ExecutionPolicy Bypass -File ./build.ps1 -Bootstrap",

src/configuration.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export class ConfigurationManager {
2121
'**/dist/**',
2222
'**/.git/**'
2323
]),
24-
logLevel: config.get<LogLevel>('logLevel', 'info')
24+
logLevel: config.get<LogLevel>('logLevel', 'info'),
25+
uiCulture: config.get<string>('uiCulture', 'en-US')
2526
};
2627
}
2728

@@ -53,6 +54,21 @@ export class ConfigurationManager {
5354
return this.getConfiguration().logLevel;
5455
}
5556

57+
/**
58+
* Gets the current UI culture
59+
*/
60+
public static getUICulture(): string {
61+
return this.getConfiguration().uiCulture;
62+
}
63+
64+
/**
65+
* Sets the UI culture
66+
*/
67+
public static async setUICulture(culture: string): Promise<void> {
68+
const config = vscode.workspace.getConfiguration(CONFIGURATION_BASENAME);
69+
await config.update('uiCulture', culture, vscode.ConfigurationTarget.Global);
70+
}
71+
5672
/**
5773
* Registers configuration change listener
5874
*/

src/decorationProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export class LocalizationDecorationProvider {
205205
return null;
206206
}
207207

208-
const localizationData = await this.powershellExecutor.parseLocalizationData(modulePath);
208+
const localizationData = await this.powershellExecutor.parseLocalizationData(modulePath, ConfigurationManager.getUICulture());
209209

210210
// Cache the result
211211
this.localizationCache.set(cacheKey, localizationData);

src/extensionManager.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export class ExtensionManager {
5757
// Set up file system watchers
5858
this.setupFileSystemWatchers();
5959

60+
// Register command palette commands
61+
this.registerCommands();
62+
6063
this.logger.info('PowerShell Localization extension activated successfully');
6164

6265
} catch (error) {
@@ -130,6 +133,40 @@ export class ExtensionManager {
130133
this.context.subscriptions.push(psm1Watcher, psd1Watcher);
131134
}
132135

136+
/**
137+
* Registers command palette commands
138+
*/
139+
private registerCommands(): void {
140+
// Register switch UI culture command
141+
const switchCommand = vscode.commands.registerCommand(
142+
'powershellLocalization.switchUICulture',
143+
async () => {
144+
await this.handleSwitchUICulture();
145+
}
146+
);
147+
148+
// Register set to en-US command
149+
const setEnUsCommand = vscode.commands.registerCommand(
150+
'powershellLocalization.setUICultureToEnUs',
151+
async () => {
152+
await this.handleSetUICulture('en-US');
153+
}
154+
);
155+
156+
// Register set to fr-FR command
157+
const setFrFrCommand = vscode.commands.registerCommand(
158+
'powershellLocalization.setUICultureToFrFr',
159+
async () => {
160+
await this.handleSetUICulture('fr-FR');
161+
}
162+
);
163+
164+
this.disposables.push(switchCommand, setEnUsCommand, setFrFrCommand);
165+
this.context.subscriptions.push(switchCommand, setEnUsCommand, setFrFrCommand);
166+
167+
this.logger.info('Command palette commands registered');
168+
}
169+
133170
/**
134171
* Handles configuration changes
135172
*/
@@ -163,6 +200,57 @@ export class ExtensionManager {
163200
}
164201
}
165202

203+
/**
204+
* Handles switching UI culture via input box
205+
*/
206+
private async handleSwitchUICulture(): Promise<void> {
207+
try {
208+
const currentCulture = ConfigurationManager.getUICulture();
209+
const inputCulture = await vscode.window.showInputBox({
210+
prompt: 'Enter UI Culture (e.g., en-US, fr-FR, de-DE)',
211+
value: currentCulture,
212+
validateInput: (value) => {
213+
const culturePattern = /^[a-z]{2}(-[A-Z]{2})?$/;
214+
if (!culturePattern.test(value)) {
215+
return 'Invalid culture format. Use format like "en-US", "fr-FR", "de-DE"';
216+
}
217+
return null;
218+
}
219+
});
220+
221+
if (inputCulture && inputCulture !== currentCulture) {
222+
await this.handleSetUICulture(inputCulture);
223+
}
224+
} catch (error) {
225+
this.logger.error('Failed to switch UI culture', error as Error);
226+
vscode.window.showErrorMessage(`Failed to switch UI culture: ${(error as Error).message}`);
227+
}
228+
}
229+
230+
/**
231+
* Handles setting UI culture to a specific value
232+
*/
233+
private async handleSetUICulture(culture: string): Promise<void> {
234+
try {
235+
const currentCulture = ConfigurationManager.getUICulture();
236+
if (culture === currentCulture) {
237+
vscode.window.showInformationMessage(`UI Culture is already set to ${culture}`);
238+
return;
239+
}
240+
241+
await ConfigurationManager.setUICulture(culture);
242+
243+
// Clear the cache since culture has changed
244+
this.decorationProvider.clearCache();
245+
246+
vscode.window.showInformationMessage(`UI Culture changed to ${culture}`);
247+
this.logger.info(`UI Culture changed from ${currentCulture} to ${culture}`);
248+
} catch (error) {
249+
this.logger.error(`Failed to set UI culture to ${culture}`, error as Error);
250+
vscode.window.showErrorMessage(`Failed to set UI culture: ${(error as Error).message}`);
251+
}
252+
}
253+
166254
/**
167255
* Disposes of all resources
168256
*/

src/powershellExecutor.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ export class PowerShellExecutor {
1717
/**
1818
* Executes the LocalizationParser.ps1 script for a given module
1919
*/
20-
public async parseLocalizationData(modulePath: string): Promise<LocalizationData> {
21-
this.logger.debug(`Parsing localization data for module: ${modulePath}`);
20+
public async parseLocalizationData(modulePath: string, uiCulture?: string): Promise<LocalizationData> {
21+
this.logger.debug(`Parsing localization data for module: ${modulePath} with culture: ${uiCulture || 'default'}`);
2222

2323
try {
2424
const scriptPath = path.join(__dirname, '..', 'resources', 'LocalizationParser.ps1');
2525

26-
// TODO: Add support for loading a specific UICulture
2726
const args = ['-ModuleFile', modulePath];
27+
28+
// Add UICulture parameter if provided
29+
if (uiCulture) {
30+
args.push('-UICulture', uiCulture);
31+
}
2832

2933
const output = await this.executeScript(scriptPath, args);
3034

@@ -138,6 +142,7 @@ export class PowerShellExecutor {
138142
'-Command',
139143
'-File',
140144
'-ModuleFile',
145+
'-UICulture',
141146
'-Path',
142147
'-Name',
143148
'-Filter',

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface ExtensionConfig {
1818
enableDecorations: boolean;
1919
searchExclude: string[];
2020
logLevel: LogLevel;
21+
uiCulture: string;
2122
}
2223

2324
export type LogLevel = 'error' | 'warn' | 'info' | 'debug';

0 commit comments

Comments
 (0)