Skip to content

Commit

Permalink
feat(global): locale naming simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
rimiti committed Sep 25, 2021
1 parent 42f6009 commit bf66e0e
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { codes } from './codes';
import { CodeGetter } from './utils';
import { ELocal, IFullCode } from './types';

const errors = new CodeGetter(codes, ELocal.en_US);
const errors = new CodeGetter(codes, ELocal.en);

export { errors, ELocal, IFullCode };
8 changes: 5 additions & 3 deletions src/resources/authentication/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { II18n, IErrorRes, EHttpCode } from '../../types';
import * as fr_FR from './locales/fr_FR.json';
import * as en_US from './locales/en_US.json';

const i18n: II18n = { fr_FR, en_US };
// Locales
import * as fr from './locales/fr.json';
import * as en from './locales/en.json';

const i18n: II18n = { fr, en };

const authentication: IErrorRes = {
client: {
Expand Down
8 changes: 4 additions & 4 deletions src/resources/general/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { II18n, IErrorRes, EHttpCode } from '../../types';

// Locals
import * as fr_FR from './locales/fr_FR.json';
import * as en_US from './locales/en_US.json';
// Locales
import * as fr from './locales/fr.json';
import * as en from './locales/en.json';

const i18n: II18n = { fr_FR, en_US };
const i18n: II18n = { fr, en };

const general: IErrorRes = {
client: {},
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 5 additions & 3 deletions src/resources/signIn/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { II18n, IErrorRes, EHttpCode } from '../../types';
import * as fr_FR from './locales/fr_FR.json';
import * as en_US from './locales/en_US.json';

const i18n: II18n = { fr_FR, en_US };
// Locales
import * as fr from './locales/fr.json';
import * as en from './locales/en.json';

const i18n: II18n = { fr, en };

const authentication: IErrorRes = {
client: {
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ enum EHttpCode {
}

enum ELocal {
'fr_FR' = 'fr_FR',
'en_US' = 'en_US',
'fr' = 'fr',
'en' = 'en',
}

interface ICode {
Expand Down
14 changes: 7 additions & 7 deletions tests/utils/codeGetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ describe('/src/utils/codeGetter.ts', () => {
internal: {},
external: {},
i18n: {
en_US: {
en: {
client: {
testError2: 'testError2_en_US',
},
},
fr_FR: {
fr: {
client: {
testError2: 'testError2_fr_FR',
},
Expand All @@ -32,7 +32,7 @@ describe('/src/utils/codeGetter.ts', () => {
};

beforeEach(() => {
errorsMock = new CodeGetter(fakeCodes, ELocal.fr_FR);
errorsMock = new CodeGetter(fakeCodes, ELocal.fr);
});

it('should get one error by key', () => {
Expand All @@ -50,24 +50,24 @@ describe('/src/utils/codeGetter.ts', () => {
});

it('should get translation of error with given lang', () => {
const err = errorsMock.get('carts:client:testError2').i18n(ELocal.en_US);
const err = errorsMock.get('carts:client:testError2').i18n(ELocal.en);

expect(typeof err).toBe('string');
expect(err).toBe('testError2_en_US');
});

it('should throw an error', () => {
let err: Error|null = null;
let err: Error | null = null;

try {
errorsMock.get('carts:client:testError').i18n(ELocal.en_US);
errorsMock.get('carts:client:testError').i18n(ELocal.en);
} catch (e) {
err = e;
}

expect(err).toStrictEqual(
Error(
'Translation for key carts:client:testError in en_US was not found',
'Translation for key carts:client:testError in en was not found',
),
);
});
Expand Down

0 comments on commit bf66e0e

Please sign in to comment.