Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass display language as a locale to Electron #159958

Merged
merged 10 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
41 changes: 38 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ function configureCommandlineSwitchesSync(cliArgs) {
'disable-hardware-acceleration',

// override for the color profile to use
'force-color-profile'
'force-color-profile',

// locale for Electron to use
'locale'
];

if (process.platform === 'linux') {
Expand All @@ -177,6 +180,8 @@ function configureCommandlineSwitchesSync(cliArgs) {
// Read argv config
const argvConfig = readArgvConfigSync();

let hasLocaleSwitch = false;
rzhao271 marked this conversation as resolved.
Show resolved Hide resolved
const isInsiderOrExploration = product.quality === 'insider' || product.quality === 'exploration';
Object.keys(argvConfig).forEach(argvKey => {
const argvValue = argvConfig[argvKey];

Expand All @@ -190,6 +195,21 @@ function configureCommandlineSwitchesSync(cliArgs) {
}
}

// Locale
else if (argvKey === 'locale') {
if (isInsiderOrExploration) {
// Pass in the locale to Electron so that the
// Windows Control Overlay is rendered correctly.
// If the locale is `qps-ploc`, the Microsoft
// Pseudo Language Language Pack is being used.
// In that case, use `en` as the Electron locale.
const localeToUse = (!argvValue || argvValue === 'qps-ploc') ?
deepak1556 marked this conversation as resolved.
Show resolved Hide resolved
'en' : argvValue;
app.commandLine.appendSwitch('lang', localeToUse);
hasLocaleSwitch = true;
}
}

// Others
else if (argvValue === true || argvValue === 'true') {
if (argvKey === 'disable-hardware-acceleration') {
Expand Down Expand Up @@ -220,6 +240,11 @@ function configureCommandlineSwitchesSync(cliArgs) {
}
});

if (!hasLocaleSwitch && isInsiderOrExploration) {
// Default Electron's locale to English
app.commandLine.appendSwitch('lang', 'en');
}

/* Following features are disabled from the runtime.
* `CalculateNativeWinOcclusion` - Disable native window occlusion tracker,
* Refs https://groups.google.com/a/chromium.org/g/embedder-dev/c/ZF3uHHyWLKw/m/VDN2hDXMAAAJ
Expand Down Expand Up @@ -551,11 +576,21 @@ async function resolveNlsConfiguration() {
// Try to use the app locale. Please note that the app locale is only
// valid after we have received the app ready event. This is why the
// code is here.
let appLocale = app.getLocale();

// The ternary and ts-ignore can both be removed once Electron
// officially adopts the getPreferredSystemLanguages API.
// Ref https://github.com/microsoft/vscode/issues/159813
// and https://github.com/electron/electron/pull/36035
/**
* @type string
*/
// @ts-ignore API not yet available in the official Electron
let appLocale = ((product.quality === 'insider' || product.quality === 'exploration') && app?.getPreferredSystemLanguages()?.length) ?
// @ts-ignore API not yet available in the official Electron
app.getPreferredSystemLanguages()[0] : app.getLocale();
if (!appLocale) {
nlsConfiguration = { locale: 'en', availableLanguages: {} };
} else {

// See above the comment about the loader and case sensitiveness
appLocale = appLocale.toLowerCase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ export class LocalizationWorkbenchContribution extends Disposable implements IWo

private checkAndInstall(): void {
const language = platform.language;
const locale = platform.locale;
let locale = platform.locale ?? '';
if (locale.startsWith('zh-hans')) {
locale = 'zh-cn';
} else if (locale.startsWith('zh-hant')) {
locale = 'zh-tw';
}
const languagePackSuggestionIgnoreList = <string[]>JSON.parse(this.storageService.get(LANGUAGEPACK_SUGGESTION_IGNORE_STORAGE_KEY, StorageScope.APPLICATION, '[]'));

if (!this.galleryService.isEnabled()) {
Expand All @@ -96,118 +101,121 @@ export class LocalizationWorkbenchContribution extends Disposable implements IWo
if (!language || !locale || locale === 'en' || locale.indexOf('en-') === 0) {
return;
}
if (language === locale || languagePackSuggestionIgnoreList.indexOf(locale) > -1) {
if (locale.startsWith(language) || languagePackSuggestionIgnoreList.includes(locale)) {
return;
}

this.isLanguageInstalled(locale)
.then(installed => {
this.isLocaleInstalled(locale)
.then(async (installed) => {
if (installed) {
return;
}

this.galleryService.query({ text: `tag:lp-${locale}` }, CancellationToken.None).then(tagResult => {
let searchLocale = locale;
let tagResult = await this.galleryService.query({ text: `tag:lp-${searchLocale}` }, CancellationToken.None);
if (tagResult.total === 0) {
// Trim the locale and try again.
searchLocale = locale.split('-')[0];
tagResult = await this.galleryService.query({ text: `tag:lp-${searchLocale}` }, CancellationToken.None);
if (tagResult.total === 0) {
return;
}
}

const extensionToInstall = tagResult.total === 1 ? tagResult.firstPage[0] : tagResult.firstPage.filter(e => e.publisher === 'MS-CEINTL' && e.name.indexOf('vscode-language-pack') === 0)[0];
const extensionToFetchTranslationsFrom = extensionToInstall || tagResult.firstPage[0];
const extensionToInstall = tagResult.total === 1 ? tagResult.firstPage[0] : tagResult.firstPage.find(e => e.publisher === 'MS-CEINTL' && e.name.startsWith('vscode-language-pack'));
const extensionToFetchTranslationsFrom = extensionToInstall ?? tagResult.firstPage[0];

if (!extensionToFetchTranslationsFrom.assets.manifest) {
return;
}
if (!extensionToFetchTranslationsFrom.assets.manifest) {
return;
}

Promise.all([this.galleryService.getManifest(extensionToFetchTranslationsFrom, CancellationToken.None), this.galleryService.getCoreTranslation(extensionToFetchTranslationsFrom, locale)])
.then(([manifest, translation]) => {
const loc = manifest && manifest.contributes && manifest.contributes.localizations && manifest.contributes.localizations.filter(x => x.languageId.toLowerCase() === locale)[0];
const languageName = loc ? (loc.languageName || locale) : locale;
const languageDisplayName = loc ? (loc.localizedLanguageName || loc.languageName || locale) : locale;
const translationsFromPack: { [key: string]: string } = translation?.contents?.['vs/workbench/contrib/localization/electron-sandbox/minimalTranslations'] ?? {};
const promptMessageKey = extensionToInstall ? 'installAndRestartMessage' : 'showLanguagePackExtensions';
const useEnglish = !translationsFromPack[promptMessageKey];
Promise.all([this.galleryService.getManifest(extensionToFetchTranslationsFrom, CancellationToken.None), this.galleryService.getCoreTranslation(extensionToFetchTranslationsFrom, searchLocale)])
.then(([manifest, translation]) => {
const loc = manifest && manifest.contributes && manifest.contributes.localizations && manifest.contributes.localizations.find(x => locale.startsWith(x.languageId.toLowerCase()));
const languageName = loc ? (loc.languageName || locale) : locale;
const languageDisplayName = loc ? (loc.localizedLanguageName || loc.languageName || locale) : locale;
const translationsFromPack: { [key: string]: string } = translation?.contents?.['vs/workbench/contrib/localization/electron-sandbox/minimalTranslations'] ?? {};
const promptMessageKey = extensionToInstall ? 'installAndRestartMessage' : 'showLanguagePackExtensions';
const useEnglish = !translationsFromPack[promptMessageKey];

const translations: { [key: string]: string } = {};
Object.keys(minimumTranslatedStrings).forEach(key => {
if (!translationsFromPack[key] || useEnglish) {
translations[key] = minimumTranslatedStrings[key].replace('{0}', languageName);
} else {
translations[key] = `${translationsFromPack[key].replace('{0}', languageDisplayName)} (${minimumTranslatedStrings[key].replace('{0}', languageName)})`;
const translations: { [key: string]: string } = {};
Object.keys(minimumTranslatedStrings).forEach(key => {
if (!translationsFromPack[key] || useEnglish) {
translations[key] = minimumTranslatedStrings[key].replace('{0}', languageName);
} else {
translations[key] = `${translationsFromPack[key].replace('{0}', languageDisplayName)} (${minimumTranslatedStrings[key].replace('{0}', languageName)})`;
}
});

const logUserReaction = (userReaction: string) => {
/* __GDPR__
"languagePackSuggestion:popup" : {
"owner": "TylerLeonhardt",
"userReaction" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"language": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
});
*/
this.telemetryService.publicLog('languagePackSuggestion:popup', { userReaction, language: locale });
};

const searchAction = {
label: translations['searchMarketplace'],
run: () => {
logUserReaction('search');
this.paneCompositeService.openPaneComposite(EXTENSIONS_VIEWLET_ID, ViewContainerLocation.Sidebar, true)
.then(viewlet => viewlet?.getViewPaneContainer() as IExtensionsViewPaneContainer)
.then(viewlet => {
viewlet.search(`tag:lp-${searchLocale}`);
viewlet.focus();
});
}
};

const logUserReaction = (userReaction: string) => {
/* __GDPR__
"languagePackSuggestion:popup" : {
"owner": "TylerLeonhardt",
"userReaction" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"language": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
}
*/
this.telemetryService.publicLog('languagePackSuggestion:popup', { userReaction, language: locale });
};
const installAndRestartAction = {
label: translations['installAndRestart'],
run: () => {
logUserReaction('installAndRestart');
this.installExtension(extensionToInstall!).then(() => this.hostService.restart());
}
};

const searchAction = {
label: translations['searchMarketplace'],
run: () => {
logUserReaction('search');
this.paneCompositeService.openPaneComposite(EXTENSIONS_VIEWLET_ID, ViewContainerLocation.Sidebar, true)
.then(viewlet => viewlet?.getViewPaneContainer() as IExtensionsViewPaneContainer)
.then(viewlet => {
viewlet.search(`tag:lp-${locale}`);
viewlet.focus();
});
}
};
const promptMessage = translations[promptMessageKey];

const installAndRestartAction = {
label: translations['installAndRestart'],
this.notificationService.prompt(
Severity.Info,
promptMessage,
[extensionToInstall ? installAndRestartAction : searchAction,
{
label: localize('neverAgain', "Don't Show Again"),
isSecondary: true,
run: () => {
logUserReaction('installAndRestart');
this.installExtension(extensionToInstall).then(() => this.hostService.restart());
languagePackSuggestionIgnoreList.push(locale);
this.storageService.store(
LANGUAGEPACK_SUGGESTION_IGNORE_STORAGE_KEY,
JSON.stringify(languagePackSuggestionIgnoreList),
StorageScope.APPLICATION,
StorageTarget.USER
);
logUserReaction('neverShowAgain');
}
};

const promptMessage = translations[promptMessageKey];

this.notificationService.prompt(
Severity.Info,
promptMessage,
[extensionToInstall ? installAndRestartAction : searchAction,
{
label: localize('neverAgain', "Don't Show Again"),
isSecondary: true,
run: () => {
languagePackSuggestionIgnoreList.push(locale);
this.storageService.store(
LANGUAGEPACK_SUGGESTION_IGNORE_STORAGE_KEY,
JSON.stringify(languagePackSuggestionIgnoreList),
StorageScope.APPLICATION,
StorageTarget.USER
);
logUserReaction('neverShowAgain');
}
}],
{
onCancel: () => {
logUserReaction('cancelled');
}
}],
{
onCancel: () => {
logUserReaction('cancelled');
}
);

});
});
}
);
});
});

}

private async isLanguageInstalled(language: string | undefined): Promise<boolean> {
private async isLocaleInstalled(locale: string): Promise<boolean> {
const installed = await this.extensionManagementService.getInstalled();
return installed.some(i => !!(i.manifest
&& i.manifest.contributes
&& i.manifest.contributes.localizations
&& i.manifest.contributes.localizations.length
&& i.manifest.contributes.localizations.some(l => l.languageId.toLowerCase() === language)));
&& i.manifest.contributes.localizations.some(l => locale.startsWith(l.languageId.toLowerCase()))));
}

private installExtension(extension: IGalleryExtension): Promise<void> {
Expand Down