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' ;
2322
2423const DEFAULT_LANGAUGE = "en" ;
2524const DEFAULT_BUNDLE_FILE = `l10n/bundle.l10n.${ DEFAULT_LANGAUGE } .json` ;
25+ const _format2Regexp = / { ( [ ^ } ] + ) } / g;
2626
2727type TranslatorFn = typeof vscode . l10n . t
2828
@@ -33,21 +33,22 @@ export interface l10n {
3333
3434
3535class 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
0 commit comments