@gravito/cosmos 1.0.0-alpha.3 → 1.0.0-alpha.5

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.
@@ -0,0 +1,46 @@
1
+ # @gravito/cosmos
2
+
3
+ > Gravito 的輕量 i18n 模組,提供 JSON 檔案式在地化。
4
+
5
+ ## 安裝
6
+
7
+ ```bash
8
+ bun add @gravito/cosmos
9
+ ```
10
+
11
+ ## 快速開始
12
+
13
+ ```typescript
14
+ import { PlanetCore } from 'gravito-core'
15
+ import { OrbitI18n } from '@gravito/cosmos'
16
+
17
+ const core = new PlanetCore()
18
+
19
+ core.boot({
20
+ orbits: [OrbitI18n],
21
+ config: {
22
+ i18n: {
23
+ defaultLocale: 'en',
24
+ fallbackLocale: 'en',
25
+ path: './lang'
26
+ }
27
+ }
28
+ })
29
+ ```
30
+
31
+ 建立 `./lang/en.json`:
32
+
33
+ ```json
34
+ {
35
+ "welcome": "Welcome, {name}!"
36
+ }
37
+ ```
38
+
39
+ 在路由中使用:
40
+
41
+ ```typescript
42
+ app.get('/', (c) => {
43
+ const t = c.get('t')
44
+ return c.text(t('welcome', { name: 'Carl' }))
45
+ })
46
+ ```
package/dist/index.js CHANGED
@@ -106,7 +106,7 @@ class I18nManager {
106
106
  return value;
107
107
  }
108
108
  }
109
- var localeMiddleware2 = (i18nManager) => {
109
+ var localeMiddleware = (i18nManager) => {
110
110
  return async (c, next) => {
111
111
  let locale = c.req.param("locale") || c.req.query("lang");
112
112
  if (!locale) {
@@ -158,7 +158,7 @@ class I18nOrbit {
158
158
  }
159
159
  }
160
160
  export {
161
- localeMiddleware2 as localeMiddleware,
161
+ localeMiddleware,
162
162
  loadTranslations,
163
163
  I18nOrbit,
164
164
  I18nManager,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravito/cosmos",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.0-alpha.5",
4
4
  "description": "Internationalization orbit for Gravito framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -17,7 +17,7 @@
17
17
  "author": "Carl Lee <carllee0520@gmail.com>",
18
18
  "license": "MIT",
19
19
  "peerDependencies": {
20
- "gravito-core": "1.0.0-beta.2",
20
+ "gravito-core": "1.0.0-beta.4",
21
21
  "hono": "^4.0.0"
22
22
  },
23
23
  "devDependencies": {
@@ -25,6 +25,12 @@ export interface I18nService {
25
25
  export class I18nInstance implements I18nService {
26
26
  private _locale: string
27
27
 
28
+ /**
29
+ * Create a new I18nInstance.
30
+ *
31
+ * @param manager - The I18nManager instance.
32
+ * @param initialLocale - The initial locale for this instance.
33
+ */
28
34
  constructor(
29
35
  private manager: I18nManager,
30
36
  initialLocale: string
@@ -40,24 +46,53 @@ export class I18nInstance implements I18nService {
40
46
  this.setLocale(value)
41
47
  }
42
48
 
49
+ /**
50
+ * Set the current locale.
51
+ *
52
+ * @param locale - The locale to set.
53
+ */
43
54
  setLocale(locale: string) {
44
55
  if (this.manager.getConfig().supportedLocales.includes(locale)) {
45
56
  this._locale = locale
46
57
  }
47
58
  }
48
59
 
60
+ /**
61
+ * Get the current locale.
62
+ *
63
+ * @returns The current locale string.
64
+ */
49
65
  getLocale(): string {
50
66
  return this._locale
51
67
  }
52
68
 
69
+ /**
70
+ * Translate a key.
71
+ *
72
+ * @param key - The translation key (e.g., 'messages.welcome').
73
+ * @param replacements - Optional replacements for parameters in the translation string.
74
+ * @returns The translated string, or the key if not found.
75
+ */
53
76
  t(key: string, replacements?: Record<string, string | number>): string {
54
77
  return this.manager.translate(this._locale, key, replacements)
55
78
  }
56
79
 
80
+ /**
81
+ * Check if a translation key exists.
82
+ *
83
+ * @param key - The translation key to check.
84
+ * @returns True if the key exists, false otherwise.
85
+ */
57
86
  has(key: string): boolean {
58
87
  return this.t(key) !== key
59
88
  }
60
89
 
90
+ /**
91
+ * Clone the current instance with a potentially new locale.
92
+ *
93
+ * @param locale - Optional new locale for the cloned instance.
94
+ * @returns A new I18nInstance.
95
+ */
61
96
  clone(locale?: string): I18nService {
62
97
  return new I18nInstance(this.manager, locale || this._locale)
63
98
  }
@@ -72,6 +107,11 @@ export class I18nManager implements I18nService {
72
107
  // Default instance for global usage (e.g. CLI or background jobs)
73
108
  private globalInstance: I18nInstance
74
109
 
110
+ /**
111
+ * Create a new I18nManager.
112
+ *
113
+ * @param config - The I18n configuration.
114
+ */
75
115
  constructor(private config: I18nConfig) {
76
116
  if (config.translations) {
77
117
  this.translations = config.translations
@@ -89,32 +129,72 @@ export class I18nManager implements I18nService {
89
129
  this.globalInstance.locale = value
90
130
  }
91
131
 
132
+ /**
133
+ * Set the global locale.
134
+ *
135
+ * @param locale - The locale to set.
136
+ */
92
137
  setLocale(locale: string): void {
93
138
  this.globalInstance.setLocale(locale)
94
139
  }
95
140
 
141
+ /**
142
+ * Get the global locale.
143
+ *
144
+ * @returns The global locale string.
145
+ */
96
146
  getLocale(): string {
97
147
  return this.globalInstance.getLocale()
98
148
  }
99
149
 
150
+ /**
151
+ * Translate a key using the global locale.
152
+ *
153
+ * @param key - The translation key.
154
+ * @param replacements - Optional replacements.
155
+ * @returns The translated string.
156
+ */
100
157
  t(key: string, replacements?: Record<string, string | number>): string {
101
158
  return this.globalInstance.t(key, replacements)
102
159
  }
103
160
 
161
+ /**
162
+ * Check if a translation key exists in the global locale.
163
+ *
164
+ * @param key - The translation key.
165
+ * @returns True if found.
166
+ */
104
167
  has(key: string): boolean {
105
168
  return this.globalInstance.has(key)
106
169
  }
107
170
 
171
+ /**
172
+ * Clone the global instance.
173
+ *
174
+ * @param locale - Optional locale for the clone.
175
+ * @returns A new I18nInstance.
176
+ */
108
177
  clone(locale?: string): I18nService {
109
178
  return new I18nInstance(this, locale || this.config.defaultLocale)
110
179
  }
111
180
 
112
181
  // --- Manager Internal API ---
113
182
 
183
+ /**
184
+ * Get the I18n configuration.
185
+ *
186
+ * @returns The configuration object.
187
+ */
114
188
  getConfig(): I18nConfig {
115
189
  return this.config
116
190
  }
117
191
 
192
+ /**
193
+ * Add a resource bundle for a specific locale.
194
+ *
195
+ * @param locale - The locale string.
196
+ * @param translations - The translations object.
197
+ */
118
198
  addResource(locale: string, translations: Record<string, string>) {
119
199
  this.translations[locale] = {
120
200
  ...(this.translations[locale] || {}),
@@ -125,11 +205,7 @@ export class I18nManager implements I18nService {
125
205
  /**
126
206
  * Internal translation logic used by instances
127
207
  */
128
- translate(
129
- locale: string,
130
- key: string,
131
- replacements?: Record<string, string | number>
132
- ): string {
208
+ translate(locale: string, key: string, replacements?: Record<string, string | number>): string {
133
209
  const keys = key.split('.')
134
210
  let value: any = this.translations[locale]
135
211
 
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { GravitoOrbit, PlanetCore } from 'gravito-core'
2
- import { type I18nConfig, I18nManager, type I18nService } from './I18nService'
2
+ import { type I18nConfig, I18nManager, type I18nService, localeMiddleware } from './I18nService'
3
3
 
4
4
  declare module 'gravito-core' {
5
5
  interface Variables {