@i18n-micro/astro 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/core.d.ts +1 -1
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.js +9 -9
- package/dist/client/preact.d.ts +1 -1
- package/dist/client/preact.js +36 -21
- package/dist/client/react.d.ts +1 -1
- package/dist/client/react.js +35 -20
- package/dist/client/svelte.d.ts +1 -1
- package/dist/client/svelte.js +8 -14
- package/dist/client/vue.d.ts +1 -1
- package/dist/client/vue.js +11 -17
- package/dist/composer.d.ts +5 -20
- package/dist/env.d.ts +0 -2
- package/dist/index.cjs +4 -4
- package/dist/index.d.ts +11 -11
- package/dist/index.mjs +308 -343
- package/dist/integration.d.ts +2 -2
- package/dist/middleware.d.ts +3 -3
- package/dist/router/adapter.d.ts +1 -1
- package/dist/utils.d.ts +2 -2
- package/package.json +11 -11
- package/src/client/core.ts +5 -11
- package/src/client/index.ts +6 -8
- package/src/client/preact.tsx +74 -61
- package/src/client/react.tsx +73 -61
- package/src/client/svelte.ts +9 -25
- package/src/client/vue.ts +7 -23
- package/src/components/i18n-link.astro +3 -3
- package/src/components/i18n-switcher.astro +3 -3
- package/src/components/i18n-t.astro +3 -13
- package/src/composer.ts +18 -95
- package/src/env.d.ts +0 -2
- package/src/index.ts +35 -42
- package/src/integration.ts +8 -13
- package/src/load-translations.ts +11 -13
- package/src/middleware.ts +15 -25
- package/src/router/adapter.ts +7 -25
- package/src/routing.ts +3 -17
- package/src/utils.ts +18 -23
package/src/utils.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Params, Locale, CleanTranslation, TranslationKey, Translations } from '@i18n-micro/types'
|
|
1
|
+
import type { CleanTranslation, Locale, Params, TranslationKey, Translations } from '@i18n-micro/types'
|
|
3
2
|
import type { AstroGlobal } from 'astro'
|
|
3
|
+
import type { AstroI18n } from './composer'
|
|
4
4
|
import type { I18nRoutingStrategy } from './router/types'
|
|
5
5
|
import './env.d'
|
|
6
6
|
|
|
@@ -12,8 +12,7 @@ export function getI18n(astro: AstroGlobal): AstroI18n {
|
|
|
12
12
|
if (!i18n) {
|
|
13
13
|
throw new Error('i18n instance not found. Make sure i18n middleware is configured.')
|
|
14
14
|
}
|
|
15
|
-
//
|
|
16
|
-
// @ts-ignore
|
|
15
|
+
// @ts-ignore private property mismatch between src and dist types
|
|
17
16
|
return i18n
|
|
18
17
|
}
|
|
19
18
|
|
|
@@ -42,8 +41,6 @@ export function getLocales(astro: AstroGlobal): Locale[] {
|
|
|
42
41
|
* Get routing strategy from Astro locals
|
|
43
42
|
*/
|
|
44
43
|
function getRoutingStrategy(astro: AstroGlobal): I18nRoutingStrategy | null {
|
|
45
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
46
|
-
// @ts-ignore
|
|
47
44
|
return (astro.locals.routingStrategy as I18nRoutingStrategy | undefined) || null
|
|
48
45
|
}
|
|
49
46
|
|
|
@@ -56,7 +53,7 @@ export function useI18n(astro: AstroGlobal) {
|
|
|
56
53
|
const locale = getLocale(astro)
|
|
57
54
|
const defaultLocale = getDefaultLocale(astro)
|
|
58
55
|
const locales = getLocales(astro)
|
|
59
|
-
const localeCodes = locales.map(l => l.code)
|
|
56
|
+
const localeCodes = locales.map((l) => l.code)
|
|
60
57
|
const routingStrategy = getRoutingStrategy(astro)
|
|
61
58
|
|
|
62
59
|
return {
|
|
@@ -115,7 +112,7 @@ export function useI18n(astro: AstroGlobal) {
|
|
|
115
112
|
// Fallback: check first segment
|
|
116
113
|
const segments = targetPath.split('/').filter(Boolean)
|
|
117
114
|
const firstSegment = segments[0]
|
|
118
|
-
return
|
|
115
|
+
return firstSegment && localeCodes.includes(firstSegment) ? firstSegment : defaultLocale
|
|
119
116
|
},
|
|
120
117
|
|
|
121
118
|
// Path utilities
|
|
@@ -217,16 +214,12 @@ export interface LocaleHeadResult {
|
|
|
217
214
|
}
|
|
218
215
|
|
|
219
216
|
export function useLocaleHead(astro: AstroGlobal, options: LocaleHeadOptions = {}): LocaleHeadResult {
|
|
220
|
-
const {
|
|
221
|
-
baseUrl = '/',
|
|
222
|
-
addDirAttribute = true,
|
|
223
|
-
addSeoAttributes = true,
|
|
224
|
-
} = options
|
|
217
|
+
const { baseUrl = '/', addDirAttribute = true, addSeoAttributes = true } = options
|
|
225
218
|
|
|
226
219
|
const locale = getLocale(astro)
|
|
227
220
|
const defaultLocale = getDefaultLocale(astro)
|
|
228
221
|
const locales = getLocales(astro)
|
|
229
|
-
const currentLocaleObj = locales.find(l => l.code === locale)
|
|
222
|
+
const currentLocaleObj = locales.find((l) => l.code === locale)
|
|
230
223
|
|
|
231
224
|
if (!currentLocaleObj) {
|
|
232
225
|
return { htmlAttrs: {}, link: [], meta: [] }
|
|
@@ -257,7 +250,7 @@ export function useLocaleHead(astro: AstroGlobal, options: LocaleHeadOptions = {
|
|
|
257
250
|
|
|
258
251
|
// Get routing strategy
|
|
259
252
|
const routingStrategy = getRoutingStrategy(astro)
|
|
260
|
-
const allLocaleCodes = locales.map(l => l.code)
|
|
253
|
+
const allLocaleCodes = locales.map((l) => l.code)
|
|
261
254
|
|
|
262
255
|
// Alternate languages
|
|
263
256
|
for (const loc of locales) {
|
|
@@ -266,8 +259,7 @@ export function useLocaleHead(astro: AstroGlobal, options: LocaleHeadOptions = {
|
|
|
266
259
|
let alternatePath = astro.url.pathname
|
|
267
260
|
if (routingStrategy?.switchLocalePath) {
|
|
268
261
|
alternatePath = routingStrategy.switchLocalePath(astro.url.pathname, loc.code, allLocaleCodes, defaultLocale)
|
|
269
|
-
}
|
|
270
|
-
else {
|
|
262
|
+
} else {
|
|
271
263
|
// Fallback: basic locale switching
|
|
272
264
|
const segments = astro.url.pathname.split('/').filter(Boolean)
|
|
273
265
|
const firstSegment = segments[0]
|
|
@@ -336,12 +328,16 @@ function setNestedValue(obj: Translations, key: string, value: unknown): void {
|
|
|
336
328
|
const parts = key.split('.')
|
|
337
329
|
let current: Translations = obj
|
|
338
330
|
for (let i = 0; i < parts.length - 1; i++) {
|
|
339
|
-
|
|
340
|
-
|
|
331
|
+
const part = parts[i]!
|
|
332
|
+
if (!current[part]) {
|
|
333
|
+
current[part] = {}
|
|
341
334
|
}
|
|
342
|
-
current = current[
|
|
335
|
+
current = current[part] as Translations
|
|
336
|
+
}
|
|
337
|
+
const last = parts[parts.length - 1]
|
|
338
|
+
if (last !== undefined) {
|
|
339
|
+
current[last] = value
|
|
343
340
|
}
|
|
344
|
-
current[parts[parts.length - 1]] = value
|
|
345
341
|
}
|
|
346
342
|
|
|
347
343
|
/**
|
|
@@ -378,8 +374,7 @@ export function getI18nProps(astro: AstroGlobal, keys?: string[]): I18nClientPro
|
|
|
378
374
|
if (Object.keys(extracted).length > 0) {
|
|
379
375
|
translations[currentRoute] = extracted
|
|
380
376
|
}
|
|
381
|
-
}
|
|
382
|
-
else {
|
|
377
|
+
} else {
|
|
383
378
|
// Если ключи не указаны, берем route-specific переводы
|
|
384
379
|
// Используем публичный метод getRouteTranslations для безопасного доступа
|
|
385
380
|
const routeTrans = i18n.getRouteTranslations(locale, currentRoute)
|