Skip to content

Commit

Permalink
Change ptu function parameter name options to option
Browse files Browse the repository at this point in the history
  • Loading branch information
imjuni committed Oct 11, 2023
1 parent d8a1239 commit 3b1674e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
17 changes: 10 additions & 7 deletions src/__tests__/i18n.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import I18nController from '#/I18nController';
import type I18nParameters from '#/interfaces/I18nParameters';
import pt from '#/pt';
import acceptLanguage from 'accept-language';
import Polyglot from 'node-polyglot';
Expand Down Expand Up @@ -87,14 +88,16 @@ describe('I18nContainer', () => {
});

it('ptu', async () => {
expect(ptu({ headers: { 'accept-language': 'ko' } }, { phrase: 'common.error' })).toEqual(
'오류가 발생했습니다, 잠시 후 다시 시도해 주십시오',
);
expect(
ptu(
{ headers: { 'accept-language': 'ko' } },
{ phrase: 'pet.weight.result', options: { pet_weight: 30 } },
),
ptu({ headers: { 'accept-language': 'ko' } }, {
phrase: 'common.error',
} satisfies I18nParameters),
).toEqual('오류가 발생했습니다, 잠시 후 다시 시도해 주십시오');
expect(
ptu({ headers: { 'accept-language': 'ko' } }, {
phrase: 'pet.weight.result',
option: { pet_weight: 30 },
} satisfies I18nParameters),
).toEqual('반려동물 체중: 30kg');
expect(ptu({ headers: { 'accept-language': 'ko' } }, 1)).toEqual('');

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as I18nController } from './I18nController';
export { default as getI18nContainerOption } from './getI18nContainerOption';
export type { default as I18nControllerOption } from './interfaces/I18nControllerOption';
export type { default as I18nParameters } from './interfaces/I18nParameters';
export { default as pt } from './pt';
export { default as ptu } from './ptu';
6 changes: 6 additions & 0 deletions src/interfaces/I18nParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type Polyglot from 'node-polyglot';

export default interface I18nParameters {
phrase: string;
option?: Parameters<Polyglot['t']>[1];
}
8 changes: 4 additions & 4 deletions src/ptu.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import I18nController from '#/I18nController';
import type I18nParameters from '#/interfaces/I18nParameters';
import type { FastifyRequest } from 'fastify';
import type Polyglot from 'node-polyglot';
import I18nController from './I18nController';

export default function ptu(req: Pick<FastifyRequest, 'headers'>, uo: unknown): string {
try {
if (typeof uo === 'object' && uo != null) {
const { phrase, options } = uo as { phrase: string; options?: Parameters<Polyglot['t']>[1] };
const { phrase, option } = uo as I18nParameters;

return I18nController.it.t(
I18nController.it.getLanguageFromRequestHeader(req.headers['accept-language']),
phrase,
options,
option,
);
}

Expand Down

0 comments on commit 3b1674e

Please sign in to comment.