1616
1717'use strict' ;
1818
19- import * as l10nLib from '@vscode/l10n'
20-
2119import * as vscode from 'vscode' ;
20+ import * as fs from 'fs' ;
2221import { extConstants } from './constants' ;
2322import { FileUtils } from './utils' ;
2423
2524const DEFAULT_LANGAUGE = "en" ;
2625const DEFAULT_BUNDLE_FILE = `l10n/bundle.l10n.${ DEFAULT_LANGAUGE } .json` ;
27-
28- type TranslatorFn = typeof vscode . l10n . t
26+ const _format2Regexp = / { ( [ ^ } ] + ) } / g;
2927
3028export interface l10n {
3129 value ( key : string , placeholderMap ?: Record < string , any > ) : string
@@ -34,21 +32,21 @@ export interface l10n {
3432
3533
3634class 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
0 commit comments