Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,29 @@ module.exports = fp(async (fastify, options) => {
};

if (!fastify.hasDecorator(options.name)) {
const defaultIntl = await createIntlInstance(options.defaultLocale, options.moduleName);
const defaultT = (id, values) => {
return defaultIntl.formatMessage({ id }, values);
};
fastify.register(require('@kne/fastify-namespace'), {
name: options.name,
options,
modules: [
['createIntl', createIntlInstance],
['getRequestLocale', getRequestLocale],
['withLocale', withLocale],
['defaultIntl', defaultIntl],
['t', defaultT]
[
't',
(id, values) => {
return fastify[options.name].defaultIntl.formatMessage({ id }, values);
}
]
],
onMount: name => {
onMount: async name => {
if (name === options.name) {
return;
}
const locale = fastify.namespace.modules?.[name]?.locale;
locale && loadMessage(locale, name);
if (name === options.moduleName) {
fastify[options.name].defaultIntl = await createIntlInstance(options.defaultLocale, options.moduleName);
}
}
});
fastify.addHook('onRequest', async request => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kne/fastify-intl",
"version": "0.1.4",
"version": "0.1.5",
"description": "`@kne/fastify-intl` 是一个专为 Fastify 项目设计的国际化解决方案插件。它基于业界成熟的 `@formatjs/intl` 库构建,提供了强大而灵活的多语言支持能力,帮助开发者轻松实现应用的国际化和本地化。",
"main": "index.js",
"types": "index.d.ts",
Expand Down