@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/src/utils.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { AstroI18n } from './composer'
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
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
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 (firstSegment && localeCodes.includes(firstSegment)) ? firstSegment : defaultLocale
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
- if (!current[parts[i]]) {
340
- current[parts[i]] = {}
331
+ const part = parts[i]!
332
+ if (!current[part]) {
333
+ current[part] = {}
341
334
  }
342
- current = current[parts[i]] as Translations
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)