@i18n-micro/astro 1.3.25 → 2.0.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.
Files changed (75) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +8 -64
  3. package/dist/auto-middleware.d.ts +1 -0
  4. package/dist/client/core.d.cts +86 -0
  5. package/dist/client/core.d.ts +71 -8
  6. package/dist/client/index.d.cts +2 -6
  7. package/dist/client/index.d.ts +2 -6
  8. package/dist/client/index.js +1 -6
  9. package/dist/client/preact.d.ts +12 -23
  10. package/dist/client/preact.js +13 -37
  11. package/dist/client/react.d.ts +14 -21
  12. package/dist/client/react.js +12 -37
  13. package/dist/client/svelte.d.ts +14 -14
  14. package/dist/client/svelte.js +17 -47
  15. package/dist/client/vue.d.ts +15 -15
  16. package/dist/client/vue.js +37 -2
  17. package/dist/composer.d.cts +16 -0
  18. package/dist/composer.d.ts +10 -30
  19. package/dist/core-CgrgdmGy.cjs +1 -0
  20. package/dist/core-DTAcn2yF.js +72 -0
  21. package/dist/env.d.ts +8 -2
  22. package/dist/index.cjs +11 -3
  23. package/dist/index.d.cts +10 -13
  24. package/dist/index.d.ts +10 -13
  25. package/dist/index.mjs +171 -482
  26. package/dist/integration.d.cts +26 -0
  27. package/dist/integration.d.ts +14 -15
  28. package/dist/middleware-BT3OA5YL.cjs +1 -0
  29. package/dist/middleware-Ch8E0iV_.js +257 -0
  30. package/dist/middleware.d.cts +34 -0
  31. package/dist/middleware.d.ts +17 -8
  32. package/dist/middleware.js +23 -0
  33. package/dist/node.d.ts +26 -0
  34. package/dist/node.js +22 -0
  35. package/dist/request.d.ts +69 -0
  36. package/dist/router/adapter.d.cts +9 -0
  37. package/dist/router/adapter.d.ts +5 -4
  38. package/dist/router/types.d.cts +66 -0
  39. package/dist/router/types.d.ts +3 -2
  40. package/dist/utils.d.cts +112 -0
  41. package/dist/utils.d.ts +39 -35
  42. package/dist/virtual.d.ts +21 -0
  43. package/package.json +28 -15
  44. package/src/auto-middleware.ts +26 -0
  45. package/src/client/core.ts +124 -59
  46. package/src/client/index.ts +2 -11
  47. package/src/client/preact.tsx +12 -127
  48. package/src/client/react.tsx +11 -123
  49. package/src/client/svelte.ts +6 -69
  50. package/src/client/vue.ts +22 -77
  51. package/src/components/i18n-link.astro +2 -59
  52. package/src/components/i18n-switcher.astro +46 -149
  53. package/src/components/i18n-t.astro +6 -54
  54. package/src/composer.ts +25 -115
  55. package/src/env.d.ts +7 -0
  56. package/src/index.ts +1 -6
  57. package/src/integration.ts +113 -52
  58. package/src/middleware.ts +174 -120
  59. package/src/node.ts +51 -0
  60. package/src/request.ts +177 -0
  61. package/src/router/adapter.ts +47 -149
  62. package/src/router/types.ts +3 -2
  63. package/src/utils.ts +149 -323
  64. package/src/virtual.d.ts +21 -0
  65. package/dist/components/index.d.cts +0 -0
  66. package/dist/components/index.d.ts +0 -0
  67. package/dist/core-DwkpOV-H.cjs +0 -1
  68. package/dist/core-VSnkGRWR.js +0 -28
  69. package/dist/load-translations.d.ts +0 -43
  70. package/dist/routing-context.d.ts +0 -4
  71. package/dist/vue-BNiEwZr2.cjs +0 -5
  72. package/dist/vue-CWHjIOiS.js +0 -1724
  73. package/src/components/index.ts +0 -11
  74. package/src/load-translations.ts +0 -106
  75. package/src/routing-context.ts +0 -23
@@ -1,11 +0,0 @@
1
- // Astro components are imported directly from their file paths
2
- // This file is for documentation purposes only
3
-
4
- // Astro components can be imported in two ways:
5
- // 1. Direct import (recommended):
6
- // import I18nT from '@i18n-micro/astro/i18n-t'
7
- // import I18nLink from '@i18n-micro/astro/i18n-link'
8
- // import I18nSwitcher from '@i18n-micro/astro/i18n-switcher'
9
- // import I18nGroup from '@i18n-micro/astro/i18n-group'
10
- // 2. Legacy import (still supported):
11
- // import I18nT from '@i18n-micro/astro/components/i18n-t.astro'
@@ -1,106 +0,0 @@
1
- import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
2
- import { join, relative, resolve, sep } from 'node:path'
3
- import type { Translations } from '@i18n-micro/types'
4
- import { mergeRouteTranslationsWithRoot, storeLoadedTranslationFile, type TranslationFileBuckets } from '@i18n-micro/utils/parse-path'
5
-
6
- /**
7
- * WARNING: Node.js-only functions
8
- *
9
- * The functions in this file use Node.js filesystem APIs (node:fs) and will NOT work
10
- * in Edge runtime environments (Cloudflare Workers, Vercel Edge, Deno Deploy, etc.).
11
- *
12
- * If you import this module in an Edge environment, the bundler will fail at build time
13
- * because node:fs is not available. This is the intended behavior (Fail Fast).
14
- *
15
- * For Edge-compatible translation loading, use import.meta.glob in your middleware:
16
- * const translations = import.meta.glob('/src/locales/*.json', { eager: true })
17
- */
18
-
19
- /**
20
- * Load translations from a directory structure
21
- * Supports both flat structure (en.json, fr.json) and nested structure (pages/home/en.json)
22
- */
23
- export interface LoadTranslationsOptions {
24
- translationDir: string
25
- rootDir?: string
26
- disablePageLocales?: boolean
27
- }
28
-
29
- export interface LoadedTranslations extends TranslationFileBuckets<Translations> {}
30
-
31
- function walkTranslationFiles(dir: string, onFile: (fullPath: string) => void): void {
32
- if (!existsSync(dir)) return
33
-
34
- for (const entry of readdirSync(dir)) {
35
- const fullPath = join(dir, entry)
36
- const stat = statSync(fullPath)
37
-
38
- if (stat.isDirectory()) {
39
- walkTranslationFiles(fullPath, onFile)
40
- continue
41
- }
42
-
43
- if (entry.endsWith('.json')) {
44
- onFile(fullPath)
45
- }
46
- }
47
- }
48
-
49
- /**
50
- * Load all translations from a directory
51
- *
52
- * WARNING: Node.js-only - This function uses node:fs and will NOT work in Edge runtime.
53
- * If imported in Edge environment, bundler will fail at build time (Fail Fast).
54
- * Use import.meta.glob for Edge-compatible loading.
55
- */
56
- export function loadTranslationsFromDir(options: LoadTranslationsOptions): LoadedTranslations {
57
- const { translationDir, rootDir = process.cwd(), disablePageLocales = false } = options
58
- const fullTranslationDir = resolve(rootDir, translationDir)
59
-
60
- if (!existsSync(fullTranslationDir)) {
61
- console.warn(`[i18n] Translation directory not found: ${fullTranslationDir}`)
62
- return { root: {}, routes: {} }
63
- }
64
-
65
- const buckets: LoadedTranslations = { root: {}, routes: {} }
66
-
67
- walkTranslationFiles(fullTranslationDir, (fullPath) => {
68
- const relativePath = relative(fullTranslationDir, fullPath).split(sep).join('/')
69
-
70
- try {
71
- const content = readFileSync(fullPath, 'utf-8')
72
- const translations = JSON.parse(content) as Translations
73
- storeLoadedTranslationFile(buckets, relativePath, translations, disablePageLocales)
74
- } catch (error) {
75
- console.error(`[i18n] Failed to load translation file: ${fullPath}`, error)
76
- }
77
- })
78
-
79
- return buckets
80
- }
81
-
82
- /**
83
- * Load translations and add them to an AstroI18n instance
84
- *
85
- * WARNING: Node.js-only - This function uses node:fs and will NOT work in Edge runtime.
86
- * For Edge environments, use import.meta.glob to load translations at build time.
87
- */
88
- export function loadTranslationsIntoI18n(
89
- i18n: {
90
- addTranslations: (locale: string, translations: Translations, merge?: boolean) => void
91
- addRouteTranslations: (locale: string, routeName: string, translations: Translations, merge?: boolean) => void
92
- },
93
- options: LoadTranslationsOptions,
94
- ): void {
95
- const { root, routes } = loadTranslationsFromDir(options)
96
-
97
- for (const [locale, translations] of Object.entries(root)) {
98
- i18n.addTranslations(locale, translations, false)
99
- }
100
-
101
- for (const [routeName, routeTranslations] of Object.entries(routes)) {
102
- for (const [locale, translations] of Object.entries(routeTranslations)) {
103
- i18n.addRouteTranslations(locale, routeName, mergeRouteTranslationsWithRoot(root[locale], translations), false)
104
- }
105
- }
106
- }
@@ -1,23 +0,0 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks'
2
- import type { I18nRoutingStrategy } from './router/types'
3
-
4
- /** Integration-time default strategy (immutable after setup). */
5
- let installedRoutingStrategy: I18nRoutingStrategy | null = null
6
-
7
- /** Per-request strategy override for concurrent SSR. */
8
- const requestRoutingStrategy = new AsyncLocalStorage<I18nRoutingStrategy | null>()
9
-
10
- export function getGlobalRoutingStrategy(): I18nRoutingStrategy | null {
11
- const store = requestRoutingStrategy.getStore()
12
- // Distinguish an explicit per-request override (including `null`, i.e. routing
13
- // disabled for this request) from "no request context" (`undefined`).
14
- return store !== undefined ? store : installedRoutingStrategy
15
- }
16
-
17
- export function setGlobalRoutingStrategy(strategy: I18nRoutingStrategy | null): void {
18
- installedRoutingStrategy = strategy
19
- }
20
-
21
- export function runWithRoutingStrategy<T>(strategy: I18nRoutingStrategy | null, fn: () => T): T {
22
- return requestRoutingStrategy.run(strategy, fn)
23
- }