@payloadcms/translations
Advanced tools
Comparing version 3.0.0-alpha.13 to 3.0.0-alpha.14
@@ -0,9 +1,10 @@ | ||
export type LanguageTranslations = { | ||
[namespace: string]: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
export type Translations = { | ||
[language: string]: { | ||
$schema: string; | ||
} | { | ||
[namespace: string]: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
} | LanguageTranslations; | ||
}; | ||
@@ -18,2 +19,3 @@ export type TFunction = (key: string, options?: Record<string, any>) => string; | ||
t: (key: string, options?: Record<string, unknown>) => string; | ||
translations: Translations; | ||
}; | ||
@@ -26,7 +28,3 @@ export type I18nOptions = { | ||
$schema: string; | ||
} | { | ||
[namespace: string]: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
} | LanguageTranslations; | ||
}; | ||
@@ -38,3 +36,6 @@ }; | ||
translations?: Translations; | ||
}) => TFunction; | ||
}) => { | ||
t: TFunction; | ||
translations: Translations; | ||
}; | ||
export type InitI18n = (args: { | ||
@@ -41,0 +42,0 @@ config: I18nOptions; |
@@ -148,11 +148,16 @@ "use strict"; | ||
exports.matchLanguage = matchLanguage; | ||
const initTFunction = (args) => (key, vars) => { | ||
const initTFunction = (args) => { | ||
const { config, language, translations } = args; | ||
const mergedLanguages = (0, deepMerge_1.deepMerge)(config?.translations ?? {}, translations); | ||
const mergedTranslations = (0, deepMerge_1.deepMerge)(config?.translations ?? {}, translations); | ||
const languagePreference = matchLanguage(language); | ||
return (0, exports.t)({ | ||
key, | ||
translations: mergedLanguages[languagePreference], | ||
vars, | ||
}); | ||
return { | ||
translations: mergedTranslations, | ||
t: (key, vars) => { | ||
return (0, exports.t)({ | ||
key, | ||
translations: mergedTranslations[languagePreference], | ||
vars, | ||
}); | ||
}, | ||
}; | ||
}; | ||
@@ -171,13 +176,15 @@ function memoize(fn, keys) { | ||
} | ||
exports.initI18n = memoize(({ config, language = 'en', translations, context }) => { | ||
exports.initI18n = memoize(({ config, language = 'en', translations: incomingTranslations, context, }) => { | ||
const { t, translations } = initTFunction({ | ||
config, | ||
language: language || config.fallbackLanguage, | ||
translations: incomingTranslations, | ||
}); | ||
const i18n = { | ||
fallbackLanguage: config.fallbackLanguage, | ||
language: language || config.fallbackLanguage, | ||
t: initTFunction({ | ||
config, | ||
language: language || config.fallbackLanguage, | ||
translations, | ||
}), | ||
t, | ||
translations, | ||
}; | ||
return i18n; | ||
}, ['language', 'context']); |
{ | ||
"name": "@payloadcms/translations", | ||
"version": "3.0.0-alpha.13", | ||
"version": "3.0.0-alpha.14", | ||
"main": "./dist/exports/index.ts", | ||
@@ -5,0 +5,0 @@ "types": "./dist/types.d.ts", |
1014351
23530