@payloadcms/translations 3.0.0-alpha.13 → 3.0.0-alpha.14
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/types.d.ts +12 -11
- package/dist/utilities/init.js +20 -13
- package/package.json +1 -1
package/dist/types.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
export type LanguageTranslations = {
|
|
2
|
+
[namespace: string]: {
|
|
3
|
+
[key: string]: string;
|
|
4
|
+
};
|
|
5
|
+
};
|
|
1
6
|
export type Translations = {
|
|
2
7
|
[language: string]: {
|
|
3
8
|
$schema: string;
|
|
4
|
-
} |
|
|
5
|
-
[namespace: string]: {
|
|
6
|
-
[key: string]: string;
|
|
7
|
-
};
|
|
8
|
-
};
|
|
9
|
+
} | LanguageTranslations;
|
|
9
10
|
};
|
|
10
11
|
export type TFunction = (key: string, options?: Record<string, any>) => string;
|
|
11
12
|
export type I18n = {
|
|
@@ -15,6 +16,7 @@ export type I18n = {
|
|
|
15
16
|
language: string;
|
|
16
17
|
/** Translate function */
|
|
17
18
|
t: (key: string, options?: Record<string, unknown>) => string;
|
|
19
|
+
translations: Translations;
|
|
18
20
|
};
|
|
19
21
|
export type I18nOptions = {
|
|
20
22
|
fallbackLanguage?: string;
|
|
@@ -22,18 +24,17 @@ export type I18nOptions = {
|
|
|
22
24
|
translations?: {
|
|
23
25
|
[language: string]: {
|
|
24
26
|
$schema: string;
|
|
25
|
-
} |
|
|
26
|
-
[namespace: string]: {
|
|
27
|
-
[key: string]: string;
|
|
28
|
-
};
|
|
29
|
-
};
|
|
27
|
+
} | LanguageTranslations;
|
|
30
28
|
};
|
|
31
29
|
};
|
|
32
30
|
export type InitTFunction = (args: {
|
|
33
31
|
config: I18nOptions;
|
|
34
32
|
language?: string;
|
|
35
33
|
translations?: Translations;
|
|
36
|
-
}) =>
|
|
34
|
+
}) => {
|
|
35
|
+
t: TFunction;
|
|
36
|
+
translations: Translations;
|
|
37
|
+
};
|
|
37
38
|
export type InitI18n = (args: {
|
|
38
39
|
config: I18nOptions;
|
|
39
40
|
language?: string;
|
package/dist/utilities/init.js
CHANGED
|
@@ -145,15 +145,20 @@ function matchLanguage(header) {
|
|
|
145
145
|
return undefined;
|
|
146
146
|
}
|
|
147
147
|
exports.matchLanguage = matchLanguage;
|
|
148
|
-
const initTFunction = (args) =>
|
|
148
|
+
const initTFunction = (args) => {
|
|
149
149
|
const { config, language, translations } = args;
|
|
150
|
-
const
|
|
150
|
+
const mergedTranslations = (0, deepMerge_1.deepMerge)(config?.translations ?? {}, translations);
|
|
151
151
|
const languagePreference = matchLanguage(language);
|
|
152
|
-
return
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
152
|
+
return {
|
|
153
|
+
translations: mergedTranslations,
|
|
154
|
+
t: (key, vars) => {
|
|
155
|
+
return (0, exports.t)({
|
|
156
|
+
key,
|
|
157
|
+
translations: mergedTranslations[languagePreference],
|
|
158
|
+
vars,
|
|
159
|
+
});
|
|
160
|
+
},
|
|
161
|
+
};
|
|
157
162
|
};
|
|
158
163
|
function memoize(fn, keys) {
|
|
159
164
|
const cacheMap = new Map();
|
|
@@ -167,15 +172,17 @@ function memoize(fn, keys) {
|
|
|
167
172
|
};
|
|
168
173
|
return memoized;
|
|
169
174
|
}
|
|
170
|
-
exports.initI18n = memoize(({ config, language = 'en', translations, context }) => {
|
|
175
|
+
exports.initI18n = memoize(({ config, language = 'en', translations: incomingTranslations, context, }) => {
|
|
176
|
+
const { t, translations } = initTFunction({
|
|
177
|
+
config,
|
|
178
|
+
language: language || config.fallbackLanguage,
|
|
179
|
+
translations: incomingTranslations,
|
|
180
|
+
});
|
|
171
181
|
const i18n = {
|
|
172
182
|
fallbackLanguage: config.fallbackLanguage,
|
|
173
183
|
language: language || config.fallbackLanguage,
|
|
174
|
-
t
|
|
175
|
-
|
|
176
|
-
language: language || config.fallbackLanguage,
|
|
177
|
-
translations,
|
|
178
|
-
}),
|
|
184
|
+
t,
|
|
185
|
+
translations,
|
|
179
186
|
};
|
|
180
187
|
return i18n;
|
|
181
188
|
}, ['language', 'context']);
|