diff --git a/l10n/bundle.l10n.json b/l10n/bundle.l10n.json index c33936f61..a4921cc98 100644 --- a/l10n/bundle.l10n.json +++ b/l10n/bundle.l10n.json @@ -52,5 +52,5 @@ "flowStyleMapForbidden": "Flow style mapping is forbidden", "flowStyleSeqForbidden": "Flow style sequence is forbidden", "unUsedAnchor": "Unused anchor \"{0}\"", - "unUsedAlias": "Unresolved alias \"{0}\"" + "unUsedAlias": "Unresolved alias \"{0}\"" } diff --git a/package.json b/package.json index deb67110d..26d27ea6b 100644 --- a/package.json +++ b/package.json @@ -72,9 +72,9 @@ "clean": "rimraf out/server && rimraf lib", "compile": "tsc -p .", "watch": "tsc --watch -p .", - "test": "mocha --require ts-node/register --require ./scripts/setup-l10n.mjs --timeout 5000 --ui bdd ./test/*.test.ts", + "test": "mocha --require ts-node/register --timeout 5000 --ui bdd ./test/*.test.ts", "coverage": "nyc mocha --require ts-node/register --timeout 5000 --require source-map-support/register --recursive --ui bdd ./test/*.test.ts", - "coveralls": "nyc --reporter=lcov --reporter=text mocha --timeout 5000 --require ts-node/register --require ./scripts/setup-l10n.mjs --require source-map-support/register --recursive --ui bdd ./test/*.test.ts", + "coveralls": "nyc --reporter=lcov --reporter=text mocha --timeout 5000 --require ts-node/register --require source-map-support/register --recursive --ui bdd ./test/*.test.ts", "lint": "eslint --max-warnings 0 -c .eslintrc.js --ext .ts src test", "lint-ci": "eslint --max-warnings 0 -c .eslintrc.js -f @microsoft/eslint-formatter-sarif -o eslint-result.sarif --ext .ts src test", "prettier-fix": "yarn prettier --write .", diff --git a/scripts/setup-l10n.mjs b/scripts/setup-l10n.mjs deleted file mode 100644 index 68a2523ec..000000000 --- a/scripts/setup-l10n.mjs +++ /dev/null @@ -1,31 +0,0 @@ -import * as l10n from '@vscode/l10n'; -import { URI } from 'vscode-uri'; -import { fileURLToPath } from 'url'; -import * as path from 'path'; - -class Setupl10n { - async load() { - const __filename = fileURLToPath(import.meta.url); - const __dirname = path.dirname(__filename); - const l10nPath = path.join(__dirname, '../l10n'); - - process.env.VSCODE_NLS_CONFIG = JSON.stringify({ - locale: 'en', - _languagePackSupport: true, - }); - - await l10n.config({ - uri: URI.file(path.join(l10nPath, 'bundle.l10n.json')).toString(), - }); - } -} - -// Run immediately -new Setupl10n() - .load() - .then(() => { - console.log('✅ l10n configured'); - }) - .catch((err) => { - console.error('❌ l10n setup failed:', err); - }); diff --git a/src/languageservice/parser/jsonParser07.ts b/src/languageservice/parser/jsonParser07.ts index a16d18a27..4b9401a45 100644 --- a/src/languageservice/parser/jsonParser07.ts +++ b/src/languageservice/parser/jsonParser07.ts @@ -1075,7 +1075,7 @@ function validate( validationResult.problems.push({ location: { offset: node.offset, length: node.length }, severity: DiagnosticSeverity.Warning, - message: schema.patternErrorMessage || schema.errorMessage || format.errorMessage, + message: schema.patternErrorMessage || schema.errorMessage || l10n.t(format.errorMessage), source: getSchemaSource(schema, originalSchema), schemaUri: getSchemaUri(schema, originalSchema), }); diff --git a/src/languageservice/services/yamlSchemaService.ts b/src/languageservice/services/yamlSchemaService.ts index a9ad24408..f5b5b702c 100644 --- a/src/languageservice/services/yamlSchemaService.ts +++ b/src/languageservice/services/yamlSchemaService.ts @@ -650,12 +650,7 @@ export class YAMLSchemaService extends JSONSchemaService { return requestService(schemaUri).then( (content) => { if (!content) { - const errorMessage = l10n.t( - 'json.schema.nocontent', - "Unable to load schema from '{0}': No content. {1}", - toDisplayString(schemaUri), - unresolvedJsonSchema.errors - ); + const errorMessage = l10n.t('json.schema.nocontent', toDisplayString(schemaUri), unresolvedJsonSchema.errors); return new UnresolvedSchema({}, [errorMessage]); } @@ -663,12 +658,7 @@ export class YAMLSchemaService extends JSONSchemaService { const schemaContent = parse(content); return new UnresolvedSchema(schemaContent, []); } catch (yamlError) { - const errorMessage = l10n.t( - 'json.schema.invalidFormat', - "Unable to parse content from '{0}': {1}.", - toDisplayString(schemaUri), - yamlError - ); + const errorMessage = l10n.t('json.schema.invalidFormat', toDisplayString(schemaUri), yamlError); return new UnresolvedSchema({}, [errorMessage]); } }, diff --git a/src/yamlServerInit.ts b/src/yamlServerInit.ts index 3d2c66d1b..9a8f82f4f 100644 --- a/src/yamlServerInit.ts +++ b/src/yamlServerInit.ts @@ -20,6 +20,7 @@ import { Telemetry } from './languageservice/telemetry'; import { registerCommands } from './languageservice/services/yamlCommands'; import * as l10n from '@vscode/l10n'; import * as path from 'path'; +import * as fs from 'fs'; export class YAMLServerInit { languageService: LanguageService; @@ -41,19 +42,6 @@ export class YAMLServerInit { * The server receives the root path(s) of the workspace and the client capabilities. */ this.connection.onInitialize(async (params: InitializeParams): Promise => { - const l10nPath: string = params.initializationOptions?.l10nPath; - const locale: string = params.locale; - if (l10nPath) { - const bundleFile = locale === 'en' ? `bundle.l10n.json` : `bundle.l10n.${locale}.json`; - const baseBundleFile = path.join(l10nPath, bundleFile); - process.env.VSCODE_NLS_CONFIG = JSON.stringify({ - locale: locale, - _languagePackSupport: true, - }); - await l10n.config({ - uri: URI.file(baseBundleFile).toString(), - }); - } return this.connectionInitialized(params); }); @@ -69,6 +57,25 @@ export class YAMLServerInit { }); } + public async setupl10nBundle(params: InitializeParams): Promise { + const __dirname = path.dirname(__filename); + const l10nPath: string = params.initializationOptions?.l10nPath || path.join(__dirname, '../l10n'); + const locale: string = params.locale || 'en'; + if (l10nPath) { + const bundleFile = !fs.existsSync(path.join(l10nPath, `bundle.l10n.${locale}.json`)) + ? `bundle.l10n.json` + : `bundle.l10n.${locale}.json`; + const baseBundleFile = path.join(l10nPath, bundleFile); + process.env.VSCODE_NLS_CONFIG = JSON.stringify({ + locale, + _languagePackSupport: true, + }); + await l10n.config({ + uri: URI.file(baseBundleFile).toString(), + }); + } + } + // public for test setup async connectionInitialized(params: InitializeParams): Promise { this.yamlSettings.capabilities = params.capabilities; @@ -111,7 +118,7 @@ export class YAMLServerInit { ); this.registerHandlers(); registerCommands(commandExecutor, this.connection); - + await this.setupl10nBundle(params); return { capabilities: { textDocumentSync: TextDocumentSyncKind.Incremental, diff --git a/test/bundlel10n.test.ts b/test/bundlel10n.test.ts new file mode 100644 index 000000000..fc89f8b21 --- /dev/null +++ b/test/bundlel10n.test.ts @@ -0,0 +1,71 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Red Hat. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +import * as assert from 'assert'; +import { SettingsState } from '../src/yamlSettings'; +import { TestCustomSchemaProvider, testFileSystem } from './utils/testHelper'; +import { createConnection, Connection } from 'vscode-languageserver/node'; +import { schemaRequestHandler, workspaceContext } from '../src/languageservice/services/schemaRequestHandler'; +import { TestTelemetry } from './utils/testsTypes'; +import { YAMLServerInit } from '../src/yamlServerInit'; +import * as l10n from '@vscode/l10n'; + +describe('Bundle l10n Test', () => { + let serverInit: YAMLServerInit; + + before(() => { + const yamlSettings = new SettingsState(); + process.argv.push('--node-ipc'); + const connection = createConnection(); + const schemaRequestHandlerWrapper = (connection: Connection, uri: string): Promise => { + const testSchemaProvider = TestCustomSchemaProvider.instance(); + const testSchema = testSchemaProvider.getContentForSchema(uri); + if (testSchema) { + return Promise.resolve(testSchema); + } + return schemaRequestHandler( + connection, + uri, + yamlSettings.workspaceFolders, + yamlSettings.workspaceRoot, + yamlSettings.useVSCodeContentRequest, + testFileSystem + ); + }; + const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection); + const telemetry = new TestTelemetry(connection); + serverInit = new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry); + }); + + after(async () => { + await serverInit.setupl10nBundle({ + locale: 'en', + processId: 0, + rootUri: '', + capabilities: undefined, + }); + }); + + describe('l10n bundle test', function () { + it('check french locale', async () => { + await serverInit.setupl10nBundle({ + locale: 'fr', + processId: 0, + rootUri: '', + capabilities: undefined, + }); + assert.equal(l10n.t('Default Value'), 'Valeur par défaut'); + }); + + it('un configured locale should return in english', async () => { + await serverInit.setupl10nBundle({ + locale: 'pt-br', + processId: 0, + rootUri: '', + capabilities: undefined, + }); + assert.equal(l10n.t('Default Value'), 'Default value'); + }); + }); +}); diff --git a/test/utils/testHelper.ts b/test/utils/testHelper.ts index 71a9e4dd4..93bc72119 100644 --- a/test/utils/testHelper.ts +++ b/test/utils/testHelper.ts @@ -82,11 +82,16 @@ export function setupLanguageService(languageSettings: LanguageSettings): TestLa const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection); const telemetry = new TestTelemetry(connection); const serverInit = new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry); + const __dirname = path.resolve(path.dirname(__filename), '..'); serverInit.connectionInitialized({ processId: null, capabilities: ClientCapabilities.LATEST as LSPClientCapabilities, rootUri: null, workspaceFolders: null, + initializationOptions: { + l10nPath: path.join(__dirname, '../l10n'), + }, + locale: 'en', }); const languageService = serverInit.languageService; const validationHandler = serverInit.validationHandler; diff --git a/yarn.lock b/yarn.lock index 0defc5476..70ac9a37d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1364,9 +1364,9 @@ ecc-jsbn@~0.1.1: safer-buffer "^2.1.0" electron-to-chromium@^1.5.173: - version "1.5.176" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.5.176.tgz#f4bbbd2c0a9a6a2a527c884eacc18244fa79dd88" - integrity sha512-2nDK9orkm7M9ZZkjO3PjbEd3VUulQLyg5T9O3enJdFvUg46Hzd4DUvTvAuEgbdHYXyFsiG4A5sO9IzToMH1cDg== + version "1.5.177" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.5.177.tgz#db730d8254959184e65320a3a0b7edcd29c54f60" + integrity sha512-7EH2G59nLsEMj97fpDuvVcYi6lwTcM1xuWw3PssD8xzboAW7zj7iB3COEEEATUfjLHrs5uKBLQT03V/8URx06g== emoji-regex@^8.0.0: version "8.0.0" @@ -3155,9 +3155,9 @@ prettier-linter-helpers@^1.0.0: fast-diff "^1.1.2" prettier@^3.5.0: - version "3.6.1" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.6.1.tgz#cc3bce21c09a477b1e987b76ce9663925d86ae44" - integrity sha512-5xGWRa90Sp2+x1dQtNpIpeOQpTDBs9cZDmA/qs2vDNN2i18PdapqY7CmBeyLlMuGqXJRIOPaCaVZTLNQRWUH/A== + version "3.6.2" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.6.2.tgz#ccda02a1003ebbb2bfda6f83a074978f608b9393" + integrity sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ== process-on-spawn@^1.0.0: version "1.1.0"