英文:
Express with i18next in Typescript
问题
我最近在我的Express API中添加了国际化功能。我找到了一个示例 https://github.com/bmanley91/express-i18n-example/blob/master/src/server.js 。我按照这个示例一步一步地进行了一些小的修改。但主要的区别在于这个示例是用JavaScript编写的,而我的项目是用TypeScript编写的。
出于示例目的,我将代码简化了。
// services/i18next.ts
import i18next from 'i18next';
import en_GB_translation from '../resources/locales/en_GB/translation.json';
const resources = {
en_GB: {
translation: en_GB_translation
}
}
i18next
.init(
{
resources,
lng: "en_GB",
supportedLngs: ["en_GB"],
fallbackLng: "en_GB"
}
)
export default i18next;
// index.ts
const i18nextMiddleware = require('i18next-express-middleware');
import i18next from './services/i18next';
app.use(i18nextMiddleware.handle(i18next));
app.use('/users', usersRoute);
export const create = async (req: Request, res: Response, next: NextFunction) => {
const response = req.t("successful");
res.status(200);
res.send(response);
}
但TypeScript报错了:
Property 't' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
是否有人实现了相同的功能并遇到了相同的问题?
英文:
I added internationalization into my Express API recently. I found example https://github.com/bmanley91/express-i18n-example/blob/master/src/server.js .
I followed that example step by step with some little modifications. But the main different is that example is written in JavaScript and my project is written in Typescript.
For example purposes I minimized code.
// services/i18next.ts
import i18next from 'i18next'
import en_GB_translation from '../resources/locales/en_GB/translation.json'
const resources = {
en_GB: {
translation: en_GB_translation
}
}
i18next
.init(
{
resources,
lng: "en_GB",
supportedLngs: ["en_GB"],
fallbackLng: "en_GB"
}
)
export default i18next
// index.ts
const i18nextMiddleware = require('i18next-express-middleware')
import import i18next from './services/i18next'
app.use(i18nextMiddleware.handle(i18next))
app.use('/users', usersRoute)
export const create = async (req: Request, res: Response, next: NextFunction) => {
const response = req.t("successful)
res.status(200);
res.send(response)
}
But Typescript throw error
Property 't' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
Is here someone who implemented same thing and ran into the same problem ?
答案1
得分: 1
卸载i18next-express-middleware
并安装i18next-http-middleware
。
这是express-i18next的最新且更好的包。
然后:
// index.ts
const i18nextMiddleware = require('i18next-http-middleware');
import i18next from './services/i18next';
app.use(i18nextMiddleware.handle(i18next));
app.use('/users', usersRoute);
英文:
Just uninstall i18next-express-middleware
and install i18next-http-middleware
.
It is newest and better package for express-i18next
Then:
// index.ts
const i18nextMiddleware = require('i18next-http-middleware')
import i18next from './services/i18next'
app.use(i18nextMiddleware.handle(i18next))
app.use('/users', usersRoute)
答案2
得分: 0
解决方法是在 .d.ts 文件中声明 "t" 函数。
declare global {
namespace Express {
interface Request {
t: (...args: any) => void
}
}
}
export {}
英文:
The solution is to declare "t" function in .d.ts file.
declare global {
namespace Express {
interface Request {
t: (...args: any) => void
}
}
}
export {}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论