File tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change 11import * as fs from 'node:fs'
22import * as path from 'node:path'
3+ import { pathToFileURL } from 'node:url'
34
45interface LanguageItem {
56 hint ?: string
@@ -105,15 +106,20 @@ function getLocale() {
105106 return linkLocale ( shellLocale . split ( '.' ) [ 0 ] . replace ( '_' , '-' ) )
106107}
107108
109+ async function loadLanguageFile ( filePath : string ) : Promise < Language > {
110+ return ( await import ( pathToFileURL ( filePath ) . toString ( ) , { with : { type : 'json' } } ) ) . default
111+ }
112+
108113export default async function getLanguage ( localesRoot : string ) {
109114 const locale = getLocale ( )
110115
111116 const languageFilePath = path . resolve ( localesRoot , `${ locale } .json` )
112- const doesLanguageExist = fs . existsSync ( languageFilePath )
117+ const fallbackPath = path . resolve ( localesRoot , 'en-US.json' )
113118
119+ const doesLanguageExist = fs . existsSync ( languageFilePath )
114120 const lang : Language = doesLanguageExist
115- ? ( await import ( languageFilePath , { with : { type : 'json' } } ) ) . default
116- : ( await import ( path . resolve ( localesRoot , 'en-US.json' ) , { with : { type : 'json' } } ) ) . default
121+ ? await loadLanguageFile ( languageFilePath )
122+ : await loadLanguageFile ( fallbackPath )
117123
118124 return lang
119125}
You can’t perform that action at this time.
0 commit comments