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/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;