123456789101112131415161718192021222324 |
- import { IMiddleware } from '@midwayjs/core';
- import { Middleware } from '@midwayjs/decorator';
- import { NextFunction, Context } from '@midwayjs/koa';
- import { head, last } from 'lodash';
- @Middleware()
- export class SetLocaleToCtxMiddleware implements IMiddleware<Context, NextFunction> {
- resolve() {
- return async (ctx: Context, next: NextFunction) => {
- const cookies = ctx.request.headers.cookie;
- let arr = cookies.split(';');
- arr = arr.filter(f => f.includes('locale='));
- // 没找到locale就默认使用中文
- if (arr.length <= 0) arr = ['locale=zh-cn'];
- const a2 = head(arr).split('=');
- const locale = last(a2);
- ctx.locale = locale;
- await next();
- };
- }
- static getName(): string {
- return 'setLocale';
- }
- }
|