Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ suite('vscode API - languages', () => {
assert.ok(found);
});

// HINT: If this test fails, and you have been modifying code used in workers, you might have
// accidentally broken the workers. Check the logs for errors.
test('link detector', async function () {
const uri = await createRandomFile('class A { // http://a.com }', undefined, '.java');
const doc = await vscode.workspace.openTextDocument(uri);
Expand Down
14 changes: 13 additions & 1 deletion src/vs/base/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,19 @@ if (typeof navigator === 'object' && !isElectronRenderer) {
_isIOS = (_userAgent.indexOf('Macintosh') >= 0 || _userAgent.indexOf('iPad') >= 0 || _userAgent.indexOf('iPhone') >= 0) && !!navigator.maxTouchPoints && navigator.maxTouchPoints > 0;
_isLinux = _userAgent.indexOf('Linux') >= 0;
_isWeb = true;
_locale = navigator.language;

// Gather loader configuration since that contains the locale
let loaderConfiguration: any = null;
if (typeof globals.require !== 'undefined' && typeof globals.require.getConfig === 'function') {
// Get the configuration from the Monaco AMD Loader
loaderConfiguration = globals.require.getConfig();
} else if (typeof globals.requirejs !== 'undefined') {
// Get the configuration from requirejs
loaderConfiguration = globals.requirejs.s.contexts._.config;
}
const configuredLocale = loaderConfiguration?.['vs/nls']?.['availableLanguages']?.['*'] as string | undefined;
Comment on lines +85 to +93
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexdima do you know if this is this the best way to get the locale out of the loader configuration?

Copy link
Copy Markdown
Member

@alexdima alexdima May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could export proper API for it via vs/nls. Today, vs/nls exports a function called localize, as well as other functions like .setPseudoTranslation, .create, .load. You could export via proper API from vs/nls the current configured locale. You can then define typings for it at nls.d.ts. If you decide to do it, then please also add a mock implementation for it at nls.mock.ts.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo this is a great idea!

_locale = configuredLocale || navigator.language;

_language = _locale;
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/code/browser/workbench/workbench.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

// Set up nls if the user is not using the default language (English)
const nlsConfig = {};
const locale = navigator.language;
const locale = window.localStorage.getItem('vscode.nls.locale') || navigator.language;
if (!locale.startsWith('en')) {
nlsConfig['vs/nls'] = {
availableLanguages: {
Expand Down
13 changes: 13 additions & 0 deletions src/vs/platform/languagePacks/browser/languagePacks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { ILanguagePackItem, LanguagePackBaseService } from 'vs/platform/languagePacks/common/languagePacks';

export class WebLanguagePacksService extends LanguagePackBaseService {
// Web doesn't have a concept of language packs, so we just return an empty array
getInstalledLanguages(): Promise<ILanguagePackItem[]> {
return Promise.resolve([]);
}
}
2 changes: 1 addition & 1 deletion src/vs/platform/languagePacks/common/languagePacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface ILanguagePackService {
}

export abstract class LanguagePackBaseService extends Disposable implements ILanguagePackService {
_serviceBrand: undefined;
declare readonly _serviceBrand: undefined;

constructor(@IExtensionGalleryService private readonly extensionGalleryService: IExtensionGalleryService) {
super();
Expand Down
3 changes: 0 additions & 3 deletions src/vs/platform/languagePacks/node/languagePacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ interface ILanguagePack {
}

export class NativeLanguagePackService extends LanguagePackBaseService {

declare readonly _serviceBrand: undefined;

private readonly cache: LanguagePacksCache;

constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { registerAction2 } from 'vs/platform/actions/common/actions';
import { ClearDisplayLanguageAction, ConfigureDisplayLanguageAction } from 'vs/workbench/contrib/localization/browser/localizationsActions';

// Register action to configure locale and related settings
registerAction2(ConfigureDisplayLanguageAction);
registerAction2(ClearDisplayLanguageAction);
26 changes: 26 additions & 0 deletions src/vs/workbench/services/localization/browser/localeService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { language } from 'vs/base/common/platform';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { ILocaleService } from 'vs/workbench/services/localization/common/locale';

export class WebLocaleService implements ILocaleService {
declare readonly _serviceBrand: undefined;

async setLocale(locale: string | undefined): Promise<boolean> {
if (locale === language || (!locale && language === navigator.language)) {
return false;
}
if (locale) {
window.localStorage.setItem('vscode.nls.locale', locale);
} else {
window.localStorage.removeItem('vscode.nls.locale');
}
return true;
}
}

registerSingleton(ILocaleService, WebLocaleService, true);
7 changes: 7 additions & 0 deletions src/vs/workbench/workbench.web.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import 'vs/workbench/services/files/browser/elevatedFileService';
import 'vs/workbench/services/workingCopy/browser/workingCopyHistoryService';
import 'vs/workbench/services/userDataSync/browser/webUserDataSyncEnablementService';
import 'vs/workbench/services/configurationResolver/browser/configurationResolverService';
import 'vs/workbench/services/localization/browser/localeService';

import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
Expand All @@ -87,6 +88,8 @@ import { ITitleService } from 'vs/workbench/services/title/common/titleService';
import { TitlebarPart } from 'vs/workbench/browser/parts/titlebar/titlebarPart';
import { ITimerService, TimerService } from 'vs/workbench/services/timer/browser/timerService';
import { IDiagnosticsService, NullDiagnosticsService } from 'vs/platform/diagnostics/common/diagnostics';
import { ILanguagePackService } from 'vs/platform/languagePacks/common/languagePacks';
import { WebLanguagePacksService } from 'vs/platform/languagePacks/browser/languagePacks';

registerSingleton(IWorkbenchExtensionManagementService, ExtensionManagementService);
registerSingleton(IAccessibilityService, AccessibilityService, true);
Expand All @@ -103,6 +106,7 @@ registerSingleton(IExtensionTipsService, ExtensionTipsService);
registerSingleton(ITimerService, TimerService);
registerSingleton(ICustomEndpointTelemetryService, NullEndpointTelemetryService, true);
registerSingleton(IDiagnosticsService, NullDiagnosticsService, true);
registerSingleton(ILanguagePackService, WebLanguagePacksService, true);

//#endregion

Expand All @@ -118,6 +122,9 @@ import 'vs/workbench/contrib/logs/browser/logs.contribution';
// Explorer
import 'vs/workbench/contrib/files/browser/files.web.contribution';

// Localization
import 'vs/workbench/contrib/localization/browser/localization.contribution';

// Performance
import 'vs/workbench/contrib/performance/browser/performance.web.contribution';

Expand Down