|
1 | 1 | /* |
2 | | - Copyright (c) 2023-2024, Oracle and/or its affiliates. |
| 2 | + Copyright (c) 2023-2025, Oracle and/or its affiliates. |
3 | 3 |
|
4 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | 'use strict'; |
18 | 18 |
|
19 | | -import * as l10nLib from '@vscode/l10n' |
20 | | - |
21 | 19 | import * as vscode from 'vscode'; |
| 20 | +import * as fs from 'fs'; |
22 | 21 | import { extConstants } from './constants'; |
23 | 22 | import { FileUtils } from './utils'; |
| 23 | +import { LOGGER } from './logger'; |
| 24 | +import * as path from 'path'; |
24 | 25 |
|
25 | | -const DEFAULT_LANGAUGE = "en"; |
26 | | -const DEFAULT_BUNDLE_FILE = `l10n/bundle.l10n.${DEFAULT_LANGAUGE}.json`; |
27 | | - |
28 | | -type TranslatorFn = typeof vscode.l10n.t |
| 26 | +const DEFAULT_LANGUAGE = "en"; |
| 27 | +const DEFAULT_BUNDLE_FILE = path.join("l10n", `bundle.l10n.${DEFAULT_LANGUAGE}.json`); |
| 28 | +const _format2Regexp = /{([^}]+)}/g; |
29 | 29 |
|
30 | 30 | export interface l10n { |
31 | 31 | value(key: string, placeholderMap?: Record<string, any>): string |
32 | | - nbLocaleCode():string |
| 32 | + nbLocaleCode(): string |
33 | 33 | } |
34 | 34 |
|
35 | 35 |
|
36 | 36 | class l10Wrapper implements l10n { |
37 | | - private defaultTranslation: TranslatorFn; |
38 | | - |
39 | | - constructor(extensionId: string, defaultBundlePath: string) { |
40 | | - let defaultBundleAbsoluteFsPath = FileUtils.toUri(`${vscode.extensions.getExtension(extensionId)?.extensionPath}/${defaultBundlePath}`).fsPath; |
41 | | - l10nLib.config({ |
42 | | - fsPath: defaultBundleAbsoluteFsPath |
43 | | - }); |
44 | | - this.defaultTranslation = l10nLib.t; |
| 37 | + private defaultl10nMap: any; |
| 38 | + constructor(extensionId: string, defaultBundlePath: string) { |
| 39 | + let extnPath = vscode.extensions.getExtension(extensionId)!!.extensionPath; |
| 40 | + let defaultBundleAbsoluteFsPath = FileUtils.toUri(path.join(extnPath,defaultBundlePath)).fsPath; |
| 41 | + let fileContent: string = ""; |
| 42 | + try { |
| 43 | + fileContent = fs.readFileSync(defaultBundleAbsoluteFsPath, 'utf-8'); |
| 44 | + } catch (err) { |
| 45 | + LOGGER.logAndThrowError("error occured while reading bundle file : ", err); |
| 46 | + } |
| 47 | + try { |
| 48 | + this.defaultl10nMap = JSON.parse(fileContent); |
| 49 | + } catch (err) { |
| 50 | + LOGGER.logAndThrowError("error occured while parsing bundle file : ", err); |
45 | 51 | } |
46 | 52 |
|
47 | | - value(key: string, placeholderMap: Record<string, any>): string { |
48 | | - const valueFromBundle:string = vscode.l10n.bundle ? vscode.l10n.t(key, placeholderMap) : key; |
49 | | - const isPresentInBundle = valueFromBundle !== key; |
50 | | - return isPresentInBundle ? valueFromBundle : this.defaultTranslation(key, placeholderMap); |
| 53 | + } |
| 54 | + |
| 55 | + value(key: string, placeholderMap: Record<string, any>): string { |
| 56 | + const valueFromBundle: string = vscode.l10n.bundle ? vscode.l10n.t(key, placeholderMap) : key; |
| 57 | + const isPresentInBundle = valueFromBundle !== key; |
| 58 | + return isPresentInBundle ? valueFromBundle : this.defaultTranslation(key, placeholderMap); |
| 59 | + } |
| 60 | + defaultTranslation(key: string, placeholderMap: Record<string, any>): string { |
| 61 | + let value = this.defaultl10nMap[key]; |
| 62 | + return value ? this.format(value, placeholderMap) : key; |
| 63 | + } |
| 64 | + nbLocaleCode() { |
| 65 | + const vscodeLanguage = vscode.env.language; |
| 66 | + if (!vscodeLanguage) return DEFAULT_LANGUAGE; |
| 67 | + const localeParts = vscodeLanguage.split(/[-_]/g); |
| 68 | + if (localeParts.length > 1) { |
| 69 | + localeParts[1] = localeParts[1].toUpperCase(); |
51 | 70 | } |
52 | | - nbLocaleCode(){ |
53 | | - const vscodeLanguage = vscode.env.language; |
54 | | - if (!vscodeLanguage) return DEFAULT_LANGAUGE; |
55 | | - const localeParts = vscodeLanguage.split(/[-_]/g); |
56 | | - if (localeParts.length > 1) { |
57 | | - localeParts[1] = localeParts[1].toUpperCase(); |
58 | | - } |
59 | | - var nbFormatLocale = localeParts.join(":"); |
60 | | - return nbFormatLocale; |
| 71 | + var nbFormatLocale = localeParts.join(":"); |
| 72 | + return nbFormatLocale; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + //copied from: |
| 77 | + //https://github.com/microsoft/vscode-l10n/blob/57b5918f3b247a03387432037669e8ae5aff886b/l10n/src/main.ts#L222 |
| 78 | + //original license: MIT |
| 79 | + /* |
| 80 | + Copyright (c) Microsoft Corporation |
| 81 | +
|
| 82 | + All rights reserved. |
| 83 | +
|
| 84 | + MIT License |
| 85 | +
|
| 86 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 87 | + of this software and associated documentation files (the "Software"), to deal |
| 88 | + in the Software without restriction, including without limitation the rights |
| 89 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 90 | + copies of the Software, and to permit persons to whom the Software is |
| 91 | + furnished to do so, subject to the following conditions: |
| 92 | +
|
| 93 | + The above copyright notice and this permission notice shall be included in all |
| 94 | + copies or substantial portions of the Software. |
| 95 | +
|
| 96 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 97 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 98 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 99 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 100 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 101 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 102 | + SOFTWARE |
| 103 | + */ |
| 104 | + format(template: string, values: Record<string, unknown>): string { |
| 105 | + if (!values || Object.keys(values).length === 0) { |
| 106 | + return template; |
61 | 107 | } |
| 108 | + return template.replace(_format2Regexp, (match, group) => (values[group] ?? match) as string); |
| 109 | + } |
62 | 110 | } |
63 | 111 |
|
64 | 112 |
|
|
0 commit comments