Skip to content

Commit b98724e

Browse files
committed
remove l10n lib dependency
1 parent 5fc6e25 commit b98724e

File tree

5 files changed

+79
-37
lines changed

5 files changed

+79
-37
lines changed

THIRD_PARTY_LICENSES.txt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9453,28 +9453,33 @@ Dependency: VS Code tooling for localizing Visual Studio Code extensions
94539453

94549454
------------------ START OF DEPENDENCY LICENCE --------------------
94559455
- @vscode/l10n
9456+
Accessible at: vscode/src/localiser.ts
9457+
Modified from source at: https://github.com/microsoft/vscode-l10n/blob/57b5918f3b247a03387432037669e8ae5aff886b/l10n/src/main.ts#L222
9458+
License: MIT License
94569459

9457-
MIT License
9460+
Copyright (c) Microsoft Corporation
94589461

9459-
Copyright (c) Microsoft Corporation.
9462+
All rights reserved.
94609463

9461-
Permission is hereby granted, free of charge, to any person obtaining a copy
9462-
of this software and associated documentation files (the "Software"), to deal
9463-
in the Software without restriction, including without limitation the rights
9464-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9465-
copies of the Software, and to permit persons to whom the Software is
9466-
furnished to do so, subject to the following conditions:
9464+
MIT License
9465+
9466+
Permission is hereby granted, free of charge, to any person obtaining a copy
9467+
of this software and associated documentation files (the "Software"), to deal
9468+
in the Software without restriction, including without limitation the rights
9469+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9470+
copies of the Software, and to permit persons to whom the Software is
9471+
furnished to do so, subject to the following conditions:
94679472

9468-
The above copyright notice and this permission notice shall be included in all
9469-
copies or substantial portions of the Software.
9473+
The above copyright notice and this permission notice shall be included in all
9474+
copies or substantial portions of the Software.
94709475

9471-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9472-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9473-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
9474-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9475-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9476-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
9477-
SOFTWARE
9476+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9477+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9478+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
9479+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9480+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9481+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
9482+
SOFTWARE
94789483

94799484
------------------ END OF DEPENDENCY LICENCE --------------------
94809485

vscode/package-lock.json

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vscode/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,6 @@
937937
},
938938
"dependencies": {
939939
"@vscode/debugadapter": "^1.68.0",
940-
"@vscode/l10n": "^0.0.18",
941940
"ajv": "^8.17.1",
942941
"jsonc-parser": "3.3.1",
943942
"vscode-languageclient": "^9.0.1"

vscode/src/localiser.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
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
import { FileUtils } from './utils';
2423

2524
const DEFAULT_LANGAUGE = "en";
2625
const DEFAULT_BUNDLE_FILE = `l10n/bundle.l10n.${DEFAULT_LANGAUGE}.json`;
27-
28-
type TranslatorFn = typeof vscode.l10n.t
26+
const _format2Regexp = /{([^}]+)}/g;
2927

3028
export interface l10n {
3129
value(key: string, placeholderMap?: Record<string, any>): string
@@ -34,21 +32,21 @@ export interface l10n {
3432

3533

3634
class l10Wrapper implements l10n {
37-
private defaultTranslation: TranslatorFn;
38-
35+
private defaultl10nContent:any;
3936
constructor(extensionId: string, defaultBundlePath: string) {
4037
let defaultBundleAbsoluteFsPath = FileUtils.toUri(`${vscode.extensions.getExtension(extensionId)?.extensionPath}/${defaultBundlePath}`).fsPath;
41-
l10nLib.config({
42-
fsPath: defaultBundleAbsoluteFsPath
43-
});
44-
this.defaultTranslation = l10nLib.t;
38+
this.defaultl10nContent = JSON.parse(fs.readFileSync(defaultBundleAbsoluteFsPath,'utf-8'));
4539
}
4640

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

6498

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@ 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
36+
window.showInformationMessage("Starting Localisation tests");
37+
test("localiser lib tests", async () => {
38+
assert.equal(
39+
l10n.value("jdk.downloader.message.downloadCompleted",{"jdkVersion":"25","osType":"macOS","jdkType":"OpenJDK"}),
40+
"OpenJDK 25 for macOS download completed!"
41+
)
42+
assert.equal(
43+
l10n.value("jdk.downloader.error_message.installationCleanup"),
44+
"Error while installation cleanup"
45+
)
46+
})
47+
// Check the consistency of the keys and value templates across the bundle files for the supported languages
3748
test("Consistency of keys across bundle.l10n.lang.json files for supported languages", async () => {
3849
const extension = extensions.getExtension(EXTENSION_NAME);
3950
assert(extension);

0 commit comments

Comments
 (0)