Skip to content

Commit f96266b

Browse files
authored
Merge pull request #7 from kne-union/linzp
提交一下
2 parents fa2ff9a + 8eea90e commit f96266b

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,29 @@ module.exports = fp(async (fastify, options) => {
116116
};
117117

118118
if (!fastify.hasDecorator(options.name)) {
119-
const defaultIntl = await createIntlInstance(options.defaultLocale, options.moduleName);
120-
const defaultT = (id, values) => {
121-
return defaultIntl.formatMessage({ id }, values);
122-
};
123119
fastify.register(require('@kne/fastify-namespace'), {
124120
name: options.name,
125121
options,
126122
modules: [
127123
['createIntl', createIntlInstance],
128124
['getRequestLocale', getRequestLocale],
129125
['withLocale', withLocale],
130-
['defaultIntl', defaultIntl],
131-
['t', defaultT]
126+
[
127+
't',
128+
(id, values) => {
129+
return fastify[options.name].defaultIntl.formatMessage({ id }, values);
130+
}
131+
]
132132
],
133-
onMount: name => {
133+
onMount: async name => {
134134
if (name === options.name) {
135135
return;
136136
}
137137
const locale = fastify.namespace.modules?.[name]?.locale;
138138
locale && loadMessage(locale, name);
139+
if (name === options.moduleName) {
140+
fastify[options.name].defaultIntl = await createIntlInstance(options.defaultLocale, options.moduleName);
141+
}
139142
}
140143
});
141144
fastify.addHook('onRequest', async request => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kne/fastify-intl",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "`@kne/fastify-intl` 是一个专为 Fastify 项目设计的国际化解决方案插件。它基于业界成熟的 `@formatjs/intl` 库构建,提供了强大而灵活的多语言支持能力,帮助开发者轻松实现应用的国际化和本地化。",
55
"main": "index.js",
66
"types": "index.d.ts",

0 commit comments

Comments
 (0)