Skip to content

Commit

Permalink
Add translations
Browse files Browse the repository at this point in the history
  • Loading branch information
horstenwillem committed Apr 23, 2018
1 parent 0350b83 commit 49ef012
Show file tree
Hide file tree
Showing 12 changed files with 1,014 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ package-lock.json
# Yarn
.yarnclean
yarn-error.log

# Exclude translation files
src/lib/locales/*.json
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ const parsedError = parseErrors(error);
serializer.serialize([parsedError]);
```

## Import translations

Import new or updated translations from the iCapps translation portal

```javascript
import { importTranslations } from 'tree-house-errors';

await importTranslations('applicationToken');
```

## Tests

- You can run `yarn test` to run all tests
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
"dependencies": {
"express-validation": "~1.0.2",
"http-status": "~1.0.1",
"i18n": "0.8.3",
"joi": "~13.1.2",
"uuid": "~3.2.1"
},
"devDependencies": {
"@types/http-status": "~0.2.30",
"@types/i18n": "0.8.3",
"@types/jest": "~22.2.0",
"@types/uuid": "~3.4.3",
"coveralls": "^3.0.0",
"icapps-translations": "^0.0.15",
"jest": "^22.1.4",
"np": "^2.20.1",
"pre-commit": "^1.2.2",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { parseErrors } from './lib/errorParser';
export { errorConfig as errors } from './lib/errorConfig';
export { importTranslations, Options } from './lib/importTranslations';
export * from './lib/errors';
8 changes: 6 additions & 2 deletions src/lib/errorParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as ev from 'express-validation';
import { ApiError, ValidationError } from './errors';
import { errorConfig as errors } from './errorConfig';
import { errorDefaults } from './constants';
import i18n from './i18nConfig';

export function parseErrors(error: any) {
export function parseErrors(error: any, language: string) {
const metaData: any = {};
let parsedError = new ApiError(errorDefaults.DEFAULT_HTTP_CODE, errorDefaults.DEFAULT_ERROR); // Default error

Expand All @@ -25,7 +26,10 @@ export function parseErrors(error: any) {

// Own thrown ApiErrors
if (error instanceof ApiError) {
parsedError = error;
i18n.setLocale(language);
const correctMessage = i18n.__(error.code);

parsedError = Object.assign({}, error, { message: correctMessage });
}

// Return object easy to use for serialisation
Expand Down
7 changes: 7 additions & 0 deletions src/lib/i18nConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as i18n from 'i18n';

i18n.configure({
directory: __dirname + '/locales',
});

export default i18n;
23 changes: 23 additions & 0 deletions src/lib/importTranslations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const translations = require('icapps-translations');

const url = '';
const defaultOptions = {
destination: './lib/locales',
};

export function importTranslations(token: string, options?: Options) {
try {
const allOptions = Object.assign({}, defaultOptions, options);
return translations.import(url, token, allOptions);
} catch (ex) {
throw ex;
}
}

export interface Options {
destination?: string;
clean?: boolean;
verbose?: boolean;
seperateCategories?: boolean;
exportType?: string;
}
Empty file added src/lib/locales/.gitkeep
Empty file.
8 changes: 4 additions & 4 deletions tests/errorParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('errorParser', () => {

describe('Unkown errors', () => {
it('Should return the default error when unknown object is passed', () => {
const parsedError = parseErrors({ myProp: 'iDunno' });
const parsedError = parseErrors({ myProp: 'iDunno' }, 'en');
expect(parsedError).toMatchObject({
id: expect.any(String),
status: defaultError.status,
Expand All @@ -27,7 +27,7 @@ describe('errorParser', () => {
const error = new Error('Some error...');
Object.assign(error, { stack: 'myStack', detail: 'some details', schema: 'public' });

const parsedError = parseErrors(error);
const parsedError = parseErrors(error, 'en');
expect(parsedError).toMatchObject({
id: expect.any(String),
status: defaultError.status,
Expand All @@ -51,7 +51,7 @@ describe('errorParser', () => {
};

const expressValidationError = new ValidationError([{ name: 'myField' }], options);
const parsedError = parseErrors(expressValidationError);
const parsedError = parseErrors(expressValidationError, 'en');
expect(parsedError).toMatchObject({
id: expect.any(String),
status: httpStatus.BAD_REQUEST,
Expand All @@ -66,7 +66,7 @@ describe('errorParser', () => {
try {
throw new ApiError(httpStatus.BAD_REQUEST, errors.INVALID_INPUT);
} catch (err) {
const parsedError = parseErrors(err);
const parsedError = parseErrors(err, 'en');
expect(parsedError).toMatchObject({
id: expect.any(String),
status: httpStatus.BAD_REQUEST,
Expand Down
9 changes: 9 additions & 0 deletions tests/translations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as fs from 'fs';
import { importTranslations, Options } from '../src';

describe('importTranslations', () => {
it('should import the translations', async () => {
await importTranslations('randomToken');
expect(await fs.existsSync('./src/lib/locales/en.json')).toEqual(true);
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"exclude": [
"node_modules",
"**/*.test.ts",
"build"
"build",
]
}

0 comments on commit 49ef012

Please sign in to comment.