File tree 1 file changed +9
-3
lines changed
1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change 1
1
import * as fs from 'node:fs'
2
2
import * as path from 'node:path'
3
+ import { pathToFileURL } from 'node:url'
3
4
4
5
interface LanguageItem {
5
6
hint ?: string
@@ -105,15 +106,20 @@ function getLocale() {
105
106
return linkLocale ( shellLocale . split ( '.' ) [ 0 ] . replace ( '_' , '-' ) )
106
107
}
107
108
109
+ async function loadLanguageFile ( filePath : string ) : Promise < Language > {
110
+ return ( await import ( pathToFileURL ( filePath ) . toString ( ) , { with : { type : 'json' } } ) ) . default
111
+ }
112
+
108
113
export default async function getLanguage ( localesRoot : string ) {
109
114
const locale = getLocale ( )
110
115
111
116
const languageFilePath = path . resolve ( localesRoot , `${ locale } .json` )
112
- const doesLanguageExist = fs . existsSync ( languageFilePath )
117
+ const fallbackPath = path . resolve ( localesRoot , 'en-US.json' )
113
118
119
+ const doesLanguageExist = fs . existsSync ( languageFilePath )
114
120
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 )
117
123
118
124
return lang
119
125
}
You can’t perform that action at this time.
0 commit comments