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

Translate monaco using default keys #10946

Merged
merged 5 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -24,7 +24,7 @@
*/
import { ConfigurationScope, Extensions, IConfigurationRegistry } from '@theia/monaco-editor-core/esm/vs/platform/configuration/common/configurationRegistry';
import { Registry } from '@theia/monaco-editor-core/esm/vs/platform/registry/common/platform';
import { CommandContribution, CommandRegistry, MessageService } from '@theia/core';
import { CommandContribution, CommandRegistry, MessageService, nls } from '@theia/core';
import { inject, injectable, interfaces } from '@theia/core/shared/inversify';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { WorkspaceService } from '@theia/workspace/lib/browser';
Expand All @@ -49,15 +49,15 @@ function generateContent(properties: string, interfaceEntries: string[]): string
import { isOSX, isWindows, nls } from '@theia/core';
import { PreferenceSchema } from '@theia/core/lib/browser';

/* eslint-disable @typescript-eslint/quotes,max-len,@theia/localization-check,no-null/no-null */
/* eslint-disable @typescript-eslint/quotes,max-len,no-null/no-null */

/**
* Please do not modify this file by hand. It should be generated automatically
* during a Monaco uplift using the command registered by monaco-editor-preference-extractor.ts
* The only manual work required is fixing preferences with type 'array' or 'object'.
*/

export const generatedEditorPreferenceProperties: PreferenceSchema['properties'] = ${properties};
export const editorGeneratedPreferenceProperties: PreferenceSchema['properties'] = ${properties};

export interface GeneratedEditorPreferences {
${interfaceEntries.join('\n ')}
Expand All @@ -83,6 +83,8 @@ export class MonacoEditorPreferenceSchemaExtractor implements CommandContributio
@inject(MessageService) protected readonly messageService: MessageService;
@inject(FileService) protected readonly fileService: FileService;

protected lastPreferenceName: string;
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved

registerCommands(commands: CommandRegistry): void {
commands.registerCommand({ id: 'extract-editor-preference-schema', label: 'Extract Editor preference schema from Monaco' }, {
execute: async () => {
Expand Down Expand Up @@ -157,18 +159,42 @@ export class MonacoEditorPreferenceSchemaExtractor implements CommandContributio
}

protected withLocalization(key: string, value: unknown): unknown {
if (key.startsWith('editor.') || key.startsWith('diffEditor.')) {
this.lastPreferenceName = key;
}
if ((key === 'description' || key === 'markdownDescription') && typeof value === 'string') {
return `nls.localizeByDefault("${value}")`;
if (value.length === 0) {
return value;
}
const defaultKey = nls.getDefaultKey(value);
if (defaultKey) {
msujew marked this conversation as resolved.
Show resolved Hide resolved
return `${deQuoteMarker}nls.localizeByDefault(${deQuoteMarker}"${value}${deQuoteMarker}")${deQuoteMarker}`;
} else {
const localizationKey = `${deQuoteMarker}"theia/editor/${this.lastPreferenceName}${deQuoteMarker}"`;
return `${deQuoteMarker}nls.localize(${localizationKey}, ${deQuoteMarker}"${value}${deQuoteMarker}")${deQuoteMarker}`;
}
}
if ((key === 'enumDescriptions' || key === 'markdownEnumDescriptions') && Array.isArray(value)) {
return value.map(description => `${deQuoteMarker}nls.localizeByDefault("${description}")${deQuoteMarker}`);
return value.map((description, i) => {
if (description.length === 0) {
return description;
}
const defaultKey = nls.getDefaultKey(description);
if (defaultKey) {
return `${deQuoteMarker}nls.localizeByDefault(${deQuoteMarker}"${description}${deQuoteMarker}")${deQuoteMarker}`;
} else {
const localizationKey = `${deQuoteMarker}"theia/editor/${this.lastPreferenceName}${i}${deQuoteMarker}"`;
return `${deQuoteMarker}nls.localize(${localizationKey}, ${deQuoteMarker}"${description}${deQuoteMarker}")${deQuoteMarker}`;
}
});
}
return value;
}

protected deQuoteCodeSnippets(stringification: string): string {
return stringification
.replace(new RegExp(`${deQuoteMarker}"|"${deQuoteMarker}`, 'g'), '')
.replace(new RegExp(`${deQuoteMarker}"|"${deQuoteMarker}|${deQuoteMarker}\\\\`, 'g'), '')
.replace(new RegExp(`\\\\"${deQuoteMarker}`, 'g'), '"')
.replace(/\\\\'/g, "\\'");
}

Expand Down
30 changes: 15 additions & 15 deletions packages/core/src/common/nls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ export namespace nls {
* Automatically localizes a text if that text also exists in the vscode repository.
*/
export function localizeByDefault(defaultValue: string, ...args: FormatType[]): string {
const key = getDefaultKey(defaultValue);
if (key) {
return localize(key, defaultValue, ...args);
}
return Localization.format(defaultValue, args);
}

export function getDefaultKey(defaultValue: string): string {
if (localization) {
if (!keyProvider) {
keyProvider = new LocalizationKeyProvider();
}
const key = keyProvider.get(defaultValue);
const key = getDefaultKey(defaultValue);
if (key) {
return key;
return localize(key, defaultValue, ...args);
} else {
console.warn(`Could not find translation key for default value: "${defaultValue}"`);
}
}
return Localization.format(defaultValue, args);
}

export function getDefaultKey(defaultValue: string): string {
if (!keyProvider) {
keyProvider = new LocalizationKeyProvider();
}
const key = keyProvider.get(defaultValue);
if (key) {
return key;
}
return '';
}

Expand All @@ -71,7 +71,7 @@ class LocalizationKeyProvider {
private data = this.buildData();

get(defaultValue: string): string | undefined {
return this.data.get(defaultValue);
return this.data.get(Localization.normalize(defaultValue.toLowerCase()));
}

/**
Expand All @@ -89,7 +89,7 @@ class LocalizationKeyProvider {
for (const [fileKey, messageBundle] of Object.entries(messages)) {
const keyBundle = keys[fileKey];
for (let i = 0; i < messageBundle.length; i++) {
const message = Localization.normalize(messageBundle[i]);
const message = Localization.normalize(messageBundle[i]).toLowerCase();
const key = keyBundle[i];
const localizationKey = this.buildKey(typeof key === 'string' ? key : key.key, fileKey);
data.set(message, localizationKey);
Expand Down
Loading