intor 2.0.2 → 2.1.0
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/index.cjs +76 -34
- package/dist/index.d.cts +83 -18
- package/dist/index.d.ts +83 -18
- package/dist/index.js +76 -34
- package/dist/next/index.cjs +206 -75
- package/dist/next/index.d.cts +67 -24
- package/dist/next/index.d.ts +67 -24
- package/dist/next/index.js +205 -75
- package/dist/next/middleware/index.cjs +36 -25
- package/dist/next/middleware/index.d.cts +3 -3
- package/dist/next/middleware/index.d.ts +3 -3
- package/dist/next/middleware/index.js +36 -25
- package/dist/next/server/index.cjs +81 -39
- package/dist/next/server/index.d.cts +51 -19
- package/dist/next/server/index.d.ts +51 -19
- package/dist/next/server/index.js +80 -37
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -76,13 +76,19 @@ var DEFAULT_FORMATTER_CONFIG = {
|
|
|
76
76
|
};
|
|
77
77
|
function getLogger({
|
|
78
78
|
id,
|
|
79
|
-
formatterConfig
|
|
79
|
+
formatterConfig,
|
|
80
|
+
preset,
|
|
80
81
|
...options
|
|
81
82
|
}) {
|
|
82
83
|
const pool = getGlobalLoggerPool();
|
|
83
84
|
let logger = pool.get(id);
|
|
84
85
|
if (!logger) {
|
|
85
|
-
logger = logry.logry({
|
|
86
|
+
logger = logry.logry({
|
|
87
|
+
id,
|
|
88
|
+
formatterConfig: !formatterConfig && !preset ? DEFAULT_FORMATTER_CONFIG : formatterConfig,
|
|
89
|
+
preset,
|
|
90
|
+
...options
|
|
91
|
+
});
|
|
86
92
|
pool.set(id, logger);
|
|
87
93
|
if (pool.size > 1e3) {
|
|
88
94
|
const keys = Array.from(pool.keys());
|
|
@@ -626,7 +632,10 @@ var loadLocalMessages = async ({
|
|
|
626
632
|
namespaces: namespaces && namespaces.length > 0 ? { count: namespaces?.length, list: [...namespaces] } : "All Namespaces",
|
|
627
633
|
concurrency
|
|
628
634
|
});
|
|
629
|
-
|
|
635
|
+
let pool;
|
|
636
|
+
if (cache.enabled) {
|
|
637
|
+
pool = getGlobalMessagesPool();
|
|
638
|
+
}
|
|
630
639
|
const key = normalizeCacheKey([
|
|
631
640
|
loggerOptions.id,
|
|
632
641
|
resolvedBasePath,
|
|
@@ -635,7 +644,7 @@ var loadLocalMessages = async ({
|
|
|
635
644
|
[...namespaces ?? []].sort().join(",")
|
|
636
645
|
]);
|
|
637
646
|
if (cache.enabled && key) {
|
|
638
|
-
const cached = await pool
|
|
647
|
+
const cached = await pool?.get(key);
|
|
639
648
|
if (cached) {
|
|
640
649
|
logger.debug("Messages cache hit.", { key });
|
|
641
650
|
return cached;
|
|
@@ -652,7 +661,7 @@ var loadLocalMessages = async ({
|
|
|
652
661
|
logger: loggerOptions
|
|
653
662
|
});
|
|
654
663
|
if (cache.enabled && key) {
|
|
655
|
-
await pool
|
|
664
|
+
await pool?.set(key, messages, cache.ttl);
|
|
656
665
|
}
|
|
657
666
|
const end = perf_hooks.performance.now();
|
|
658
667
|
const duration = Math.round(end - start);
|
|
@@ -769,7 +778,10 @@ var loadApiMessages = async ({
|
|
|
769
778
|
logger.warn("No apiUrl provided. Skipping fetch.");
|
|
770
779
|
return;
|
|
771
780
|
}
|
|
772
|
-
|
|
781
|
+
let pool;
|
|
782
|
+
if (cache.enabled) {
|
|
783
|
+
pool = getGlobalMessagesPool();
|
|
784
|
+
}
|
|
773
785
|
const key = normalizeCacheKey([
|
|
774
786
|
loggerOptions.id,
|
|
775
787
|
basePath,
|
|
@@ -778,7 +790,7 @@ var loadApiMessages = async ({
|
|
|
778
790
|
[...namespaces ?? []].sort().join(",")
|
|
779
791
|
]);
|
|
780
792
|
if (cache.enabled && key) {
|
|
781
|
-
const cached = await pool
|
|
793
|
+
const cached = await pool?.get(key);
|
|
782
794
|
if (cached) {
|
|
783
795
|
logger.debug("Messages cache hit.", { key });
|
|
784
796
|
return cached;
|
|
@@ -794,7 +806,7 @@ var loadApiMessages = async ({
|
|
|
794
806
|
});
|
|
795
807
|
if (messages) {
|
|
796
808
|
if (cache.enabled && key) {
|
|
797
|
-
await pool
|
|
809
|
+
await pool?.set(key, messages, cache.ttl);
|
|
798
810
|
}
|
|
799
811
|
return messages;
|
|
800
812
|
}
|
|
@@ -812,7 +824,7 @@ var loadApiMessages = async ({
|
|
|
812
824
|
searchParams: decodeURIComponent(searchParams.toString())
|
|
813
825
|
});
|
|
814
826
|
if (cache.enabled && key) {
|
|
815
|
-
await pool
|
|
827
|
+
await pool?.set(key, fallbackResult.messages, cache.ttl);
|
|
816
828
|
}
|
|
817
829
|
return fallbackResult.messages;
|
|
818
830
|
}
|
|
@@ -823,35 +835,41 @@ var loadApiMessages = async ({
|
|
|
823
835
|
return;
|
|
824
836
|
};
|
|
825
837
|
|
|
826
|
-
// src/modules/messages/
|
|
827
|
-
var
|
|
838
|
+
// src/modules/messages/load-messages.ts
|
|
839
|
+
var loadMessages = async ({
|
|
828
840
|
config,
|
|
829
841
|
locale,
|
|
830
842
|
pathname
|
|
831
843
|
}) => {
|
|
832
|
-
const baseLogger = getLogger({ id: config.id });
|
|
844
|
+
const baseLogger = getLogger({ id: config.id, ...config.logger });
|
|
833
845
|
const logger = baseLogger.child({ scope: "messages-loader" });
|
|
834
|
-
|
|
846
|
+
if (!config.loader) {
|
|
847
|
+
logger.warn(
|
|
848
|
+
"No loader options have been configured in the current config."
|
|
849
|
+
);
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
const { loader } = config;
|
|
835
853
|
const fallbackLocales = config.fallbackLocales[locale] || [];
|
|
836
854
|
const namespaces = resolveNamespaces({ config, pathname });
|
|
837
855
|
logger.debug("Namespaces ready for loading.", {
|
|
838
856
|
namespaces: namespaces.length > 0 ? { count: namespaces.length, list: [...namespaces] } : "All Namespaces"
|
|
839
857
|
});
|
|
840
|
-
logger.debug("Loader type selected.", { loaderType:
|
|
858
|
+
logger.debug("Loader type selected.", { loaderType: loader.type });
|
|
841
859
|
let loadedMessages;
|
|
842
|
-
if (
|
|
843
|
-
const loadLocalMessages2 = createLoadLocalMessages(
|
|
860
|
+
if (loader.type === "import") {
|
|
861
|
+
const loadLocalMessages2 = createLoadLocalMessages(loader.basePath);
|
|
844
862
|
loadedMessages = await loadLocalMessages2({
|
|
845
|
-
...
|
|
863
|
+
...loader,
|
|
846
864
|
locale,
|
|
847
865
|
fallbackLocales,
|
|
848
866
|
namespaces,
|
|
849
867
|
cache: config.cache,
|
|
850
868
|
logger: { id: config.id }
|
|
851
869
|
});
|
|
852
|
-
} else if (
|
|
870
|
+
} else if (loader.type === "api") {
|
|
853
871
|
loadedMessages = await loadApiMessages({
|
|
854
|
-
...
|
|
872
|
+
...loader,
|
|
855
873
|
locale,
|
|
856
874
|
fallbackLocales,
|
|
857
875
|
namespaces,
|
|
@@ -865,29 +883,27 @@ var getMessages = async ({
|
|
|
865
883
|
};
|
|
866
884
|
|
|
867
885
|
// src/modules/intor/intor.ts
|
|
868
|
-
var intor = async ({
|
|
869
|
-
config,
|
|
870
|
-
adapter,
|
|
871
|
-
adapterRuntime
|
|
872
|
-
}) => {
|
|
886
|
+
var intor = async (config, i18nContext) => {
|
|
873
887
|
const baseLogger = getLogger({ id: config.id, ...config.logger });
|
|
874
888
|
const logger = baseLogger.child({ scope: "intor" });
|
|
875
889
|
logger.info("Start Intor initialization.");
|
|
876
890
|
const { messages, loader } = config;
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
891
|
+
const isI18nContextFunction = typeof i18nContext === "function";
|
|
892
|
+
let context;
|
|
893
|
+
if (isI18nContextFunction) {
|
|
894
|
+
context = await i18nContext(config);
|
|
880
895
|
} else {
|
|
881
|
-
|
|
882
|
-
locale:
|
|
883
|
-
pathname:
|
|
896
|
+
context = {
|
|
897
|
+
locale: i18nContext?.locale || config.defaultLocale,
|
|
898
|
+
pathname: i18nContext?.pathname || ""
|
|
884
899
|
};
|
|
885
900
|
}
|
|
886
|
-
const { locale, pathname } =
|
|
887
|
-
|
|
901
|
+
const { locale, pathname } = context;
|
|
902
|
+
const source = isI18nContextFunction ? "[function]" : "[static object]";
|
|
903
|
+
logger.debug(`Context resolved via ${source}.`, context);
|
|
888
904
|
let loadedMessages;
|
|
889
905
|
if (shouldLoadMessages(loader)) {
|
|
890
|
-
loadedMessages = await
|
|
906
|
+
loadedMessages = await loadMessages({ config, locale, pathname });
|
|
891
907
|
}
|
|
892
908
|
const mergedMessages = mergeMessages(messages, loadedMessages);
|
|
893
909
|
logger.info("Messages initialized.", {
|
|
@@ -905,6 +921,31 @@ var intor = async ({
|
|
|
905
921
|
messages: mergedMessages
|
|
906
922
|
};
|
|
907
923
|
};
|
|
924
|
+
async function getTranslator(opts) {
|
|
925
|
+
const { config, locale, pathname = "", preKey } = opts;
|
|
926
|
+
const messages = await loadMessages({ config, locale, pathname });
|
|
927
|
+
const translator = new intorTranslator.Translator({
|
|
928
|
+
locale,
|
|
929
|
+
messages,
|
|
930
|
+
fallbackLocales: config.fallbackLocales,
|
|
931
|
+
loadingMessage: config.translator?.loadingMessage,
|
|
932
|
+
placeholder: config.translator?.placeholder
|
|
933
|
+
});
|
|
934
|
+
const props = {
|
|
935
|
+
messages,
|
|
936
|
+
locale
|
|
937
|
+
};
|
|
938
|
+
if (preKey) {
|
|
939
|
+
const scoped = translator.scoped(preKey);
|
|
940
|
+
return { ...props, ...scoped };
|
|
941
|
+
} else {
|
|
942
|
+
return {
|
|
943
|
+
...props,
|
|
944
|
+
t: translator.t,
|
|
945
|
+
hasKey: translator.hasKey
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
}
|
|
908
949
|
|
|
909
950
|
Object.defineProperty(exports, "Translator", {
|
|
910
951
|
enumerable: true,
|
|
@@ -914,10 +955,11 @@ exports.PREFIX_PLACEHOLDER = PREFIX_PLACEHOLDER;
|
|
|
914
955
|
exports.clearLoggerPool = clearLoggerPool;
|
|
915
956
|
exports.clearMessagesPool = clearMessagesPool;
|
|
916
957
|
exports.extractPathname = extractPathname;
|
|
917
|
-
exports.
|
|
958
|
+
exports.getTranslator = getTranslator;
|
|
918
959
|
exports.intor = intor;
|
|
919
960
|
exports.loadApiMessages = loadApiMessages;
|
|
920
961
|
exports.loadLocalMessages = loadLocalMessages;
|
|
962
|
+
exports.loadMessages = loadMessages;
|
|
921
963
|
exports.mergeMessages = mergeMessages;
|
|
922
964
|
exports.normalizeCacheKey = normalizeCacheKey;
|
|
923
965
|
exports.normalizeLocale = normalizeLocale;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Locale, LocaleNamespaceMessages, FallbackLocalesMap } from 'intor-translator';
|
|
2
|
-
export { FallbackLocalesMap, FormatMessage, Locale, LocaleKey, LocaleNamespaceMessages, Message, MessageRecord, Namespace, NamespaceMessages, NestedMessage, OnLoading, OnMissing, Replacement, RichReplacement, StrictLocaleKey, TranslateContext, TranslateHandlers, Translator, UnionLocaleMessages } from 'intor-translator';
|
|
3
1
|
import { Level, NormalizerConfig, FormatterConfig, LoggerPreset } from 'logry/edge';
|
|
2
|
+
import { Locale, LocaleNamespaceMessages, FallbackLocalesMap, InferTranslatorKey, LocaleKey, Replacement, RichReplacement, NodeKeys, UnionLocaleMessages, ScopedLeafKeys } from 'intor-translator';
|
|
3
|
+
export { FallbackLocalesMap, FormatMessage, Locale, LocaleKey, LocaleNamespaceMessages, Message, MessageRecord, Namespace, NamespaceMessages, NestedMessage, OnLoading, OnMissing, Replacement, RichReplacement, StrictLocaleKey, TranslateContext, TranslateHandlers, Translator, UnionLocaleMessages } from 'intor-translator';
|
|
4
4
|
import { Logger } from 'logry';
|
|
5
5
|
import Keyv from 'keyv';
|
|
6
6
|
|
|
@@ -122,15 +122,28 @@ type IntorResolvedConfig = (WithLoader | WithoutLoader) & {
|
|
|
122
122
|
readonly cache: CacheResolvedOptions;
|
|
123
123
|
};
|
|
124
124
|
|
|
125
|
-
|
|
126
|
-
|
|
125
|
+
declare const PREFIX_PLACEHOLDER = "{locale}";
|
|
126
|
+
|
|
127
|
+
type IfGen<Then, Else = never> = IntorGeneratedTypes extends void ? Else : Then;
|
|
128
|
+
type GenConfigKeys = IfGen<keyof IntorGeneratedTypes, string>;
|
|
129
|
+
type GenConfig<C extends GenConfigKeys = "__default__"> = IntorGeneratedTypes extends void ? {
|
|
130
|
+
Locales: string;
|
|
131
|
+
Messages: LocaleNamespaceMessages;
|
|
132
|
+
} : C extends keyof IntorGeneratedTypes ? {
|
|
133
|
+
Locales: IntorGeneratedTypes[C]["Locales"];
|
|
134
|
+
Messages: {
|
|
135
|
+
[K in IntorGeneratedTypes[C]["Locales"]]: IntorGeneratedTypes[C]["Messages"][typeof PREFIX_PLACEHOLDER];
|
|
136
|
+
};
|
|
137
|
+
} : never;
|
|
138
|
+
type GenMessages<C extends GenConfigKeys = "__default__"> = GenConfig<C>["Messages"];
|
|
139
|
+
type GenLocaleFallback = Locale;
|
|
140
|
+
type GenLocale<Config extends string = "__default__"> = Config extends keyof IntorGeneratedTypes ? IntorGeneratedTypes[Config]["Locales"] : GenLocaleFallback;
|
|
141
|
+
|
|
142
|
+
interface I18nContext {
|
|
143
|
+
locale: GenLocale;
|
|
127
144
|
pathname: string;
|
|
128
145
|
}
|
|
129
|
-
|
|
130
|
-
config: IntorResolvedConfig;
|
|
131
|
-
adapter?: (config: IntorResolvedConfig) => Promise<AdapterRuntime>;
|
|
132
|
-
adapterRuntime?: Partial<AdapterRuntime>;
|
|
133
|
-
}
|
|
146
|
+
type GetI18nContext = (config: IntorResolvedConfig) => Promise<I18nContext>;
|
|
134
147
|
interface IntorResult {
|
|
135
148
|
config: IntorResolvedConfig;
|
|
136
149
|
initialLocale: Locale;
|
|
@@ -141,23 +154,28 @@ interface IntorResult {
|
|
|
141
154
|
/**
|
|
142
155
|
* Entry point for initializing Intor.
|
|
143
156
|
*
|
|
144
|
-
* 1. Resolve
|
|
145
|
-
* 2. Load messages if loader enabled
|
|
146
|
-
* 3. Merge static
|
|
157
|
+
* 1. Resolve context via adapter or fallback values.
|
|
158
|
+
* 2. Load messages if loader is enabled.
|
|
159
|
+
* 3. Merge static messages with loaded messages.
|
|
147
160
|
*/
|
|
148
|
-
declare const intor: (
|
|
161
|
+
declare const intor: (config: IntorResolvedConfig, i18nContext: GetI18nContext | Partial<I18nContext>) => Promise<IntorResult>;
|
|
149
162
|
|
|
150
163
|
type MessagesLoaderOptions = {
|
|
151
164
|
config: IntorResolvedConfig;
|
|
152
165
|
locale: Locale;
|
|
153
166
|
pathname: string;
|
|
154
167
|
};
|
|
155
|
-
type MessagesLoaderResult = Promise<
|
|
168
|
+
type MessagesLoaderResult<C extends GenConfigKeys = "__default__"> = Promise<GenMessages<C> | undefined>;
|
|
156
169
|
|
|
157
170
|
/**
|
|
158
|
-
*
|
|
171
|
+
* Load messages for a given locale and pathname.
|
|
172
|
+
*
|
|
173
|
+
* - Resolve namespaces based on config and pathname.
|
|
174
|
+
* - Support both **local import** and **remote API** loaders.
|
|
175
|
+
* - Apply fallback locales if needed.
|
|
176
|
+
* - Cache messages if enabled (handled by underlying loader, not this function directly).
|
|
159
177
|
*/
|
|
160
|
-
declare const
|
|
178
|
+
declare const loadMessages: <C extends GenConfigKeys = "__default__">({ config, locale, pathname, }: MessagesLoaderOptions) => MessagesLoaderResult<C>;
|
|
161
179
|
|
|
162
180
|
interface LoadLocalMessagesOptions {
|
|
163
181
|
basePath?: string | null;
|
|
@@ -171,6 +189,14 @@ interface LoadLocalMessagesOptions {
|
|
|
171
189
|
};
|
|
172
190
|
}
|
|
173
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Load local messages from the file system.
|
|
194
|
+
*
|
|
195
|
+
* - Load messages for a target locale with optional fallback locales.
|
|
196
|
+
* - Support filtering by specific namespaces.
|
|
197
|
+
* - Cache messages if enabled.
|
|
198
|
+
* - Limit concurrent file reads for performance.
|
|
199
|
+
*/
|
|
174
200
|
declare const loadLocalMessages: ({ basePath, locale, fallbackLocales, namespaces, concurrency, cache, logger: loggerOptions, }: LoadLocalMessagesOptions) => Promise<LocaleNamespaceMessages>;
|
|
175
201
|
|
|
176
202
|
interface LoadApiMessagesOptions extends Omit<ApiLoader, "type"> {
|
|
@@ -184,10 +210,49 @@ interface LoadApiMessagesOptions extends Omit<ApiLoader, "type"> {
|
|
|
184
210
|
|
|
185
211
|
/**
|
|
186
212
|
* Load locale messages from a remote API.
|
|
213
|
+
*
|
|
214
|
+
* - Fetch messages for a target locale with optional fallback locales.
|
|
215
|
+
* - Cache messages if enabled.
|
|
187
216
|
*/
|
|
188
217
|
declare const loadApiMessages: <Messages extends LocaleNamespaceMessages>({ apiUrl, apiHeaders, basePath, locale, fallbackLocales, namespaces, cache, logger: loggerOptions, }: LoadApiMessagesOptions) => Promise<Messages | undefined>;
|
|
189
218
|
|
|
190
|
-
|
|
219
|
+
type PreKey<C extends GenConfigKeys = "__default__"> = NodeKeys<UnionLocaleMessages<GenMessages<C>>>;
|
|
220
|
+
interface TranslatorBaseProps<M> {
|
|
221
|
+
messages: M;
|
|
222
|
+
locale: LocaleKey<M>;
|
|
223
|
+
}
|
|
224
|
+
interface TranslatorClientProps<M> {
|
|
225
|
+
isLoading: boolean;
|
|
226
|
+
setLocale: (locale: LocaleKey<M>) => void;
|
|
227
|
+
}
|
|
228
|
+
type TranslatorInstance<M> = {
|
|
229
|
+
hasKey: (key?: IfGen<InferTranslatorKey<M>, string>, targetLocale?: LocaleKey<M> | undefined) => boolean;
|
|
230
|
+
t: <Result = string>(key?: IfGen<InferTranslatorKey<M>, string>, replacements?: Replacement | RichReplacement) => Result;
|
|
231
|
+
} & TranslatorBaseProps<M> & TranslatorClientProps<M>;
|
|
232
|
+
type ScopedTranslatorInstance<M, K extends string> = {
|
|
233
|
+
hasKey: (key?: IfGen<ScopedLeafKeys<M, K> & string, string>, targetLocale?: LocaleKey<M>) => boolean;
|
|
234
|
+
t: (key?: IfGen<ScopedLeafKeys<M, K> & string, string>, replacements?: Replacement | RichReplacement) => string;
|
|
235
|
+
} & TranslatorBaseProps<M> & TranslatorClientProps<M>;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Create a translator instance for a specific locale and pathname
|
|
239
|
+
*
|
|
240
|
+
* - Loads messages using the provided config, locale, and pathname.
|
|
241
|
+
* - Initializes a translator with `t`, `hasKey`, and optional scoped methods.
|
|
242
|
+
* - Supports optional `preKey` to create a scoped translator for nested keys.
|
|
243
|
+
* - Passes additional options to the underlying `Translator`.
|
|
244
|
+
*/
|
|
245
|
+
declare function getTranslator<C extends GenConfigKeys = "__default__">(opts: {
|
|
246
|
+
config: IntorResolvedConfig;
|
|
247
|
+
locale: LocaleKey<GenMessages<C>>;
|
|
248
|
+
pathname?: string;
|
|
249
|
+
}): Promise<TranslatorInstance<GenMessages<C>>>;
|
|
250
|
+
declare function getTranslator<C extends GenConfigKeys = "__default__", K extends PreKey<C> = PreKey<C>>(opts: {
|
|
251
|
+
config: IntorResolvedConfig;
|
|
252
|
+
locale: LocaleKey<GenMessages<C>>;
|
|
253
|
+
pathname?: string;
|
|
254
|
+
preKey?: K;
|
|
255
|
+
}): Promise<ScopedTranslatorInstance<GenMessages<C>, K>>;
|
|
191
256
|
|
|
192
257
|
/**
|
|
193
258
|
* Merge static and loaded namespace messages by locale.
|
|
@@ -303,4 +368,4 @@ declare global {
|
|
|
303
368
|
*/
|
|
304
369
|
declare function clearMessagesPool(): void;
|
|
305
370
|
|
|
306
|
-
export { type
|
|
371
|
+
export { type I18nContext, type IntorResult, type LoadApiMessagesOptions, type LoadLocalMessagesOptions, PREFIX_PLACEHOLDER, clearLoggerPool, clearMessagesPool, extractPathname, getTranslator, intor, loadApiMessages, loadLocalMessages, loadMessages, mergeMessages, normalizeCacheKey, normalizeLocale, normalizePathname, resolveNamespaces, resolvePreferredLocale, standardizePathname };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Locale, LocaleNamespaceMessages, FallbackLocalesMap } from 'intor-translator';
|
|
2
|
-
export { FallbackLocalesMap, FormatMessage, Locale, LocaleKey, LocaleNamespaceMessages, Message, MessageRecord, Namespace, NamespaceMessages, NestedMessage, OnLoading, OnMissing, Replacement, RichReplacement, StrictLocaleKey, TranslateContext, TranslateHandlers, Translator, UnionLocaleMessages } from 'intor-translator';
|
|
3
1
|
import { Level, NormalizerConfig, FormatterConfig, LoggerPreset } from 'logry/edge';
|
|
2
|
+
import { Locale, LocaleNamespaceMessages, FallbackLocalesMap, InferTranslatorKey, LocaleKey, Replacement, RichReplacement, NodeKeys, UnionLocaleMessages, ScopedLeafKeys } from 'intor-translator';
|
|
3
|
+
export { FallbackLocalesMap, FormatMessage, Locale, LocaleKey, LocaleNamespaceMessages, Message, MessageRecord, Namespace, NamespaceMessages, NestedMessage, OnLoading, OnMissing, Replacement, RichReplacement, StrictLocaleKey, TranslateContext, TranslateHandlers, Translator, UnionLocaleMessages } from 'intor-translator';
|
|
4
4
|
import { Logger } from 'logry';
|
|
5
5
|
import Keyv from 'keyv';
|
|
6
6
|
|
|
@@ -122,15 +122,28 @@ type IntorResolvedConfig = (WithLoader | WithoutLoader) & {
|
|
|
122
122
|
readonly cache: CacheResolvedOptions;
|
|
123
123
|
};
|
|
124
124
|
|
|
125
|
-
|
|
126
|
-
|
|
125
|
+
declare const PREFIX_PLACEHOLDER = "{locale}";
|
|
126
|
+
|
|
127
|
+
type IfGen<Then, Else = never> = IntorGeneratedTypes extends void ? Else : Then;
|
|
128
|
+
type GenConfigKeys = IfGen<keyof IntorGeneratedTypes, string>;
|
|
129
|
+
type GenConfig<C extends GenConfigKeys = "__default__"> = IntorGeneratedTypes extends void ? {
|
|
130
|
+
Locales: string;
|
|
131
|
+
Messages: LocaleNamespaceMessages;
|
|
132
|
+
} : C extends keyof IntorGeneratedTypes ? {
|
|
133
|
+
Locales: IntorGeneratedTypes[C]["Locales"];
|
|
134
|
+
Messages: {
|
|
135
|
+
[K in IntorGeneratedTypes[C]["Locales"]]: IntorGeneratedTypes[C]["Messages"][typeof PREFIX_PLACEHOLDER];
|
|
136
|
+
};
|
|
137
|
+
} : never;
|
|
138
|
+
type GenMessages<C extends GenConfigKeys = "__default__"> = GenConfig<C>["Messages"];
|
|
139
|
+
type GenLocaleFallback = Locale;
|
|
140
|
+
type GenLocale<Config extends string = "__default__"> = Config extends keyof IntorGeneratedTypes ? IntorGeneratedTypes[Config]["Locales"] : GenLocaleFallback;
|
|
141
|
+
|
|
142
|
+
interface I18nContext {
|
|
143
|
+
locale: GenLocale;
|
|
127
144
|
pathname: string;
|
|
128
145
|
}
|
|
129
|
-
|
|
130
|
-
config: IntorResolvedConfig;
|
|
131
|
-
adapter?: (config: IntorResolvedConfig) => Promise<AdapterRuntime>;
|
|
132
|
-
adapterRuntime?: Partial<AdapterRuntime>;
|
|
133
|
-
}
|
|
146
|
+
type GetI18nContext = (config: IntorResolvedConfig) => Promise<I18nContext>;
|
|
134
147
|
interface IntorResult {
|
|
135
148
|
config: IntorResolvedConfig;
|
|
136
149
|
initialLocale: Locale;
|
|
@@ -141,23 +154,28 @@ interface IntorResult {
|
|
|
141
154
|
/**
|
|
142
155
|
* Entry point for initializing Intor.
|
|
143
156
|
*
|
|
144
|
-
* 1. Resolve
|
|
145
|
-
* 2. Load messages if loader enabled
|
|
146
|
-
* 3. Merge static
|
|
157
|
+
* 1. Resolve context via adapter or fallback values.
|
|
158
|
+
* 2. Load messages if loader is enabled.
|
|
159
|
+
* 3. Merge static messages with loaded messages.
|
|
147
160
|
*/
|
|
148
|
-
declare const intor: (
|
|
161
|
+
declare const intor: (config: IntorResolvedConfig, i18nContext: GetI18nContext | Partial<I18nContext>) => Promise<IntorResult>;
|
|
149
162
|
|
|
150
163
|
type MessagesLoaderOptions = {
|
|
151
164
|
config: IntorResolvedConfig;
|
|
152
165
|
locale: Locale;
|
|
153
166
|
pathname: string;
|
|
154
167
|
};
|
|
155
|
-
type MessagesLoaderResult = Promise<
|
|
168
|
+
type MessagesLoaderResult<C extends GenConfigKeys = "__default__"> = Promise<GenMessages<C> | undefined>;
|
|
156
169
|
|
|
157
170
|
/**
|
|
158
|
-
*
|
|
171
|
+
* Load messages for a given locale and pathname.
|
|
172
|
+
*
|
|
173
|
+
* - Resolve namespaces based on config and pathname.
|
|
174
|
+
* - Support both **local import** and **remote API** loaders.
|
|
175
|
+
* - Apply fallback locales if needed.
|
|
176
|
+
* - Cache messages if enabled (handled by underlying loader, not this function directly).
|
|
159
177
|
*/
|
|
160
|
-
declare const
|
|
178
|
+
declare const loadMessages: <C extends GenConfigKeys = "__default__">({ config, locale, pathname, }: MessagesLoaderOptions) => MessagesLoaderResult<C>;
|
|
161
179
|
|
|
162
180
|
interface LoadLocalMessagesOptions {
|
|
163
181
|
basePath?: string | null;
|
|
@@ -171,6 +189,14 @@ interface LoadLocalMessagesOptions {
|
|
|
171
189
|
};
|
|
172
190
|
}
|
|
173
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Load local messages from the file system.
|
|
194
|
+
*
|
|
195
|
+
* - Load messages for a target locale with optional fallback locales.
|
|
196
|
+
* - Support filtering by specific namespaces.
|
|
197
|
+
* - Cache messages if enabled.
|
|
198
|
+
* - Limit concurrent file reads for performance.
|
|
199
|
+
*/
|
|
174
200
|
declare const loadLocalMessages: ({ basePath, locale, fallbackLocales, namespaces, concurrency, cache, logger: loggerOptions, }: LoadLocalMessagesOptions) => Promise<LocaleNamespaceMessages>;
|
|
175
201
|
|
|
176
202
|
interface LoadApiMessagesOptions extends Omit<ApiLoader, "type"> {
|
|
@@ -184,10 +210,49 @@ interface LoadApiMessagesOptions extends Omit<ApiLoader, "type"> {
|
|
|
184
210
|
|
|
185
211
|
/**
|
|
186
212
|
* Load locale messages from a remote API.
|
|
213
|
+
*
|
|
214
|
+
* - Fetch messages for a target locale with optional fallback locales.
|
|
215
|
+
* - Cache messages if enabled.
|
|
187
216
|
*/
|
|
188
217
|
declare const loadApiMessages: <Messages extends LocaleNamespaceMessages>({ apiUrl, apiHeaders, basePath, locale, fallbackLocales, namespaces, cache, logger: loggerOptions, }: LoadApiMessagesOptions) => Promise<Messages | undefined>;
|
|
189
218
|
|
|
190
|
-
|
|
219
|
+
type PreKey<C extends GenConfigKeys = "__default__"> = NodeKeys<UnionLocaleMessages<GenMessages<C>>>;
|
|
220
|
+
interface TranslatorBaseProps<M> {
|
|
221
|
+
messages: M;
|
|
222
|
+
locale: LocaleKey<M>;
|
|
223
|
+
}
|
|
224
|
+
interface TranslatorClientProps<M> {
|
|
225
|
+
isLoading: boolean;
|
|
226
|
+
setLocale: (locale: LocaleKey<M>) => void;
|
|
227
|
+
}
|
|
228
|
+
type TranslatorInstance<M> = {
|
|
229
|
+
hasKey: (key?: IfGen<InferTranslatorKey<M>, string>, targetLocale?: LocaleKey<M> | undefined) => boolean;
|
|
230
|
+
t: <Result = string>(key?: IfGen<InferTranslatorKey<M>, string>, replacements?: Replacement | RichReplacement) => Result;
|
|
231
|
+
} & TranslatorBaseProps<M> & TranslatorClientProps<M>;
|
|
232
|
+
type ScopedTranslatorInstance<M, K extends string> = {
|
|
233
|
+
hasKey: (key?: IfGen<ScopedLeafKeys<M, K> & string, string>, targetLocale?: LocaleKey<M>) => boolean;
|
|
234
|
+
t: (key?: IfGen<ScopedLeafKeys<M, K> & string, string>, replacements?: Replacement | RichReplacement) => string;
|
|
235
|
+
} & TranslatorBaseProps<M> & TranslatorClientProps<M>;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Create a translator instance for a specific locale and pathname
|
|
239
|
+
*
|
|
240
|
+
* - Loads messages using the provided config, locale, and pathname.
|
|
241
|
+
* - Initializes a translator with `t`, `hasKey`, and optional scoped methods.
|
|
242
|
+
* - Supports optional `preKey` to create a scoped translator for nested keys.
|
|
243
|
+
* - Passes additional options to the underlying `Translator`.
|
|
244
|
+
*/
|
|
245
|
+
declare function getTranslator<C extends GenConfigKeys = "__default__">(opts: {
|
|
246
|
+
config: IntorResolvedConfig;
|
|
247
|
+
locale: LocaleKey<GenMessages<C>>;
|
|
248
|
+
pathname?: string;
|
|
249
|
+
}): Promise<TranslatorInstance<GenMessages<C>>>;
|
|
250
|
+
declare function getTranslator<C extends GenConfigKeys = "__default__", K extends PreKey<C> = PreKey<C>>(opts: {
|
|
251
|
+
config: IntorResolvedConfig;
|
|
252
|
+
locale: LocaleKey<GenMessages<C>>;
|
|
253
|
+
pathname?: string;
|
|
254
|
+
preKey?: K;
|
|
255
|
+
}): Promise<ScopedTranslatorInstance<GenMessages<C>, K>>;
|
|
191
256
|
|
|
192
257
|
/**
|
|
193
258
|
* Merge static and loaded namespace messages by locale.
|
|
@@ -303,4 +368,4 @@ declare global {
|
|
|
303
368
|
*/
|
|
304
369
|
declare function clearMessagesPool(): void;
|
|
305
370
|
|
|
306
|
-
export { type
|
|
371
|
+
export { type I18nContext, type IntorResult, type LoadApiMessagesOptions, type LoadLocalMessagesOptions, PREFIX_PLACEHOLDER, clearLoggerPool, clearMessagesPool, extractPathname, getTranslator, intor, loadApiMessages, loadLocalMessages, loadMessages, mergeMessages, normalizeCacheKey, normalizeLocale, normalizePathname, resolveNamespaces, resolvePreferredLocale, standardizePathname };
|