Skip to content

Commit a37bf49

Browse files
committed
remove l10n lib dependency
1 parent 0950275 commit a37bf49

File tree

5 files changed

+81
-36
lines changed

5 files changed

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

63100

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)