Skip to content

Commit e227193

Browse files
author
Balyam muralidhar narendra kumar
committed
remove l10n lib dependency
1 parent 0950275 commit e227193

File tree

2 files changed

+114
-60
lines changed

2 files changed

+114
-60
lines changed

vscode/src/localiser.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
'use strict';
1818

19-
import * as l10nLib from '@vscode/l10n'
20-
2119
import * as vscode from 'vscode';
20+
import * as fs from 'fs';
2221
import { extConstants } from './constants';
2322

2423
const DEFAULT_LANGAUGE = "en";
2524
const DEFAULT_BUNDLE_FILE = `l10n/bundle.l10n.${DEFAULT_LANGAUGE}.json`;
25+
const _format2Regexp = /{([^}]+)}/g;
2626

2727
type TranslatorFn = typeof vscode.l10n.t
2828

@@ -33,21 +33,22 @@ export interface l10n {
3333

3434

3535
class l10Wrapper implements l10n {
36-
private defaultTranslation: TranslatorFn;
37-
36+
private defaultl10nContent:any;
3837
constructor(extensionId: string, defaultBundlePath: string) {
39-
let defaultBundleAbsoluteFsPath = vscode.Uri.file(`${vscode.extensions.getExtension(extensionId)?.extensionPath}/${defaultBundlePath}`).fsPath
40-
l10nLib.config({
41-
fsPath: defaultBundleAbsoluteFsPath
42-
});
43-
this.defaultTranslation = l10nLib.t;
38+
let defaultBundleAbsoluteFsPath = vscode.Uri.file(`${vscode.extensions.getExtension(extensionId)?.extensionPath}/${defaultBundlePath}`).fsPath;
39+
this.defaultl10nContent = JSON.parse(fs.readFileSync(defaultBundleAbsoluteFsPath,'utf-8'));
4440
}
4541

4642
value(key: string, placeholderMap: Record<string, any>): string {
43+
4744
const valueFromBundle:string = vscode.l10n.bundle ? vscode.l10n.t(key, placeholderMap) : key;
4845
const isPresentInBundle = valueFromBundle !== key;
4946
return isPresentInBundle ? valueFromBundle : this.defaultTranslation(key, placeholderMap);
5047
}
48+
defaultTranslation(key: string, placeholderMap:Record<string, any>):string{
49+
let value = this.defaultl10nContent[key];
50+
return value?this.format(value,placeholderMap):key;
51+
}
5152
nbLocaleCode(){
5253
const vscodeLanguage = vscode.env.language;
5354
if (!vscodeLanguage) return DEFAULT_LANGAUGE;
@@ -58,6 +59,44 @@ class l10Wrapper implements l10n {
5859
var nbFormatLocale = localeParts.join(":");
5960
return nbFormatLocale;
6061
}
62+
63+
64+
65+
//copied from:
66+
//https://github.com/microsoft/vscode-l10n/blob/57b5918f3b247a03387432037669e8ae5aff886b/l10n/src/main.ts#L222
67+
//original license: MIT
68+
/**
69+
70+
Copyright (c) Microsoft Corporation
71+
72+
All rights reserved.
73+
74+
MIT License
75+
76+
Permission is hereby granted, free of charge, to any person obtaining a copy
77+
of this software and associated documentation files (the "Software"), to deal
78+
in the Software without restriction, including without limitation the rights
79+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
80+
copies of the Software, and to permit persons to whom the Software is
81+
furnished to do so, subject to the following conditions:
82+
83+
The above copyright notice and this permission notice shall be included in all
84+
copies or substantial portions of the Software.
85+
86+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
87+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
88+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
89+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
90+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
91+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
92+
SOFTWARE
93+
*/
94+
format(template: string, values: Record<string, unknown>): string {
95+
if (!values || Object.keys(values).length === 0) {
96+
return template;
97+
}
98+
return template.replace(_format2Regexp, (match, group) => (values[group] ?? match) as string);
99+
}
61100
}
62101

63102

vscode/src/test/integration/suite/localisation/extension.test.ts

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,65 +27,80 @@ import * as path from 'path';
2727
import * as vscode from 'vscode';
2828

2929
import { extensions, window } from 'vscode';
30+
import {l10n} from '../../../../localiser'
3031
import { DEFAULT_BUNDLE_FILE_NAME, DEFAULT_PACKAGE_FILE_NAME, EXTENSION_NAME, SUPPORTED_LANGUAGES } from '../../constants';
3132
import { checkCommandsLocalisation, checkConfigurationLocalisation, checkDebuggersLocalisation, checkL10nUsageInFiles, checkViewsLocalisation, getKeysFromJSON, matchKeys, matchValuesTemplate } from '../../testutils';
3233

3334

3435
suite("Extension localisation tests", function () {
35-
window.showInformationMessage("Starting Localisation tests");
36-
// Check the consistency of the keys and value templates across the bundle files for the supported languages
37-
test("Consistency of keys across bundle.l10n.lang.json files for supported languages", async () => {
38-
const extension = extensions.getExtension(EXTENSION_NAME);
39-
assert(extension);
40-
const enBundlePath = path.join(extension.extensionPath, "l10n", DEFAULT_BUNDLE_FILE_NAME);
41-
assert.ok(fs.existsSync(enBundlePath), `${DEFAULT_BUNDLE_FILE_NAME} doesn't exists`);
42-
for (const lang of SUPPORTED_LANGUAGES) {
43-
const langBundlePath = path.join(extension.extensionPath, "l10n", `bundle.l10n.${lang}.json`);
44-
assert.ok(fs.existsSync(langBundlePath), `bundle.l10n.${lang}.json doesn't exists`);
45-
assert.ok(matchKeys(enBundlePath, langBundlePath), `Keys of ${DEFAULT_BUNDLE_FILE_NAME} and bundle.l10n.${lang}.json don't match`);
46-
assert.ok(matchValuesTemplate(enBundlePath, langBundlePath), `Value templates don't match for of the keys of ${DEFAULT_BUNDLE_FILE_NAME} and bundle.l10n.${lang}.json `);
47-
}
48-
});
36+
var lang = vscode.env.language;
37+
const val = l10n.value("jdk.extension.runConfig.default.label");
38+
window.showInformationMessage("Starting Localisation tests");
39+
test("locale tests", async () => {
40+
assert.equal(
41+
l10n.value("jdk.downloader.message.downloadCompleted",{"jdkVersion":"25","osType":"macOS","jdkType":"OpenJDK"}),
42+
"OpenJDK 25 for macOS download completed!"
43+
)
44+
assert.equal(
45+
l10n.value("jdk.downloader.error_message.installationCleanup"),
46+
"Error while installation cleanup"
47+
)
48+
})
4949

50-
test("Consistency of keys across package.nls.lang.json files for supported languages", async () => {
51-
const extension = extensions.getExtension(EXTENSION_NAME);
52-
assert(extension);
53-
const enPackagePath = path.join(extension.extensionPath, DEFAULT_PACKAGE_FILE_NAME);
54-
assert.ok(fs.existsSync(enPackagePath), `${DEFAULT_PACKAGE_FILE_NAME} doesn't exists`);
55-
for (const lang of SUPPORTED_LANGUAGES) {
56-
const langPackagePath = path.join(extension.extensionPath, `package.nls.${lang}.json`);
57-
assert.ok(fs.existsSync(langPackagePath), `package.nls.${lang}.json doesn't exists`);
58-
assert.ok(matchKeys(enPackagePath, langPackagePath), `Keys of ${DEFAULT_PACKAGE_FILE_NAME} and package.nls.${lang}.json don't match`);
59-
}
60-
});
50+
window.showInformationMessage("Starting Localisation tests");
51+
// Check the consistency of the keys and value templates across the bundle files for the supported languages
52+
test("Consistency of keys across bundle.l10n.lang.json files for supported languages", async () => {
53+
const extension = extensions.getExtension(EXTENSION_NAME);
54+
assert(extension);
55+
const enBundlePath = path.join(extension.extensionPath, "l10n", DEFAULT_BUNDLE_FILE_NAME);
56+
assert.ok(fs.existsSync(enBundlePath), `${DEFAULT_BUNDLE_FILE_NAME} doesn't exists`);
57+
for (const lang of SUPPORTED_LANGUAGES) {
58+
const langBundlePath = path.join(extension.extensionPath, "l10n", `bundle.l10n.${lang}.json`);
59+
assert.ok(fs.existsSync(langBundlePath), `bundle.l10n.${lang}.json doesn't exists`);
60+
assert.ok(matchKeys(enBundlePath, langBundlePath), `Keys of ${DEFAULT_BUNDLE_FILE_NAME} and bundle.l10n.${lang}.json don't match`);
61+
assert.ok(matchValuesTemplate(enBundlePath, langBundlePath), `Value templates don't match for of the keys of ${DEFAULT_BUNDLE_FILE_NAME} and bundle.l10n.${lang}.json `);
62+
}
63+
});
6164

62-
// Check localisable fields being appropriately localised for the contributes defined in package.json
63-
test("Localisable fields in package.json localised properly ", async () => {
64-
const extension = extensions.getExtension(EXTENSION_NAME);
65-
assert(extension);
66-
const packagePath = path.join(extension.extensionPath, "package.json");
67-
assert.ok(fs.existsSync(packagePath), "package.json doesn't exists");
68-
const enPackagePath = path.join(extension.extensionPath, DEFAULT_PACKAGE_FILE_NAME);
69-
assert.ok(fs.existsSync(enPackagePath), `${DEFAULT_PACKAGE_FILE_NAME} doesn't exists`);
70-
const validKeys: Set<string> = getKeysFromJSON(enPackagePath);
71-
const packageObj = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
72-
const contributes = packageObj.contributes
73-
assert.ok(checkCommandsLocalisation(contributes.commands, validKeys), "Error some commands not localized");
74-
assert.ok(checkViewsLocalisation(contributes.views, validKeys), "Error some views is not localized");
75-
assert.ok(checkDebuggersLocalisation(contributes.debuggers, validKeys), "Error some debugger labels not localized");
76-
assert.ok(checkConfigurationLocalisation(contributes.configuration, validKeys), "Error some configuration labels not localized");
77-
});
65+
test("Consistency of keys across package.nls.lang.json files for supported languages", async () => {
66+
const extension = extensions.getExtension(EXTENSION_NAME);
67+
assert(extension);
68+
const enPackagePath = path.join(extension.extensionPath, DEFAULT_PACKAGE_FILE_NAME);
69+
assert.ok(fs.existsSync(enPackagePath), `${DEFAULT_PACKAGE_FILE_NAME} doesn't exists`);
70+
for (const lang of SUPPORTED_LANGUAGES) {
71+
const langPackagePath = path.join(extension.extensionPath, `package.nls.${lang}.json`);
72+
assert.ok(fs.existsSync(langPackagePath), `package.nls.${lang}.json doesn't exists`);
73+
assert.ok(matchKeys(enPackagePath, langPackagePath), `Keys of ${DEFAULT_PACKAGE_FILE_NAME} and package.nls.${lang}.json don't match`);
74+
}
75+
});
7876

77+
// Check localisable fields being appropriately localised for the contributes defined in package.json
78+
test("Localisable fields in package.json localised properly ", async () => {
79+
const extension = extensions.getExtension(EXTENSION_NAME);
80+
assert(extension);
81+
const packagePath = path.join(extension.extensionPath, "package.json");
82+
assert.ok(fs.existsSync(packagePath), "package.json doesn't exists");
83+
const enPackagePath = path.join(extension.extensionPath, DEFAULT_PACKAGE_FILE_NAME);
84+
assert.ok(fs.existsSync(enPackagePath), `${DEFAULT_PACKAGE_FILE_NAME} doesn't exists`);
85+
const validKeys: Set<string> = getKeysFromJSON(enPackagePath);
86+
const packageObj = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
87+
const contributes = packageObj.contributes
88+
assert.ok(checkCommandsLocalisation(contributes.commands, validKeys), "Error some commands not localized");
89+
assert.ok(checkViewsLocalisation(contributes.views, validKeys), "Error some views is not localized");
90+
assert.ok(checkDebuggersLocalisation(contributes.debuggers, validKeys), "Error some debugger labels not localized");
91+
assert.ok(checkConfigurationLocalisation(contributes.configuration, validKeys), "Error some configuration labels not localized");
92+
});
7993

80-
// Check if l10n.value is called with a valid key and the placeholder map has all the keys as required in the string template
81-
test("Proper usage of l10n.value for localisation in the ts/js code files", async () => {
82-
const ignoredDirEntriesNames = new Set(["test"]); // Names of folders,files( .js only),subfolders within the out directory which are not to be checked
83-
const extension = vscode.extensions.getExtension(EXTENSION_NAME);
84-
assert(extension, "extension not found");
85-
const enBundlePath = path.join(extension.extensionPath, "l10n", DEFAULT_BUNDLE_FILE_NAME);
86-
assert(enBundlePath, `${DEFAULT_BUNDLE_FILE_NAME} not found`);
87-
const validKeyValues = JSON.parse(fs.readFileSync(enBundlePath, 'utf8'));
88-
assert(checkL10nUsageInFiles(path.join(extension.extensionPath, "out"), ignoredDirEntriesNames, validKeyValues) === 0, "Some files have invalid localisation keys used. Check the logs or error messages");
89-
});
94+
95+
// Check if l10n.value is called with a valid key and the placeholder map has all the keys as required in the string template
96+
test("Proper usage of l10n.value for localisation in the ts/js code files", async () => {
97+
const ignoredDirEntriesNames = new Set(["test"]); // Names of folders,files( .js only),subfolders within the out directory which are not to be checked
98+
const extension = vscode.extensions.getExtension(EXTENSION_NAME);
99+
assert(extension, "extension not found");
100+
const enBundlePath = path.join(extension.extensionPath, "l10n", DEFAULT_BUNDLE_FILE_NAME);
101+
assert(enBundlePath, `${DEFAULT_BUNDLE_FILE_NAME} not found`);
102+
const validKeyValues = JSON.parse(fs.readFileSync(enBundlePath, 'utf8'));
103+
assert(checkL10nUsageInFiles(path.join(extension.extensionPath, "out"), ignoredDirEntriesNames, validKeyValues) === 0, "Some files have invalid localisation keys used. Check the logs or error messages");
104+
});
90105

91106
});

0 commit comments

Comments
 (0)