mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-18 10:44:08 +00:00
Rename resources/scripts-v2 to resources/scripts and drop @v2 alias
Now that the legacy v1 frontend (commit 064bdf53) is gone, the v2 directory is the only frontend and the v2 suffix is just noise. Renames resources/scripts-v2 to resources/scripts via git mv (so git records the move as renames, preserving blame and log --follow), then bulk-rewrites the 152 files that imported via @v2/... to use @/scripts/... instead. The existing @ alias (resources/) covers the new path with no extra config needed.
Drops the now-unused @v2 alias from vite.config.js and points the laravel-vite-plugin entry at resources/scripts/main.ts. Updates the only blade reference (resources/views/app.blade.php) to match. The package.json test script (eslint ./resources/scripts) automatically targets the right place after the rename without any edit.
Verified: npm run build exits clean and the Vite warning lines now reference resources/scripts/plugins/i18n.ts, confirming every import resolved through the new path. git log --follow on any moved file walks back through its scripts-v2 history.
This commit is contained in:
123
resources/scripts/plugins/i18n.ts
Normal file
123
resources/scripts/plugins/i18n.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import { createI18n } from 'vue-i18n'
|
||||
import type { I18n, I18nOptions } from 'vue-i18n'
|
||||
import en from '../../../lang/en.json'
|
||||
|
||||
/**
|
||||
* Locale-to-filename mapping for language files whose filename does
|
||||
* not match the locale code exactly.
|
||||
*/
|
||||
const LOCALE_FILE_MAP: Record<string, string> = {
|
||||
zh_CN: 'zh-cn',
|
||||
pt_BR: 'pt-br',
|
||||
}
|
||||
|
||||
/** Tracks which languages have already been loaded. */
|
||||
const loadedLanguages = new Set<string>(['en'])
|
||||
|
||||
/** In-memory cache of loaded message objects keyed by locale. */
|
||||
const languageCache = new Map<string, Record<string, unknown>>()
|
||||
|
||||
/**
|
||||
* Dynamically import a language JSON file for a given locale.
|
||||
*/
|
||||
async function loadLanguageMessages(
|
||||
locale: string
|
||||
): Promise<Record<string, unknown>> {
|
||||
if (languageCache.has(locale)) {
|
||||
return languageCache.get(locale)!
|
||||
}
|
||||
|
||||
// English is already statically imported — no dynamic import needed
|
||||
if (locale === 'en') {
|
||||
const messages = en as unknown as Record<string, unknown>
|
||||
languageCache.set('en', messages)
|
||||
return messages
|
||||
}
|
||||
|
||||
const fileName = LOCALE_FILE_MAP[locale] ?? locale
|
||||
|
||||
try {
|
||||
const mod: { default: Record<string, unknown> } = await import(
|
||||
`../../../lang/${fileName}.json`
|
||||
)
|
||||
const messages = mod.default ?? mod
|
||||
languageCache.set(locale, messages)
|
||||
loadedLanguages.add(locale)
|
||||
return messages
|
||||
} catch (error: unknown) {
|
||||
console.warn(`Failed to load language: ${locale}`, error)
|
||||
|
||||
// Fall back to English
|
||||
if (locale !== 'en' && !languageCache.has('en')) {
|
||||
try {
|
||||
const fallback: { default: Record<string, unknown> } = await import(
|
||||
'../../../lang/en.json'
|
||||
)
|
||||
const fallbackMessages = fallback.default ?? fallback
|
||||
languageCache.set('en', fallbackMessages)
|
||||
return fallbackMessages
|
||||
} catch (fallbackError: unknown) {
|
||||
console.error('Failed to load fallback language (en)', fallbackError)
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
return languageCache.get('en') ?? {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a language and activate it on the given i18n instance.
|
||||
*/
|
||||
export async function setI18nLanguage(
|
||||
i18n: I18n<Record<string, unknown>, Record<string, unknown>, Record<string, unknown>, string, false>,
|
||||
locale: string
|
||||
): Promise<void> {
|
||||
if (!loadedLanguages.has(locale)) {
|
||||
const messages = await loadLanguageMessages(locale)
|
||||
i18n.global.setLocaleMessage(locale, messages)
|
||||
}
|
||||
|
||||
i18n.global.locale.value = locale
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a language has already been loaded.
|
||||
*/
|
||||
export function isLanguageLoaded(locale: string): boolean {
|
||||
return loadedLanguages.has(locale)
|
||||
}
|
||||
|
||||
/** Type alias for the i18n instance created by this module. */
|
||||
export type AppI18n = I18n<
|
||||
Record<string, unknown>,
|
||||
Record<string, unknown>,
|
||||
Record<string, unknown>,
|
||||
string,
|
||||
false
|
||||
>
|
||||
|
||||
/**
|
||||
* Create and return the vue-i18n plugin instance.
|
||||
*
|
||||
* Only the English bundle is included synchronously; all other
|
||||
* languages are loaded on demand via `setI18nLanguage`.
|
||||
*/
|
||||
export function createAppI18n(
|
||||
extraMessages?: Record<string, Record<string, unknown>>
|
||||
): AppI18n {
|
||||
const messages: Record<string, Record<string, unknown>> = {
|
||||
en: en as unknown as Record<string, unknown>,
|
||||
...extraMessages,
|
||||
}
|
||||
|
||||
const options: I18nOptions = {
|
||||
legacy: false,
|
||||
locale: 'en',
|
||||
fallbackLocale: 'en',
|
||||
globalInjection: true,
|
||||
messages,
|
||||
}
|
||||
|
||||
return createI18n(options) as AppI18n
|
||||
}
|
||||
6
resources/scripts/plugins/index.ts
Normal file
6
resources/scripts/plugins/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export { createAppI18n, setI18nLanguage, isLanguageLoaded } from './i18n'
|
||||
export type { AppI18n } from './i18n'
|
||||
|
||||
export { createAppPinia } from './pinia'
|
||||
|
||||
export { installTooltipDirective } from './tooltip'
|
||||
9
resources/scripts/plugins/pinia.ts
Normal file
9
resources/scripts/plugins/pinia.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { createPinia } from 'pinia'
|
||||
import type { Pinia } from 'pinia'
|
||||
|
||||
/**
|
||||
* Create and return the Pinia store instance.
|
||||
*/
|
||||
export function createAppPinia(): Pinia {
|
||||
return createPinia()
|
||||
}
|
||||
9
resources/scripts/plugins/tooltip.ts
Normal file
9
resources/scripts/plugins/tooltip.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { VTooltip } from 'v-tooltip'
|
||||
import type { App, Directive } from 'vue'
|
||||
|
||||
/**
|
||||
* Install the v-tooltip directive on the given Vue app instance.
|
||||
*/
|
||||
export function installTooltipDirective(app: App): void {
|
||||
app.directive('tooltip', VTooltip as Directive)
|
||||
}
|
||||
Reference in New Issue
Block a user