From: Haoqun Jiang Date: Wed, 2 Apr 2025 06:31:59 +0000 (+0800) Subject: fix: handle windows import paths; fixes #727 X-Git-Tag: v3.16.1~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e3db3de1ca6e89ea04591b9f063853a9fce8b84b;p=thirdparty%2Fvuejs%2Fcreate-vue.git fix: handle windows import paths; fixes #727 --- 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 }