@gravito/cosmos 3.0.0 → 3.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.
- package/.turbo/turbo-build.log +20 -0
- package/.turbo/turbo-test$colon$ci.log +35 -0
- package/.turbo/turbo-test$colon$coverage.log +35 -0
- package/.turbo/turbo-test.log +27 -0
- package/.turbo/turbo-typecheck.log +2 -0
- package/CHANGELOG.md +7 -0
- package/build.ts +35 -0
- package/dist/index.cjs +309 -0
- package/dist/index.d.cts +274 -0
- package/dist/index.d.ts +274 -0
- package/dist/index.js +139 -30
- package/package.json +18 -6
- package/src/I18nService.ts +94 -15
- package/src/index.ts +32 -6
- package/tests/manager.test.ts +0 -1
- package/tests/service.test.ts +5 -0
- package/dist/core/src/Application.d.ts +0 -185
- package/dist/core/src/ConfigManager.d.ts +0 -21
- package/dist/core/src/Container.d.ts +0 -38
- package/dist/core/src/Event.d.ts +0 -5
- package/dist/core/src/EventManager.d.ts +0 -123
- package/dist/core/src/GlobalErrorHandlers.d.ts +0 -31
- package/dist/core/src/GravitoServer.d.ts +0 -20
- package/dist/core/src/HookManager.d.ts +0 -70
- package/dist/core/src/Listener.d.ts +0 -4
- package/dist/core/src/Logger.d.ts +0 -20
- package/dist/core/src/PlanetCore.d.ts +0 -207
- package/dist/core/src/Route.d.ts +0 -25
- package/dist/core/src/Router.d.ts +0 -232
- package/dist/core/src/ServiceProvider.d.ts +0 -150
- package/dist/core/src/adapters/PhotonAdapter.d.ts +0 -142
- package/dist/core/src/adapters/bun/BunContext.d.ts +0 -36
- package/dist/core/src/adapters/bun/BunNativeAdapter.d.ts +0 -21
- package/dist/core/src/adapters/bun/BunRequest.d.ts +0 -27
- package/dist/core/src/adapters/bun/RadixNode.d.ts +0 -15
- package/dist/core/src/adapters/bun/RadixRouter.d.ts +0 -31
- package/dist/core/src/adapters/bun/types.d.ts +0 -20
- package/dist/core/src/adapters/types.d.ts +0 -186
- package/dist/core/src/engine/AOTRouter.d.ts +0 -117
- package/dist/core/src/engine/FastContext.d.ts +0 -34
- package/dist/core/src/engine/Gravito.d.ts +0 -191
- package/dist/core/src/engine/MinimalContext.d.ts +0 -36
- package/dist/core/src/engine/analyzer.d.ts +0 -21
- package/dist/core/src/engine/index.d.ts +0 -26
- package/dist/core/src/engine/path.d.ts +0 -26
- package/dist/core/src/engine/pool.d.ts +0 -83
- package/dist/core/src/engine/types.d.ts +0 -107
- package/dist/core/src/exceptions/AuthenticationException.d.ts +0 -4
- package/dist/core/src/exceptions/AuthorizationException.d.ts +0 -4
- package/dist/core/src/exceptions/GravitoException.d.ts +0 -15
- package/dist/core/src/exceptions/HttpException.d.ts +0 -5
- package/dist/core/src/exceptions/ModelNotFoundException.d.ts +0 -6
- package/dist/core/src/exceptions/ValidationException.d.ts +0 -14
- package/dist/core/src/exceptions/index.d.ts +0 -6
- package/dist/core/src/helpers/Arr.d.ts +0 -14
- package/dist/core/src/helpers/Str.d.ts +0 -18
- package/dist/core/src/helpers/data.d.ts +0 -5
- package/dist/core/src/helpers/errors.d.ts +0 -12
- package/dist/core/src/helpers/response.d.ts +0 -17
- package/dist/core/src/helpers.d.ts +0 -38
- package/dist/core/src/http/CookieJar.d.ts +0 -37
- package/dist/core/src/http/middleware/BodySizeLimit.d.ts +0 -6
- package/dist/core/src/http/middleware/Cors.d.ts +0 -12
- package/dist/core/src/http/middleware/Csrf.d.ts +0 -11
- package/dist/core/src/http/middleware/HeaderTokenGate.d.ts +0 -11
- package/dist/core/src/http/middleware/SecurityHeaders.d.ts +0 -17
- package/dist/core/src/http/middleware/ThrottleRequests.d.ts +0 -12
- package/dist/core/src/http/types.d.ts +0 -312
- package/dist/core/src/index.d.ts +0 -60
- package/dist/core/src/runtime.d.ts +0 -63
- package/dist/core/src/security/Encrypter.d.ts +0 -24
- package/dist/core/src/security/Hasher.d.ts +0 -29
- package/dist/core/src/testing/HttpTester.d.ts +0 -38
- package/dist/core/src/testing/TestResponse.d.ts +0 -78
- package/dist/core/src/testing/index.d.ts +0 -2
- package/dist/core/src/types/events.d.ts +0 -94
- package/dist/cosmos/src/I18nService.d.ts +0 -144
- package/dist/cosmos/src/index.d.ts +0 -21
- package/dist/cosmos/src/loader.d.ts +0 -11
- package/dist/photon/src/index.d.ts +0 -2
- package/dist/src/index.js +0 -173
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { GravitoMiddleware, GravitoOrbit, PlanetCore } from '@gravito/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A map of translations where keys are translation keys and values
|
|
5
|
+
* are either the translated string or a nested map for grouping.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
* @since 3.0.0
|
|
9
|
+
*/
|
|
10
|
+
type TranslationMap = {
|
|
11
|
+
[key: string]: string | TranslationMap;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Configuration for the I18n service.
|
|
15
|
+
*
|
|
16
|
+
* @public
|
|
17
|
+
* @since 3.0.0
|
|
18
|
+
*/
|
|
19
|
+
interface I18nConfig {
|
|
20
|
+
/** The fallback locale to use when the requested one is not found. */
|
|
21
|
+
defaultLocale: string;
|
|
22
|
+
/** List of locales officially supported by the application. */
|
|
23
|
+
supportedLocales: string[];
|
|
24
|
+
/**
|
|
25
|
+
* Optional record of translations indexed by locale.
|
|
26
|
+
* Keys are locale strings (e.g., 'en', 'zh-TW').
|
|
27
|
+
*/
|
|
28
|
+
translations?: Record<string, TranslationMap>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Interface for the I18n service providing translation capabilities.
|
|
32
|
+
*
|
|
33
|
+
* It allows for setting and getting the current locale, translating strings
|
|
34
|
+
* with optional replacements, and checking for key existence.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
* @since 3.0.0
|
|
38
|
+
*/
|
|
39
|
+
interface I18nService {
|
|
40
|
+
/** The current active locale. */
|
|
41
|
+
locale: string;
|
|
42
|
+
/**
|
|
43
|
+
* Set the active locale for this service instance.
|
|
44
|
+
*
|
|
45
|
+
* @param locale - Valid locale string from supportedLocales.
|
|
46
|
+
*/
|
|
47
|
+
setLocale(locale: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* Get the current active locale.
|
|
50
|
+
*
|
|
51
|
+
* @returns Current locale string.
|
|
52
|
+
*/
|
|
53
|
+
getLocale(): string;
|
|
54
|
+
/**
|
|
55
|
+
* Translate a key into the current locale.
|
|
56
|
+
*
|
|
57
|
+
* @param key - The translation key (e.g., 'auth.login_success').
|
|
58
|
+
* @param replacements - Optional placeholders in the format `:key` replaced by values.
|
|
59
|
+
* @returns The translated string, or the key itself if not found.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* i18n.t('messages.hello', { name: 'John' }); // "Hello John"
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
t(key: string, replacements?: Record<string, string | number>): string;
|
|
67
|
+
/**
|
|
68
|
+
* Check if a translation key exists for the current locale.
|
|
69
|
+
*
|
|
70
|
+
* @param key - The key to check.
|
|
71
|
+
* @returns True if the key exists, false otherwise.
|
|
72
|
+
*/
|
|
73
|
+
has(key: string): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Create a new request-scoped instance of the I18n service.
|
|
76
|
+
*
|
|
77
|
+
* @param locale - Optional initial locale for the new instance.
|
|
78
|
+
* @returns A new I18nService instance.
|
|
79
|
+
*/
|
|
80
|
+
clone(locale?: string): I18nService;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Request-scoped I18n Instance
|
|
84
|
+
* Holds the state (locale) for a single request, but shares the heavy resources (translations)
|
|
85
|
+
*/
|
|
86
|
+
declare class I18nInstance implements I18nService {
|
|
87
|
+
readonly manager: I18nManager;
|
|
88
|
+
private _locale;
|
|
89
|
+
/**
|
|
90
|
+
* Create a new I18nInstance.
|
|
91
|
+
*
|
|
92
|
+
* @param manager - The I18nManager instance.
|
|
93
|
+
* @param initialLocale - The initial locale for this instance.
|
|
94
|
+
*/
|
|
95
|
+
constructor(manager: I18nManager, initialLocale: string);
|
|
96
|
+
get locale(): string;
|
|
97
|
+
set locale(value: string);
|
|
98
|
+
/**
|
|
99
|
+
* Set the current locale.
|
|
100
|
+
*
|
|
101
|
+
* @param locale - The locale to set.
|
|
102
|
+
*/
|
|
103
|
+
setLocale(locale: string): void;
|
|
104
|
+
/**
|
|
105
|
+
* Get the current locale.
|
|
106
|
+
*
|
|
107
|
+
* @returns The current locale string.
|
|
108
|
+
*/
|
|
109
|
+
getLocale(): string;
|
|
110
|
+
/**
|
|
111
|
+
* Translate a key.
|
|
112
|
+
*
|
|
113
|
+
* @param key - The translation key (e.g., 'messages.welcome').
|
|
114
|
+
* @param replacements - Optional replacements for parameters in the translation string.
|
|
115
|
+
* @returns The translated string, or the key if not found.
|
|
116
|
+
*/
|
|
117
|
+
t(key: string, replacements?: Record<string, string | number>): string;
|
|
118
|
+
/**
|
|
119
|
+
* Check if a translation key exists.
|
|
120
|
+
*
|
|
121
|
+
* @param key - The translation key to check.
|
|
122
|
+
* @returns True if the key exists, false otherwise.
|
|
123
|
+
*/
|
|
124
|
+
has(key: string): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Clone the current instance with a potentially new locale.
|
|
127
|
+
*
|
|
128
|
+
* @param locale - Optional new locale for the cloned instance.
|
|
129
|
+
* @returns A new I18nInstance.
|
|
130
|
+
*/
|
|
131
|
+
clone(locale?: string): I18nService;
|
|
132
|
+
/**
|
|
133
|
+
* Get the I18n configuration.
|
|
134
|
+
*/
|
|
135
|
+
getConfig(): I18nConfig;
|
|
136
|
+
/**
|
|
137
|
+
* Get the translations.
|
|
138
|
+
*/
|
|
139
|
+
get translations(): Record<string, TranslationMap>;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Global I18n Manager
|
|
143
|
+
* Holds shared configuration and translation resources
|
|
144
|
+
*/
|
|
145
|
+
declare class I18nManager implements I18nService {
|
|
146
|
+
private config;
|
|
147
|
+
translations: Record<string, TranslationMap>;
|
|
148
|
+
private globalInstance;
|
|
149
|
+
/**
|
|
150
|
+
* Create a new I18nManager.
|
|
151
|
+
*
|
|
152
|
+
* @param config - The I18n configuration.
|
|
153
|
+
*/
|
|
154
|
+
constructor(config: I18nConfig);
|
|
155
|
+
get locale(): string;
|
|
156
|
+
set locale(value: string);
|
|
157
|
+
/**
|
|
158
|
+
* Set the global locale.
|
|
159
|
+
*
|
|
160
|
+
* @param locale - The locale to set.
|
|
161
|
+
*/
|
|
162
|
+
setLocale(locale: string): void;
|
|
163
|
+
/**
|
|
164
|
+
* Get the global locale.
|
|
165
|
+
*
|
|
166
|
+
* @returns The global locale string.
|
|
167
|
+
*/
|
|
168
|
+
getLocale(): string;
|
|
169
|
+
/**
|
|
170
|
+
* Translate a key using the global locale.
|
|
171
|
+
*
|
|
172
|
+
* @param key - The translation key.
|
|
173
|
+
* @param replacements - Optional replacements.
|
|
174
|
+
* @returns The translated string.
|
|
175
|
+
*/
|
|
176
|
+
t(key: string, replacements?: Record<string, string | number>): string;
|
|
177
|
+
/**
|
|
178
|
+
* Check if a translation key exists in the global locale.
|
|
179
|
+
*
|
|
180
|
+
* @param key - The translation key.
|
|
181
|
+
* @returns True if found.
|
|
182
|
+
*/
|
|
183
|
+
has(key: string): boolean;
|
|
184
|
+
/**
|
|
185
|
+
* Clone the global instance.
|
|
186
|
+
*
|
|
187
|
+
* @param locale - Optional locale for the clone.
|
|
188
|
+
* @returns A new I18nInstance.
|
|
189
|
+
*/
|
|
190
|
+
clone(locale?: string): I18nService;
|
|
191
|
+
/**
|
|
192
|
+
* Get the I18n configuration.
|
|
193
|
+
*
|
|
194
|
+
* @returns The configuration object.
|
|
195
|
+
*/
|
|
196
|
+
getConfig(): I18nConfig;
|
|
197
|
+
/**
|
|
198
|
+
* Add a resource bundle for a specific locale.
|
|
199
|
+
*
|
|
200
|
+
* @param locale - The locale string.
|
|
201
|
+
* @param translations - The translations object.
|
|
202
|
+
*/
|
|
203
|
+
addResource(locale: string, translations: TranslationMap): void;
|
|
204
|
+
/**
|
|
205
|
+
* Internal translation logic used by instances
|
|
206
|
+
*/
|
|
207
|
+
translate(locale: string, key: string, replacements?: Record<string, string | number>): string;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Locale Middleware
|
|
211
|
+
*
|
|
212
|
+
* Detects locale from:
|
|
213
|
+
* 1. Route Parameter (e.g. /:locale/foo) - Recommended for SEO
|
|
214
|
+
* 2. Header (Accept-Language) - Recommended for APIs
|
|
215
|
+
*/
|
|
216
|
+
declare const localeMiddleware: (i18nManager: I18nService) => GravitoMiddleware;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Load translations from a directory
|
|
220
|
+
* Structure:
|
|
221
|
+
* /lang
|
|
222
|
+
* /en.json -> { "welcome": "Hello" }
|
|
223
|
+
* /zh.json -> { "welcome": "Hello" }
|
|
224
|
+
* /en/auth.json -> { "failed": "Login failed" } (Optional deep structure, maybe later)
|
|
225
|
+
*
|
|
226
|
+
* For now, we support flat JSON files per locale: en.json, zh.json
|
|
227
|
+
*/
|
|
228
|
+
declare function loadTranslations(directory: string): Promise<Record<string, Record<string, string>>>;
|
|
229
|
+
|
|
230
|
+
declare module '@gravito/core' {
|
|
231
|
+
interface GravitoVariables {
|
|
232
|
+
/** I18n service for translations */
|
|
233
|
+
i18n: I18nService;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* OrbitCosmos provides internationalization (i18n) support for Gravito.
|
|
238
|
+
* It manages translations, locale switching, and provides a middleware for request-scoped locale detection.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```typescript
|
|
242
|
+
* const cosmos = new OrbitCosmos({
|
|
243
|
+
* defaultLocale: 'en',
|
|
244
|
+
* supportedLocales: ['en', 'zh-TW'],
|
|
245
|
+
* translations: {
|
|
246
|
+
* en: { welcome: 'Welcome!' },
|
|
247
|
+
* 'zh-TW': { welcome: '歡迎!' }
|
|
248
|
+
* }
|
|
249
|
+
* });
|
|
250
|
+
* core.addOrbit(cosmos);
|
|
251
|
+
* ```
|
|
252
|
+
* @public
|
|
253
|
+
*/
|
|
254
|
+
declare class OrbitCosmos implements GravitoOrbit {
|
|
255
|
+
private config;
|
|
256
|
+
/**
|
|
257
|
+
* Create a new OrbitCosmos instance.
|
|
258
|
+
* @param config - The i18n configuration options.
|
|
259
|
+
*/
|
|
260
|
+
constructor(config: I18nConfig);
|
|
261
|
+
/**
|
|
262
|
+
* Install the i18n service into PlanetCore.
|
|
263
|
+
* Registers the I18nManager and sets up the locale middleware.
|
|
264
|
+
*
|
|
265
|
+
* @param core - The PlanetCore instance.
|
|
266
|
+
*/
|
|
267
|
+
install(core: PlanetCore): void;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* @deprecated Use OrbitCosmos instead. Will be removed in v4.0.0.
|
|
271
|
+
*/
|
|
272
|
+
declare const I18nOrbit: typeof OrbitCosmos;
|
|
273
|
+
|
|
274
|
+
export { type I18nConfig, I18nInstance, I18nManager, I18nOrbit, type I18nService, OrbitCosmos, type TranslationMap, loadTranslations, localeMiddleware };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { GravitoMiddleware, GravitoOrbit, PlanetCore } from '@gravito/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A map of translations where keys are translation keys and values
|
|
5
|
+
* are either the translated string or a nested map for grouping.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
* @since 3.0.0
|
|
9
|
+
*/
|
|
10
|
+
type TranslationMap = {
|
|
11
|
+
[key: string]: string | TranslationMap;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Configuration for the I18n service.
|
|
15
|
+
*
|
|
16
|
+
* @public
|
|
17
|
+
* @since 3.0.0
|
|
18
|
+
*/
|
|
19
|
+
interface I18nConfig {
|
|
20
|
+
/** The fallback locale to use when the requested one is not found. */
|
|
21
|
+
defaultLocale: string;
|
|
22
|
+
/** List of locales officially supported by the application. */
|
|
23
|
+
supportedLocales: string[];
|
|
24
|
+
/**
|
|
25
|
+
* Optional record of translations indexed by locale.
|
|
26
|
+
* Keys are locale strings (e.g., 'en', 'zh-TW').
|
|
27
|
+
*/
|
|
28
|
+
translations?: Record<string, TranslationMap>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Interface for the I18n service providing translation capabilities.
|
|
32
|
+
*
|
|
33
|
+
* It allows for setting and getting the current locale, translating strings
|
|
34
|
+
* with optional replacements, and checking for key existence.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
* @since 3.0.0
|
|
38
|
+
*/
|
|
39
|
+
interface I18nService {
|
|
40
|
+
/** The current active locale. */
|
|
41
|
+
locale: string;
|
|
42
|
+
/**
|
|
43
|
+
* Set the active locale for this service instance.
|
|
44
|
+
*
|
|
45
|
+
* @param locale - Valid locale string from supportedLocales.
|
|
46
|
+
*/
|
|
47
|
+
setLocale(locale: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* Get the current active locale.
|
|
50
|
+
*
|
|
51
|
+
* @returns Current locale string.
|
|
52
|
+
*/
|
|
53
|
+
getLocale(): string;
|
|
54
|
+
/**
|
|
55
|
+
* Translate a key into the current locale.
|
|
56
|
+
*
|
|
57
|
+
* @param key - The translation key (e.g., 'auth.login_success').
|
|
58
|
+
* @param replacements - Optional placeholders in the format `:key` replaced by values.
|
|
59
|
+
* @returns The translated string, or the key itself if not found.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* i18n.t('messages.hello', { name: 'John' }); // "Hello John"
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
t(key: string, replacements?: Record<string, string | number>): string;
|
|
67
|
+
/**
|
|
68
|
+
* Check if a translation key exists for the current locale.
|
|
69
|
+
*
|
|
70
|
+
* @param key - The key to check.
|
|
71
|
+
* @returns True if the key exists, false otherwise.
|
|
72
|
+
*/
|
|
73
|
+
has(key: string): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Create a new request-scoped instance of the I18n service.
|
|
76
|
+
*
|
|
77
|
+
* @param locale - Optional initial locale for the new instance.
|
|
78
|
+
* @returns A new I18nService instance.
|
|
79
|
+
*/
|
|
80
|
+
clone(locale?: string): I18nService;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Request-scoped I18n Instance
|
|
84
|
+
* Holds the state (locale) for a single request, but shares the heavy resources (translations)
|
|
85
|
+
*/
|
|
86
|
+
declare class I18nInstance implements I18nService {
|
|
87
|
+
readonly manager: I18nManager;
|
|
88
|
+
private _locale;
|
|
89
|
+
/**
|
|
90
|
+
* Create a new I18nInstance.
|
|
91
|
+
*
|
|
92
|
+
* @param manager - The I18nManager instance.
|
|
93
|
+
* @param initialLocale - The initial locale for this instance.
|
|
94
|
+
*/
|
|
95
|
+
constructor(manager: I18nManager, initialLocale: string);
|
|
96
|
+
get locale(): string;
|
|
97
|
+
set locale(value: string);
|
|
98
|
+
/**
|
|
99
|
+
* Set the current locale.
|
|
100
|
+
*
|
|
101
|
+
* @param locale - The locale to set.
|
|
102
|
+
*/
|
|
103
|
+
setLocale(locale: string): void;
|
|
104
|
+
/**
|
|
105
|
+
* Get the current locale.
|
|
106
|
+
*
|
|
107
|
+
* @returns The current locale string.
|
|
108
|
+
*/
|
|
109
|
+
getLocale(): string;
|
|
110
|
+
/**
|
|
111
|
+
* Translate a key.
|
|
112
|
+
*
|
|
113
|
+
* @param key - The translation key (e.g., 'messages.welcome').
|
|
114
|
+
* @param replacements - Optional replacements for parameters in the translation string.
|
|
115
|
+
* @returns The translated string, or the key if not found.
|
|
116
|
+
*/
|
|
117
|
+
t(key: string, replacements?: Record<string, string | number>): string;
|
|
118
|
+
/**
|
|
119
|
+
* Check if a translation key exists.
|
|
120
|
+
*
|
|
121
|
+
* @param key - The translation key to check.
|
|
122
|
+
* @returns True if the key exists, false otherwise.
|
|
123
|
+
*/
|
|
124
|
+
has(key: string): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Clone the current instance with a potentially new locale.
|
|
127
|
+
*
|
|
128
|
+
* @param locale - Optional new locale for the cloned instance.
|
|
129
|
+
* @returns A new I18nInstance.
|
|
130
|
+
*/
|
|
131
|
+
clone(locale?: string): I18nService;
|
|
132
|
+
/**
|
|
133
|
+
* Get the I18n configuration.
|
|
134
|
+
*/
|
|
135
|
+
getConfig(): I18nConfig;
|
|
136
|
+
/**
|
|
137
|
+
* Get the translations.
|
|
138
|
+
*/
|
|
139
|
+
get translations(): Record<string, TranslationMap>;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Global I18n Manager
|
|
143
|
+
* Holds shared configuration and translation resources
|
|
144
|
+
*/
|
|
145
|
+
declare class I18nManager implements I18nService {
|
|
146
|
+
private config;
|
|
147
|
+
translations: Record<string, TranslationMap>;
|
|
148
|
+
private globalInstance;
|
|
149
|
+
/**
|
|
150
|
+
* Create a new I18nManager.
|
|
151
|
+
*
|
|
152
|
+
* @param config - The I18n configuration.
|
|
153
|
+
*/
|
|
154
|
+
constructor(config: I18nConfig);
|
|
155
|
+
get locale(): string;
|
|
156
|
+
set locale(value: string);
|
|
157
|
+
/**
|
|
158
|
+
* Set the global locale.
|
|
159
|
+
*
|
|
160
|
+
* @param locale - The locale to set.
|
|
161
|
+
*/
|
|
162
|
+
setLocale(locale: string): void;
|
|
163
|
+
/**
|
|
164
|
+
* Get the global locale.
|
|
165
|
+
*
|
|
166
|
+
* @returns The global locale string.
|
|
167
|
+
*/
|
|
168
|
+
getLocale(): string;
|
|
169
|
+
/**
|
|
170
|
+
* Translate a key using the global locale.
|
|
171
|
+
*
|
|
172
|
+
* @param key - The translation key.
|
|
173
|
+
* @param replacements - Optional replacements.
|
|
174
|
+
* @returns The translated string.
|
|
175
|
+
*/
|
|
176
|
+
t(key: string, replacements?: Record<string, string | number>): string;
|
|
177
|
+
/**
|
|
178
|
+
* Check if a translation key exists in the global locale.
|
|
179
|
+
*
|
|
180
|
+
* @param key - The translation key.
|
|
181
|
+
* @returns True if found.
|
|
182
|
+
*/
|
|
183
|
+
has(key: string): boolean;
|
|
184
|
+
/**
|
|
185
|
+
* Clone the global instance.
|
|
186
|
+
*
|
|
187
|
+
* @param locale - Optional locale for the clone.
|
|
188
|
+
* @returns A new I18nInstance.
|
|
189
|
+
*/
|
|
190
|
+
clone(locale?: string): I18nService;
|
|
191
|
+
/**
|
|
192
|
+
* Get the I18n configuration.
|
|
193
|
+
*
|
|
194
|
+
* @returns The configuration object.
|
|
195
|
+
*/
|
|
196
|
+
getConfig(): I18nConfig;
|
|
197
|
+
/**
|
|
198
|
+
* Add a resource bundle for a specific locale.
|
|
199
|
+
*
|
|
200
|
+
* @param locale - The locale string.
|
|
201
|
+
* @param translations - The translations object.
|
|
202
|
+
*/
|
|
203
|
+
addResource(locale: string, translations: TranslationMap): void;
|
|
204
|
+
/**
|
|
205
|
+
* Internal translation logic used by instances
|
|
206
|
+
*/
|
|
207
|
+
translate(locale: string, key: string, replacements?: Record<string, string | number>): string;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Locale Middleware
|
|
211
|
+
*
|
|
212
|
+
* Detects locale from:
|
|
213
|
+
* 1. Route Parameter (e.g. /:locale/foo) - Recommended for SEO
|
|
214
|
+
* 2. Header (Accept-Language) - Recommended for APIs
|
|
215
|
+
*/
|
|
216
|
+
declare const localeMiddleware: (i18nManager: I18nService) => GravitoMiddleware;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Load translations from a directory
|
|
220
|
+
* Structure:
|
|
221
|
+
* /lang
|
|
222
|
+
* /en.json -> { "welcome": "Hello" }
|
|
223
|
+
* /zh.json -> { "welcome": "Hello" }
|
|
224
|
+
* /en/auth.json -> { "failed": "Login failed" } (Optional deep structure, maybe later)
|
|
225
|
+
*
|
|
226
|
+
* For now, we support flat JSON files per locale: en.json, zh.json
|
|
227
|
+
*/
|
|
228
|
+
declare function loadTranslations(directory: string): Promise<Record<string, Record<string, string>>>;
|
|
229
|
+
|
|
230
|
+
declare module '@gravito/core' {
|
|
231
|
+
interface GravitoVariables {
|
|
232
|
+
/** I18n service for translations */
|
|
233
|
+
i18n: I18nService;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* OrbitCosmos provides internationalization (i18n) support for Gravito.
|
|
238
|
+
* It manages translations, locale switching, and provides a middleware for request-scoped locale detection.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```typescript
|
|
242
|
+
* const cosmos = new OrbitCosmos({
|
|
243
|
+
* defaultLocale: 'en',
|
|
244
|
+
* supportedLocales: ['en', 'zh-TW'],
|
|
245
|
+
* translations: {
|
|
246
|
+
* en: { welcome: 'Welcome!' },
|
|
247
|
+
* 'zh-TW': { welcome: '歡迎!' }
|
|
248
|
+
* }
|
|
249
|
+
* });
|
|
250
|
+
* core.addOrbit(cosmos);
|
|
251
|
+
* ```
|
|
252
|
+
* @public
|
|
253
|
+
*/
|
|
254
|
+
declare class OrbitCosmos implements GravitoOrbit {
|
|
255
|
+
private config;
|
|
256
|
+
/**
|
|
257
|
+
* Create a new OrbitCosmos instance.
|
|
258
|
+
* @param config - The i18n configuration options.
|
|
259
|
+
*/
|
|
260
|
+
constructor(config: I18nConfig);
|
|
261
|
+
/**
|
|
262
|
+
* Install the i18n service into PlanetCore.
|
|
263
|
+
* Registers the I18nManager and sets up the locale middleware.
|
|
264
|
+
*
|
|
265
|
+
* @param core - The PlanetCore instance.
|
|
266
|
+
*/
|
|
267
|
+
install(core: PlanetCore): void;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* @deprecated Use OrbitCosmos instead. Will be removed in v4.0.0.
|
|
271
|
+
*/
|
|
272
|
+
declare const I18nOrbit: typeof OrbitCosmos;
|
|
273
|
+
|
|
274
|
+
export { type I18nConfig, I18nInstance, I18nManager, I18nOrbit, type I18nService, OrbitCosmos, type TranslationMap, loadTranslations, localeMiddleware };
|