import fs from 'fs' ; import path from 'path' ; import { env } from 'vscode' ; export default class i18n { static language = env . language . toLocaleLowerCase ( ) ; static messages : Record < string , string > = { } ; static init ( extensionPath ) { let name = this . language === 'en' ? 'package.nls.json' : `package.nls.${ this . language }.json` ; if ( ! fs . existsSync ( path . join ( extensionPath , name ) ) ) name = 'package.nls.json' ; this . messages = JSON . parse ( fs . readFileSync ( path . join ( extensionPath , name ) , 'utf-8' ) ) ; } static format ( str , args ) { return str . replace ( /{(\d+)}/g , ( match , number ) => { return typeof args [ number ] !== 'undefined' ? args [ number ] . toString ( ) : match ; } ) ; } static t ( key , ... args ) { let text = this . messages [ key ] || '' ; if ( args && args . length ) text = this . format ( text , args ) ; return text ; } }