@fluentic/i18n 0.1.0-beta.1 → 0.1.0-beta.2
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/CHANGELOG.md +13 -0
- package/README.md +9 -1
- package/dist/adapter.d.ts +6 -4
- package/dist/adapter.js +4 -3
- package/dist/compiled.d.ts +26 -24
- package/dist/compiled.js +4 -2
- package/dist/compiled.js.map +1 -1
- package/dist/compiler-support.d.ts +4 -0
- package/dist/compiler-support.js +4 -0
- package/dist/core/format.d.ts +11 -0
- package/dist/core/i18n.d.ts +26 -0
- package/dist/core/index.d.ts +1 -5
- package/dist/core/index.js +3 -2
- package/dist/core/types.d.ts +79 -0
- package/dist/hash-VJlyT31Z.js +90 -0
- package/dist/hash-VJlyT31Z.js.map +1 -0
- package/dist/hydration-store-BK6UxrtJ.js +13 -0
- package/dist/hydration-store-BK6UxrtJ.js.map +1 -0
- package/dist/hydration-store.d.ts +29 -0
- package/dist/hydration.d.ts +7 -0
- package/dist/hydration.js +35 -0
- package/dist/hydration.js.map +1 -0
- package/dist/index.d.ts +6 -6
- package/dist/index.js +3 -2
- package/dist/{interpreter-Dld8QyOI.js → interpreter-BfzvLLrD.js} +4 -3
- package/dist/interpreter-BfzvLLrD.js.map +1 -0
- package/dist/{manager-BW778X1Y.js → manager-BgMJWXWY.js} +21 -31
- package/dist/manager-BgMJWXWY.js.map +1 -0
- package/dist/manager.d.ts +62 -0
- package/dist/message-D4jK2Lzc.js +9 -0
- package/dist/message-D4jK2Lzc.js.map +1 -0
- package/dist/react.d.ts +41 -46
- package/dist/react.js +12 -8
- package/dist/react.js.map +1 -1
- package/dist/registry-manager.d.ts +7 -0
- package/dist/runtime-DXnGeCBw.js +110 -0
- package/dist/runtime-DXnGeCBw.js.map +1 -0
- package/dist/shared/compact.d.ts +5 -0
- package/dist/shared/formatting.d.ts +11 -0
- package/dist/shared/hash.d.ts +7 -0
- package/dist/shared/interpreter.d.ts +2 -0
- package/dist/shared/message.d.ts +2 -0
- package/dist/shared/runtime.d.ts +49 -0
- package/dist/{src-BovRx-mX.js → src-ON68XIog.js} +9 -7
- package/dist/src-ON68XIog.js.map +1 -0
- package/dist/state-manager.d.ts +6 -0
- package/dist/state.d.ts +8 -0
- package/dist/storage-manager.d.ts +14 -0
- package/dist/{translation-manager-C79eKu2K.js → translation-manager-BkmFnlb_.js} +5 -104
- package/dist/translation-manager-BkmFnlb_.js.map +1 -0
- package/dist/translation-manager.d.ts +34 -0
- package/dist/translation.d.ts +20 -0
- package/dist/types-BkDsJfVD.js.map +1 -1
- package/dist/types.d.ts +3 -2
- package/package.json +20 -54
- package/dist/adapter-CWC8rng-.d.ts +0 -178
- package/dist/analyze-CdXCxQDg.d.ts +0 -14
- package/dist/compact-CJ0OWoIv.d.ts +0 -7
- package/dist/compiler-BZtB9WLi.js +0 -365
- package/dist/compiler-BZtB9WLi.js.map +0 -1
- package/dist/compiler.d.ts +0 -35
- package/dist/compiler.js +0 -3
- package/dist/i18n-CfYNwubL.d.ts +0 -25
- package/dist/interpreter-Dld8QyOI.js.map +0 -1
- package/dist/manager-BW778X1Y.js.map +0 -1
- package/dist/options-DE7vYj0e.d.ts +0 -7
- package/dist/options-Dxxx0aOz.js +0 -23
- package/dist/options-Dxxx0aOz.js.map +0 -1
- package/dist/plugin/babel.d.ts +0 -11
- package/dist/plugin/babel.js +0 -31
- package/dist/plugin/babel.js.map +0 -1
- package/dist/plugin/esbuild.d.ts +0 -7
- package/dist/plugin/esbuild.js +0 -37
- package/dist/plugin/esbuild.js.map +0 -1
- package/dist/plugin/rollup.d.ts +0 -30
- package/dist/plugin/rollup.js +0 -34
- package/dist/plugin/rollup.js.map +0 -1
- package/dist/plugin/vite.d.ts +0 -31
- package/dist/plugin/vite.js +0 -11
- package/dist/plugin/vite.js.map +0 -1
- package/dist/plugin/webpack-loader.d.ts +0 -3
- package/dist/plugin/webpack-loader.js +0 -21
- package/dist/plugin/webpack-loader.js.map +0 -1
- package/dist/plugin/webpack.d.ts +0 -8
- package/dist/plugin/webpack.js +0 -22
- package/dist/plugin/webpack.js.map +0 -1
- package/dist/src-BovRx-mX.js.map +0 -1
- package/dist/translation-manager-C79eKu2K.js.map +0 -1
- package/dist/types-BuGoCU3P.d.ts +0 -75
- package/dist-cjs/babel.cjs +0 -379
- package/dist-cjs/babel.cjs.map +0 -1
- package/src/adapter.ts +0 -6
- package/src/compiled.ts +0 -116
- package/src/compiler/analyze.ts +0 -182
- package/src/compiler/index.ts +0 -208
- package/src/compiler/message.ts +0 -74
- package/src/core/format.ts +0 -40
- package/src/core/i18n.ts +0 -124
- package/src/core/index.ts +0 -1
- package/src/core/types.ts +0 -121
- package/src/index.ts +0 -9
- package/src/manager.ts +0 -175
- package/src/plugin/babel.ts +0 -34
- package/src/plugin/esbuild.ts +0 -45
- package/src/plugin/options.ts +0 -24
- package/src/plugin/rollup.ts +0 -29
- package/src/plugin/vite.ts +0 -6
- package/src/plugin/webpack-loader.ts +0 -22
- package/src/plugin/webpack.ts +0 -15
- package/src/react.ts +0 -97
- package/src/registry-manager.ts +0 -3
- package/src/shared/compact.ts +0 -51
- package/src/shared/formatting.ts +0 -121
- package/src/shared/interpreter.ts +0 -128
- package/src/shared/runtime.ts +0 -131
- package/src/state-manager.ts +0 -29
- package/src/state.ts +0 -20
- package/src/storage-manager.ts +0 -47
- package/src/translation-manager.ts +0 -82
- package/src/translation.ts +0 -106
- package/src/types.ts +0 -2
package/src/core/types.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import type { CompactMessage } from '../shared/compact';
|
|
2
|
-
import type { I18nJsxConfig } from '../types';
|
|
3
|
-
|
|
4
|
-
export type { I18nJsxConfig } from '../types';
|
|
5
|
-
|
|
6
|
-
export type I18nJsxOutput = I18nJsxConfig extends { JSX: infer Output; } ? NonNullable<Output> : unknown;
|
|
7
|
-
export type I18nTagElement = I18nJsxConfig extends { Element: infer Element; } ? Element : never;
|
|
8
|
-
export type I18nTagValue = I18nJsxConfig extends { Value: infer Value; } ? Value : unknown;
|
|
9
|
-
export type PrimitiveType = string | number | boolean | null | undefined;
|
|
10
|
-
|
|
11
|
-
export type LanguageId = string;
|
|
12
|
-
export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue; };
|
|
13
|
-
export type MessageMeta = JsonValue;
|
|
14
|
-
export type MessageContext = string;
|
|
15
|
-
|
|
16
|
-
export type MessageSchemaValue = StringConstructor | NumberConstructor | BooleanConstructor | DateConstructor;
|
|
17
|
-
export type MessageSchema = Partial<Record<string, MessageSchemaValue>>;
|
|
18
|
-
|
|
19
|
-
export type InferMessageSchema<Schema extends MessageSchema> = {
|
|
20
|
-
[Key in keyof Schema]: Schema[Key] extends StringConstructor ? string
|
|
21
|
-
: Schema[Key] extends NumberConstructor ? number
|
|
22
|
-
: Schema[Key] extends BooleanConstructor ? boolean
|
|
23
|
-
: Schema[Key] extends DateConstructor ? Date
|
|
24
|
-
: never;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export type MessageDescriptor<Text extends string = string, Schema extends MessageSchema | undefined = MessageSchema> =
|
|
28
|
-
{
|
|
29
|
-
readonly __fluenticMessage: true;
|
|
30
|
-
readonly text: Text;
|
|
31
|
-
readonly compiled?: readonly [string, CompactMessage];
|
|
32
|
-
readonly context: MessageContext | null;
|
|
33
|
-
readonly schema: Schema extends MessageSchema ? Schema : MessageSchema;
|
|
34
|
-
readonly notes: readonly string[];
|
|
35
|
-
readonly meta: MessageMeta | undefined;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export type AnyMessageDescriptor = MessageDescriptor<string, MessageSchema | undefined>;
|
|
39
|
-
|
|
40
|
-
export type MessageValueContext = { languageId: LanguageId; };
|
|
41
|
-
export type MessageValueFactory<T = unknown> = (context: MessageValueContext) => T;
|
|
42
|
-
|
|
43
|
-
export type MessageTagHandler<T = unknown, R = unknown> = ((chunks: Array<string | T>) => R) & {
|
|
44
|
-
readonly __fluenticTagHandler: true;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export type MessageRuntimeValue =
|
|
48
|
-
| PrimitiveType
|
|
49
|
-
| Date
|
|
50
|
-
| AnyMessageDescriptor
|
|
51
|
-
| MessageValueFactory
|
|
52
|
-
| Pick<MessageTagHandler, '__fluenticTagHandler'>;
|
|
53
|
-
|
|
54
|
-
export type MessageParam = MessageRuntimeValue;
|
|
55
|
-
export type MessageParams = Record<string, MessageParam>;
|
|
56
|
-
export type MessageInput = string | AnyMessageDescriptor;
|
|
57
|
-
|
|
58
|
-
export type InferRuntimeParams<Schema extends MessageSchema> = {
|
|
59
|
-
[Key in keyof InferMessageSchema<Schema>]:
|
|
60
|
-
| InferMessageSchema<Schema>[Key]
|
|
61
|
-
| MessageValueFactory<InferMessageSchema<Schema>[Key]>;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export type DescriptorRenderArgs<Schema extends MessageSchema | undefined> = Schema extends undefined
|
|
65
|
-
? [params?: MessageParams]
|
|
66
|
-
: keyof Schema extends never ? []
|
|
67
|
-
: Schema extends MessageSchema ? [params: InferRuntimeParams<Schema>]
|
|
68
|
-
: never;
|
|
69
|
-
|
|
70
|
-
export type TryRenderArgs<Value> = Value extends { readonly __fluenticMessage: true; }
|
|
71
|
-
? Value extends MessageDescriptor<string, infer Schema>
|
|
72
|
-
? Schema extends MessageSchema | undefined ? DescriptorRenderArgs<Schema>
|
|
73
|
-
: [params: MessageParams]
|
|
74
|
-
: [params: MessageParams]
|
|
75
|
-
: [params?: MessageParams];
|
|
76
|
-
|
|
77
|
-
export type TextRenderer = {
|
|
78
|
-
<Text extends string>(message: Text, params?: MessageParams): string;
|
|
79
|
-
<Text extends string, Schema extends MessageSchema | undefined>(
|
|
80
|
-
message: MessageDescriptor<Text, Schema>,
|
|
81
|
-
...args: DescriptorRenderArgs<Schema>
|
|
82
|
-
): string;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
export type JsxRenderer<Output = unknown> = {
|
|
86
|
-
<Text extends string>(message: Text, params?: MessageParams): Output;
|
|
87
|
-
<Text extends string, Schema extends MessageSchema | undefined>(
|
|
88
|
-
message: MessageDescriptor<Text, Schema>,
|
|
89
|
-
...args: DescriptorRenderArgs<Schema>
|
|
90
|
-
): Output;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
export type MessageFormatOptions = {
|
|
94
|
-
msg: MessageInput;
|
|
95
|
-
params?: MessageParams;
|
|
96
|
-
languageId?: LanguageId;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
export type GetJsxHandler<Output = unknown> = (options: MessageFormatOptions) => Output;
|
|
100
|
-
|
|
101
|
-
export type CompiledTranslation = { compiled: CompactMessage; };
|
|
102
|
-
|
|
103
|
-
export type TranslationGetter = (args: {
|
|
104
|
-
languageId: LanguageId;
|
|
105
|
-
context: MessageContext | null;
|
|
106
|
-
message: string;
|
|
107
|
-
getCompiledId?: () => string;
|
|
108
|
-
}) => CompactMessage | CompiledTranslation | null;
|
|
109
|
-
|
|
110
|
-
export function isMessageDescriptor(value: unknown): value is AnyMessageDescriptor {
|
|
111
|
-
return value !== null && typeof value === 'object' && '__fluenticMessage' in value &&
|
|
112
|
-
value.__fluenticMessage === true;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function isMessageTagHandler(value: unknown): value is MessageTagHandler {
|
|
116
|
-
return typeof value === 'function' && '__fluenticTagHandler' in value && value.__fluenticTagHandler === true;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export function isMessageSchemaValue(value: unknown): value is MessageSchemaValue {
|
|
120
|
-
return value === String || value === Number || value === Boolean || value === Date;
|
|
121
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import * as formatting from './core/format';
|
|
2
|
-
import { createT } from './core/i18n';
|
|
3
|
-
|
|
4
|
-
export const t = /* @__PURE__ */ createT(formatting);
|
|
5
|
-
export type { MessageApi, TApi } from './core/i18n';
|
|
6
|
-
export * from './core/types';
|
|
7
|
-
export { I18nRegistry, noTranslate, setFormats } from './shared/runtime';
|
|
8
|
-
export { defineTranslationLoader } from './translation-manager';
|
|
9
|
-
export type { TranslationData, TranslationItem, TranslationLoader, TranslationLoaderMap } from './translation-manager';
|
package/src/manager.ts
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { createRuntime, defaultRuntime, type Formats, getFormats, type Runtime } from './shared/runtime';
|
|
2
|
-
import { createStateManager } from './state-manager';
|
|
3
|
-
import { createStorageManager, type I18nStorage } from './storage-manager';
|
|
4
|
-
import { type Catalog, createTranslationManager, type TranslationLoader } from './translation-manager';
|
|
5
|
-
|
|
6
|
-
export type { I18nStorage, StoredState } from './storage-manager';
|
|
7
|
-
export { defineTranslationLoader } from './translation-manager';
|
|
8
|
-
export type * from './translation-manager';
|
|
9
|
-
|
|
10
|
-
export type StateData = { languageId: string; isLoading: boolean; ready: boolean; };
|
|
11
|
-
export type I18nManagerOptions = { languageId: string; fallbackLanguageId?: string; };
|
|
12
|
-
export type CreateI18nManagerOptions = { storage?: I18nStorage | null; runtime?: Runtime; };
|
|
13
|
-
export type GetTranslationArgs = { languageId?: string; context?: string | null; message: string; };
|
|
14
|
-
|
|
15
|
-
export type I18nSnapshot = {
|
|
16
|
-
languageId: string;
|
|
17
|
-
fallbackLanguageId?: string;
|
|
18
|
-
translations: Record<string, Catalog>;
|
|
19
|
-
formats?: Partial<Formats>;
|
|
20
|
-
compiledLanguages?: string[];
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export function createI18nManager(options: CreateI18nManagerOptions = {}) {
|
|
24
|
-
const runtime = options.runtime ?? createRuntime();
|
|
25
|
-
|
|
26
|
-
const state = createStateManager<StateData>({
|
|
27
|
-
languageId: runtime.languageId,
|
|
28
|
-
isLoading: false,
|
|
29
|
-
ready: runtime.ready,
|
|
30
|
-
});
|
|
31
|
-
const storage = createStorageManager(options.storage ?? null);
|
|
32
|
-
const translations = createTranslationManager();
|
|
33
|
-
|
|
34
|
-
let fallbackLanguageId: string | undefined;
|
|
35
|
-
let version = 0;
|
|
36
|
-
|
|
37
|
-
runtime.subscribe = state.subscribe;
|
|
38
|
-
runtime.getSnapshot = state.get;
|
|
39
|
-
|
|
40
|
-
/* configure */
|
|
41
|
-
|
|
42
|
-
const configure = (config: I18nManagerOptions) => {
|
|
43
|
-
runtime.languageId = config.languageId;
|
|
44
|
-
runtime.ready = false;
|
|
45
|
-
fallbackLanguageId = config.fallbackLanguageId;
|
|
46
|
-
version++;
|
|
47
|
-
state.set({ languageId: config.languageId, ready: false });
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const setTranslationLoader = (loader: TranslationLoader | null) => {
|
|
51
|
-
translations.setTranslationLoader(loader);
|
|
52
|
-
runtime.ready = false;
|
|
53
|
-
state.set({ ready: false });
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/* language and translations */
|
|
57
|
-
|
|
58
|
-
const init = async () => {
|
|
59
|
-
const saved = storage.hasStorage() ? await storage.getStorage().read() : null;
|
|
60
|
-
|
|
61
|
-
await loadTranslations(saved?.languageId ?? runtime.languageId);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const setLanguageId = async (languageId: string) => {
|
|
65
|
-
const committed = await loadTranslations(languageId);
|
|
66
|
-
|
|
67
|
-
if (committed && storage.hasStorage()) await storage.getStorage().write({ languageId });
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const getLanguageId = () => {
|
|
71
|
-
return runtime.languageId;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const getTranslation = (args: GetTranslationArgs) => {
|
|
75
|
-
for (const languageId of getLanguageIds(args.languageId ?? runtime.languageId)) {
|
|
76
|
-
const value = translations.getTranslation({ ...args, languageId, context: args.context ?? null });
|
|
77
|
-
|
|
78
|
-
if (value !== null) return value;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return null;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/* snapshots */
|
|
85
|
-
|
|
86
|
-
const getSnapshot = (): I18nSnapshot => {
|
|
87
|
-
return {
|
|
88
|
-
languageId: runtime.languageId,
|
|
89
|
-
fallbackLanguageId,
|
|
90
|
-
translations: Object.fromEntries(
|
|
91
|
-
Object.entries(translations.catalogs).map(([id, catalog]) => [id, { ...catalog }]),
|
|
92
|
-
),
|
|
93
|
-
formats: getFormats(runtime, runtime.languageId),
|
|
94
|
-
compiledLanguages: Object.keys(translations.compiledCatalogs).filter((id) => translations.compiledCatalogs[id]),
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const hydrate = (snapshot: I18nSnapshot) => {
|
|
99
|
-
version++;
|
|
100
|
-
|
|
101
|
-
for (const id of Object.keys(translations.catalogs)) delete translations.catalogs[id];
|
|
102
|
-
for (const id of Object.keys(translations.compiledCatalogs)) delete translations.compiledCatalogs[id];
|
|
103
|
-
for (const [id, catalog] of Object.entries(snapshot.translations)) {
|
|
104
|
-
translations.catalogs[id] = Object.assign(Object.create(null), catalog);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
for (const id of snapshot.compiledLanguages ?? []) translations.compiledCatalogs[id] = true;
|
|
108
|
-
|
|
109
|
-
runtime.formats = snapshot.formats ?? {};
|
|
110
|
-
runtime.languageId = snapshot.languageId;
|
|
111
|
-
fallbackLanguageId = snapshot.fallbackLanguageId;
|
|
112
|
-
runtime.ready = true;
|
|
113
|
-
state.set({ languageId: snapshot.languageId, ready: true, isLoading: false });
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
/* utils */
|
|
117
|
-
|
|
118
|
-
const getLanguageIds = (languageId: string) => {
|
|
119
|
-
const ids = fallbackLanguageId === undefined
|
|
120
|
-
? [languageId, languageId.split('-')[0]]
|
|
121
|
-
: [languageId, languageId.split('-')[0], fallbackLanguageId];
|
|
122
|
-
|
|
123
|
-
return [...new Set(ids)];
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
const loadTranslations = async (languageId: string) => {
|
|
127
|
-
const current = ++version;
|
|
128
|
-
state.set({ isLoading: true });
|
|
129
|
-
|
|
130
|
-
try {
|
|
131
|
-
await Promise.all(getLanguageIds(languageId).map(translations.loadTranslations));
|
|
132
|
-
if (version !== current) return false;
|
|
133
|
-
runtime.languageId = languageId;
|
|
134
|
-
runtime.ready = true;
|
|
135
|
-
state.set({ languageId, ready: true, isLoading: false });
|
|
136
|
-
|
|
137
|
-
return true;
|
|
138
|
-
} finally {
|
|
139
|
-
if (current === version) state.set({ isLoading: false });
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
const manager = {
|
|
144
|
-
configure,
|
|
145
|
-
setStorage: storage.setStorage,
|
|
146
|
-
setTranslationLoader,
|
|
147
|
-
init,
|
|
148
|
-
setLanguageId,
|
|
149
|
-
getLanguageId,
|
|
150
|
-
getTranslation,
|
|
151
|
-
getSnapshot,
|
|
152
|
-
hydrate,
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
runtime.translationGetter = (args) => {
|
|
156
|
-
for (const languageId of getLanguageIds(args.languageId)) {
|
|
157
|
-
const compiled = translations.compiledCatalogs[languageId];
|
|
158
|
-
const message = compiled && args.getCompiledId ? args.getCompiledId() : args.message;
|
|
159
|
-
const context = compiled && args.getCompiledId ? null : args.context;
|
|
160
|
-
const value = translations.getTranslation({ languageId, context, message });
|
|
161
|
-
|
|
162
|
-
if (value !== null) return compiled ? { compiled: value } : value;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return null;
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
return { manager, state, runtime };
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export type ManagedI18n = ReturnType<typeof createI18nManager>;
|
|
172
|
-
export type I18nManager = ManagedI18n['manager'];
|
|
173
|
-
export const defaultManaged = createI18nManager({ runtime: defaultRuntime });
|
|
174
|
-
export const I18nManager = defaultManaged.manager;
|
|
175
|
-
export const defaultI18nState = defaultManaged.state;
|
package/src/plugin/babel.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { compileProgram } from '../compiler';
|
|
2
|
-
import { traverse } from '../compiler/analyze';
|
|
3
|
-
import { loadOptions, type PluginOptions } from './options';
|
|
4
|
-
/** Owns parsing even when a host (e.g. Metro) passes an already parsed AST. */
|
|
5
|
-
export default function fluenticBabel(_api: unknown, options: PluginOptions = {}) {
|
|
6
|
-
const config = loadOptions(options);
|
|
7
|
-
const handled = new WeakSet<object>();
|
|
8
|
-
const enabled = (code: string) => config.compiled && code.includes('@fluentic/i18n');
|
|
9
|
-
|
|
10
|
-
const compile = (code: string, filename: string) => {
|
|
11
|
-
const { ast } = compileProgram(code, filename, config);
|
|
12
|
-
// Standalone paths have a default Hub. Babel must create its own paths for presets.
|
|
13
|
-
traverse.cache.clear();
|
|
14
|
-
handled.add(ast.program);
|
|
15
|
-
|
|
16
|
-
return ast;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
name: 'fluentic-i18n',
|
|
21
|
-
parserOverride(code: string, parserOptions: { sourceFilename?: string; }) {
|
|
22
|
-
if (!enabled(code)) return undefined;
|
|
23
|
-
|
|
24
|
-
return compile(code, parserOptions.sourceFilename ?? 'source.tsx');
|
|
25
|
-
},
|
|
26
|
-
pre(file: any) {
|
|
27
|
-
if (!enabled(file.code) || handled.has(file.path.node)) return;
|
|
28
|
-
|
|
29
|
-
const ast = compile(file.code, file.opts.filename ?? 'source.tsx');
|
|
30
|
-
file.path.replaceWith(ast.program);
|
|
31
|
-
file.scope.crawl();
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
}
|
package/src/plugin/esbuild.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import { transform } from '../compiler';
|
|
3
|
-
import { accepts, loadOptions, type PluginOptions } from './options';
|
|
4
|
-
|
|
5
|
-
export default function fluentic(options: PluginOptions = {}) {
|
|
6
|
-
return {
|
|
7
|
-
name: 'fluentic-i18n',
|
|
8
|
-
setup(build: any) {
|
|
9
|
-
let config = loadOptions(options, build.initialOptions.absWorkingDir);
|
|
10
|
-
build.onStart(() => {
|
|
11
|
-
config = loadOptions(options, build.initialOptions.absWorkingDir);
|
|
12
|
-
});
|
|
13
|
-
build.onLoad(
|
|
14
|
-
{ filter: /\.jsonl$/ },
|
|
15
|
-
async ({ path }: { path: string; }) => ({
|
|
16
|
-
contents: await readFile(path, 'utf8'),
|
|
17
|
-
loader: 'text',
|
|
18
|
-
watchFiles: [path],
|
|
19
|
-
}),
|
|
20
|
-
);
|
|
21
|
-
build.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, async ({ path }: { path: string; }) => {
|
|
22
|
-
if (!config.compiled || !accepts(path, config)) return;
|
|
23
|
-
|
|
24
|
-
const result = transform(await readFile(path, 'utf8'), { ...config, filename: path });
|
|
25
|
-
|
|
26
|
-
if (!result) return;
|
|
27
|
-
|
|
28
|
-
const loader = /\.tsx$/.test(path)
|
|
29
|
-
? 'tsx'
|
|
30
|
-
: /\.[cm]?ts$/.test(path)
|
|
31
|
-
? 'ts'
|
|
32
|
-
: /\.jsx$/.test(path)
|
|
33
|
-
? 'jsx'
|
|
34
|
-
: 'js';
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
contents: result.code + '\n//# sourceMappingURL=data:application/json;base64,' +
|
|
38
|
-
Buffer.from(JSON.stringify(result.map)).toString('base64'),
|
|
39
|
-
loader,
|
|
40
|
-
watchFiles: [path, ...(config.configFile ? [config.configFile] : [])],
|
|
41
|
-
};
|
|
42
|
-
});
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
}
|
package/src/plugin/options.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { createJiti } from 'jiti';
|
|
2
|
-
import { existsSync } from 'node:fs';
|
|
3
|
-
import { resolve } from 'node:path';
|
|
4
|
-
|
|
5
|
-
export type PluginOptions = { compiled?: boolean; config?: string; includeDependencies?: boolean; };
|
|
6
|
-
|
|
7
|
-
export function loadOptions(
|
|
8
|
-
options: PluginOptions = {},
|
|
9
|
-
root = process.cwd(),
|
|
10
|
-
): PluginOptions & { configFile?: string; } {
|
|
11
|
-
const file = options.config
|
|
12
|
-
? resolve(root, options.config)
|
|
13
|
-
: ['i18n.config.ts', 'i18n.config.js', 'i18n.config.mjs'].map((name) => resolve(root, name)).find(existsSync);
|
|
14
|
-
|
|
15
|
-
const config = file
|
|
16
|
-
? createJiti(import.meta.url, { moduleCache: false })(file) as { default?: PluginOptions; } & PluginOptions
|
|
17
|
-
: {};
|
|
18
|
-
|
|
19
|
-
return { ...((config.default ?? config) as PluginOptions), ...options, configFile: file };
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function accepts(id: string, options: PluginOptions) {
|
|
23
|
-
return /\.[cm]?[jt]sx?$/.test(id.split('?')[0]) && (options.includeDependencies || !id.includes('/node_modules/'));
|
|
24
|
-
}
|
package/src/plugin/rollup.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import { transform } from '../compiler';
|
|
3
|
-
import { accepts, loadOptions, type PluginOptions } from './options';
|
|
4
|
-
|
|
5
|
-
export default function fluentic(options: PluginOptions = {}) {
|
|
6
|
-
let config = loadOptions(options);
|
|
7
|
-
|
|
8
|
-
return {
|
|
9
|
-
name: 'fluentic-i18n',
|
|
10
|
-
buildStart(this: { addWatchFile(file: string): void; }) {
|
|
11
|
-
if (config.configFile) this.addWatchFile(config.configFile);
|
|
12
|
-
},
|
|
13
|
-
watchChange(id: string) {
|
|
14
|
-
if (id === config.configFile) config = loadOptions(options);
|
|
15
|
-
},
|
|
16
|
-
async load(id: string) {
|
|
17
|
-
if (!id.endsWith('.jsonl')) return null;
|
|
18
|
-
|
|
19
|
-
const content = await readFile(id, 'utf8');
|
|
20
|
-
|
|
21
|
-
return { code: 'export default ' + JSON.stringify(content), map: { mappings: '' } };
|
|
22
|
-
},
|
|
23
|
-
transform(code: string, id: string) {
|
|
24
|
-
if (!config.compiled || !accepts(id, config)) return null;
|
|
25
|
-
|
|
26
|
-
return transform(code, { ...config, filename: id.split('?')[0] });
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
}
|
package/src/plugin/vite.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import remapping from '@jridgewell/remapping';
|
|
2
|
-
import { transform } from '../compiler';
|
|
3
|
-
import { loadOptions } from './options';
|
|
4
|
-
|
|
5
|
-
export default function loader(this: any, code: string, inputMap: unknown) {
|
|
6
|
-
this.cacheable?.();
|
|
7
|
-
const options = loadOptions(this.getOptions?.() ?? {}, this.rootContext);
|
|
8
|
-
|
|
9
|
-
if (options.configFile) this.addDependency(options.configFile);
|
|
10
|
-
if (this.resourcePath.endsWith('.jsonl')) {
|
|
11
|
-
this.callback(null, 'export default ' + JSON.stringify(code));
|
|
12
|
-
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const result = options.compiled ? transform(code, { filename: this.resourcePath, ...options }) : null;
|
|
17
|
-
this.callback(
|
|
18
|
-
null,
|
|
19
|
-
result?.code ?? code,
|
|
20
|
-
result?.map && inputMap ? remapping([result.map as any, inputMap as any], () => null) : result?.map ?? inputMap,
|
|
21
|
-
);
|
|
22
|
-
}
|
package/src/plugin/webpack.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';
|
|
2
|
-
import type { PluginOptions } from './options';
|
|
3
|
-
|
|
4
|
-
const require = createRequire(import.meta.url);
|
|
5
|
-
export default class FluenticI18nPlugin {
|
|
6
|
-
constructor(private options: PluginOptions = {}) {}
|
|
7
|
-
apply(compiler: any) {
|
|
8
|
-
compiler.options.module.rules.push({
|
|
9
|
-
test: /(?:\.[cm]?[jt]sx?|\.jsonl)$/,
|
|
10
|
-
enforce: 'pre',
|
|
11
|
-
...(this.options.includeDependencies ? {} : { exclude: /node_modules/ }),
|
|
12
|
-
use: [{ loader: require.resolve('@fluentic/i18n/plugin/webpack-loader'), options: this.options }],
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
}
|
package/src/react.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import React, { createContext, useContext, useRef, useSyncExternalStore } from 'react';
|
|
2
|
-
import type { MessageFormatOptions } from './core/types';
|
|
3
|
-
import { createI18nManager, defaultManaged, type I18nSnapshot, type ManagedI18n } from './manager';
|
|
4
|
-
import { defaultRuntime, getRuntime, type PartsRenderer, type Runtime, withRuntime } from './shared/runtime';
|
|
5
|
-
|
|
6
|
-
declare module '@fluentic/i18n/types' {
|
|
7
|
-
interface I18nJsxConfig {
|
|
8
|
-
JSX: React.ReactElement;
|
|
9
|
-
Element: React.ReactElement;
|
|
10
|
-
Value: React.ReactNode;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
const Context = createContext<ManagedI18n | null>(null);
|
|
14
|
-
|
|
15
|
-
function TranslatedMessage({ render }: { render: PartsRenderer; }) {
|
|
16
|
-
const managed = useContext(Context);
|
|
17
|
-
const runtime = managed?.runtime ?? getRuntime();
|
|
18
|
-
useSyncExternalStore(runtime.subscribe, runtime.getSnapshot, runtime.getSnapshot);
|
|
19
|
-
|
|
20
|
-
return React.createElement(
|
|
21
|
-
React.Fragment,
|
|
22
|
-
null,
|
|
23
|
-
...render(runtime).map((part, index) =>
|
|
24
|
-
React.createElement(React.Fragment, { key: index }, part as React.ReactNode)
|
|
25
|
-
),
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function installReactAdapter(runtime: Runtime, lineBreak: React.ReactNode = React.createElement('br')): void {
|
|
30
|
-
runtime.jsxLineBreak = lineBreak;
|
|
31
|
-
runtime.jsxHandler = (_options: MessageFormatOptions, render: PartsRenderer) =>
|
|
32
|
-
React.createElement(TranslatedMessage, { render });
|
|
33
|
-
runtime.tagElement = (element) => {
|
|
34
|
-
if (!React.isValidElement(element)) throw new TypeError('[i18n] t.tag expects a handler or React element');
|
|
35
|
-
|
|
36
|
-
return (chunks) => React.cloneElement(element, undefined, ...chunks as React.ReactNode[]);
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
installReactAdapter(defaultRuntime);
|
|
40
|
-
export const I18nManager = defaultManaged.manager;
|
|
41
|
-
export const I18nState = {
|
|
42
|
-
get: defaultManaged.state.get,
|
|
43
|
-
subscribe: defaultManaged.state.subscribe,
|
|
44
|
-
use() {
|
|
45
|
-
const managed = useContext(Context) ?? defaultManaged;
|
|
46
|
-
|
|
47
|
-
return useSyncExternalStore(managed.state.subscribe, managed.state.get, managed.state.get);
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export function I18nProvider(
|
|
52
|
-
{ children, snapshot, instance, lineBreak }: {
|
|
53
|
-
children?: React.ReactNode;
|
|
54
|
-
snapshot?: I18nSnapshot;
|
|
55
|
-
instance?: ManagedI18n;
|
|
56
|
-
lineBreak?: React.ReactNode;
|
|
57
|
-
},
|
|
58
|
-
) {
|
|
59
|
-
const ref = useRef<
|
|
60
|
-
{ managed: ManagedI18n; snapshot?: I18nSnapshot; instance?: ManagedI18n; lineBreak?: React.ReactNode; } | null
|
|
61
|
-
>(null);
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
!ref.current || ref.current.snapshot !== snapshot || ref.current.instance !== instance ||
|
|
65
|
-
ref.current.lineBreak !== lineBreak
|
|
66
|
-
) {
|
|
67
|
-
if (snapshot && instance) throw new Error('[i18n] pass either snapshot or instance, not both');
|
|
68
|
-
|
|
69
|
-
const managed = instance ?? createI18nManager();
|
|
70
|
-
installReactAdapter(managed.runtime, lineBreak);
|
|
71
|
-
if (snapshot) managed.manager.hydrate(snapshot);
|
|
72
|
-
ref.current = { managed, snapshot, instance, lineBreak };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const managed = ref.current.managed;
|
|
76
|
-
const state = useSyncExternalStore(managed.state.subscribe, managed.state.get, managed.state.get);
|
|
77
|
-
|
|
78
|
-
if (!state.ready) throw new Error('[i18n] initialize the provider instance or pass a ready snapshot');
|
|
79
|
-
|
|
80
|
-
return React.createElement(Context.Provider, { value: managed }, children);
|
|
81
|
-
}
|
|
82
|
-
/** Explicit binding for text attributes in isolated providers and SSR. */
|
|
83
|
-
export function useI18nRuntime(): Runtime {
|
|
84
|
-
const managed = useContext(Context) ?? defaultManaged;
|
|
85
|
-
useSyncExternalStore(managed.state.subscribe, managed.state.get, managed.state.get);
|
|
86
|
-
|
|
87
|
-
return managed.runtime;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export { createI18nManager, defineTranslationLoader } from './manager';
|
|
91
|
-
export type { I18nSnapshot, I18nStorage, ManagedI18n, StateData } from './manager';
|
|
92
|
-
/** Bind an existing universal renderer to this provider (including client SSR). */
|
|
93
|
-
export function useI18nRenderer<F extends (...args: any[]) => any>(renderer: F): F {
|
|
94
|
-
const runtime = useI18nRuntime();
|
|
95
|
-
|
|
96
|
-
return ((...args: Parameters<F>) => withRuntime(runtime, () => renderer(...args))) as F;
|
|
97
|
-
}
|
package/src/registry-manager.ts
DELETED
package/src/shared/compact.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/** Compact message format v1. Strings are literal messages or literal parts. */
|
|
2
|
-
export type CompactMessage = string | CompactPart[];
|
|
3
|
-
|
|
4
|
-
export type CompactPart =
|
|
5
|
-
| string
|
|
6
|
-
| [0, string]
|
|
7
|
-
| [1, string, string | Record<string, unknown> | null]
|
|
8
|
-
| [2 | 3, string, string | Record<string, unknown> | null]
|
|
9
|
-
| [4, string, Record<string, CompactMessage>]
|
|
10
|
-
| [5, string, number, 0 | 1, Record<string, CompactMessage>]
|
|
11
|
-
| [6]
|
|
12
|
-
| [7, string, CompactMessage];
|
|
13
|
-
|
|
14
|
-
export const COMPACT_VERSION = 1;
|
|
15
|
-
|
|
16
|
-
export function validateCompact(value: unknown, depth = 0): asserts value is CompactMessage {
|
|
17
|
-
if (depth > 100) throw new TypeError('[i18n] compact message nesting exceeds 100');
|
|
18
|
-
if (typeof value === 'string') return;
|
|
19
|
-
if (!Array.isArray(value)) throw new TypeError('[i18n] invalid compact message');
|
|
20
|
-
|
|
21
|
-
for (const part of value) {
|
|
22
|
-
if (typeof part === 'string') continue;
|
|
23
|
-
if (!Array.isArray(part)) throw new TypeError('[i18n] invalid compact instruction');
|
|
24
|
-
|
|
25
|
-
const [op, name] = part;
|
|
26
|
-
const lengths: Record<number, number> = { 0: 2, 1: 3, 2: 3, 3: 3, 4: 3, 5: 5, 6: 1, 7: 3 };
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
!Object.prototype.hasOwnProperty.call(lengths, op) || part.length !== lengths[op] ||
|
|
30
|
-
(op !== 6 && typeof name !== 'string')
|
|
31
|
-
) throw new TypeError('[i18n] invalid compact opcode');
|
|
32
|
-
if (
|
|
33
|
-
op >= 1 && op <= 3 && part[2] !== null && typeof part[2] !== 'string' &&
|
|
34
|
-
(typeof part[2] !== 'object' || Array.isArray(part[2]))
|
|
35
|
-
) throw new TypeError('[i18n] invalid compact format');
|
|
36
|
-
if (op === 5 && (typeof part[2] !== 'number' || !Number.isFinite(part[2]) || ![0, 1].includes(part[3]))) {
|
|
37
|
-
throw new TypeError('[i18n] invalid compact plural');
|
|
38
|
-
}
|
|
39
|
-
if (op === 4 || op === 5) {
|
|
40
|
-
const branches = part[op === 4 ? 2 : 4];
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
!branches || typeof branches !== 'object' || Array.isArray(branches) ||
|
|
44
|
-
!Object.prototype.hasOwnProperty.call(branches, 'other')
|
|
45
|
-
) throw new TypeError('[i18n] missing other branch');
|
|
46
|
-
|
|
47
|
-
for (const child of Object.values(branches)) validateCompact(child, depth + 1);
|
|
48
|
-
}
|
|
49
|
-
if (op === 7) validateCompact(part[2], depth + 1);
|
|
50
|
-
}
|
|
51
|
-
}
|