From e3db3de1ca6e89ea04591b9f063853a9fce8b84b Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Wed, 2 Apr 2025 14:31:59 +0800 Subject: [PATCH] fix: handle windows import paths; fixes #727 --- utils/getLanguage.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/utils/getLanguage.ts b/utils/getLanguage.ts index 71de0c7e..632efce2 100644 --- a/utils/getLanguage.ts +++ b/utils/getLanguage.ts @@ -1,5 +1,6 @@ import * as fs from 'node:fs' import * as path from 'node:path' +import { pathToFileURL } from 'node:url' interface LanguageItem { hint?: string @@ -105,15 +106,20 @@ function getLocale() { return linkLocale(shellLocale.split('.')[0].replace('_', '-')) } +async function loadLanguageFile(filePath: string): Promise { + return (await import(pathToFileURL(filePath).toString(), { with: { type: 'json' } })).default +} + export default async function getLanguage(localesRoot: string) { const locale = getLocale() const languageFilePath = path.resolve(localesRoot, `${locale}.json`) - const doesLanguageExist = fs.existsSync(languageFilePath) + const fallbackPath = path.resolve(localesRoot, 'en-US.json') + const doesLanguageExist = fs.existsSync(languageFilePath) const lang: Language = doesLanguageExist - ? (await import(languageFilePath, { with: { type: 'json' } })).default - : (await import(path.resolve(localesRoot, 'en-US.json'), { with: { type: 'json' } })).default + ? await loadLanguageFile(languageFilePath) + : await loadLanguageFile(fallbackPath) return lang } -- 2.39.5