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 1 commit
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
Prev Previous commit
Next Next commit
Improve the recommender system
  • Loading branch information
rzhao271 committed Nov 7, 2022
commit c3e368b258c7aba444ec4910881cdeb145ed480f
10 changes: 9 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,16 +573,24 @@ async function resolveNlsConfiguration() {
// The ternary and ts-ignore can both be removed once Electron
// officially adopts the getSystemLocale API.
// Ref https://github.com/electron/electron/pull/35697
/**
* @type string
*/
let appLocale = (product.quality === 'insider' || product.quality === 'exploration') ?
// @ts-ignore API not yet available in the official Electron
app.getSystemLocale() : app.getLocale();
rzhao271 marked this conversation as resolved.
Show resolved Hide resolved
if (!appLocale) {
nlsConfiguration = { locale: 'en', availableLanguages: {} };
} else {

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

if (!appLocale.startsWith('pt') && !appLocale.startsWith('zh')) {
// Trim off the country code to help with the recommender.
// If - isn't in the string, the following code leaves appLocale unchanged.
appLocale = appLocale.split('-')[0];
}

const { getNLSConfiguration } = require('./vs/base/node/languagePacks');
nlsConfiguration = await getNLSConfiguration(product.commit, userDataPath, metaDataFile, appLocale);
if (!nlsConfiguration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export class LocalizationWorkbenchContribution extends Disposable implements IWo
}

private checkAndInstall(): void {
// The locale can contain extraneous country codes, such as fr-CA
const language = platform.language;
const locale = platform.locale;
const languagePackSuggestionIgnoreList = <string[]>JSON.parse(this.storageService.get(LANGUAGEPACK_SUGGESTION_IGNORE_STORAGE_KEY, StorageScope.APPLICATION, '[]'));
Expand All @@ -97,7 +96,7 @@ export class LocalizationWorkbenchContribution extends Disposable implements IWo
if (!language || !locale || locale === 'en' || locale.indexOf('en-') === 0) {
return;
}
if (locale.startsWith(language) || languagePackSuggestionIgnoreList.includes(locale)) {
if (language === locale || languagePackSuggestionIgnoreList.includes(locale)) {
return;
}

Expand All @@ -112,16 +111,16 @@ export class LocalizationWorkbenchContribution extends Disposable implements IWo
return;
}

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];
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;
}

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.find(x => locale.startsWith(x.languageId.toLowerCase()));
const loc = manifest && manifest.contributes && manifest.contributes.localizations && manifest.contributes.localizations.find(x => x.languageId.toLowerCase() === locale);
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'] ?? {};
Expand Down Expand Up @@ -165,7 +164,7 @@ export class LocalizationWorkbenchContribution extends Disposable implements IWo
label: translations['installAndRestart'],
run: () => {
logUserReaction('installAndRestart');
this.installExtension(extensionToInstall).then(() => this.hostService.restart());
this.installExtension(extensionToInstall!).then(() => this.hostService.restart());
rzhao271 marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand Down