Skip to content

Commit

Permalink
feat: added i18n.translateError method
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed May 9, 2020
1 parent c3039bd commit 20b8c1c
Show file tree
Hide file tree
Showing 5 changed files with 1,553 additions and 1,482 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* [Install](#install)
* [Usage](#usage)
* [API](#api)
* [i18n.translate(key, locale)](#i18ntranslatekey-locale)
* [i18n.translate(key, locale, ...args)](#i18ntranslatekey-locale-args)
* [i18n.translateError(key, locale, ...args)](#i18ntranslateerrorkey-locale-args)
* [i18n.middleware(ctx, next)](#i18nmiddlewarectx-next)
* [i18n.redirect(ctx, next)](#i18nredirectctx-next)
* [Options](#options)
Expand Down Expand Up @@ -58,9 +59,15 @@ app.listen();

## API

### i18n.translate(key, locale)
### i18n.translate(key, locale, ...args)

Returns translation for phrase `key` with the given `locale`.
Returns translation for phrase `key` with the given `locale`. Optionally pass additional arguments, e.g. format specifier replacements for use in the phrase. For example if you have a phrase of "An error occurred %s" with a key of "ERROR_OCCURRED", and you use it as such `i18n.translate('ERROR_OCCURRED', 'en', 'some error message')` then it would return `'An error occurred some error message`.

### i18n.translateError(key, locale, ...args)

Returns the same string as `i18n.translate`, but wrapped with a new `Error` object with a property `no_translate` and a value set to `true`.

This is an extremely useful method if you are using `koa-better-error-handler` package in the [Lad][] framework – as it will prevent a double translation from occurring.

### i18n.middleware(ctx, next)

Expand Down Expand Up @@ -189,3 +196,5 @@ For a list of all available locales see [i18n-locales][].
[cabin]: https://cabinjs.com

[multimatch]: https://github.com/sindresorhus/multimatch

[lad]: https://github.com/ladjs/lad
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,41 @@
"Nick Baugh <niftylettuce@gmail.com> (http://niftylettuce.com/)"
],
"dependencies": {
"@hapi/boom": "^9.0.0",
"boolean": "3.0.0",
"@hapi/boom": "^9.1.0",
"boolean": "3.0.1",
"country-language": "^0.1.7",
"debug": "^4.1.1",
"i18n": "^0.9.1",
"i18n-locales": "^0.0.4",
"lodash": "^4.17.15",
"moment": "^2.24.0",
"moment": "^2.25.3",
"multimatch": "^4.0.0",
"qs": "^6.9.1",
"qs": "^6.9.4",
"titleize": "^2.1.0"
},
"devDependencies": {
"@babel/cli": "^7.8.3",
"@babel/core": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"ava": "2.x",
"codecov": "^3.6.2",
"codecov": "^3.6.5",
"cross-env": "6.x",
"eslint": "^6.8.0",
"eslint": "6.x",
"eslint-config-xo-lass": "^1.0.3",
"eslint-plugin-node": "^11.0.0",
"fixpack": "^2.3.1",
"eslint-plugin-node": "^11.1.0",
"fixpack": "^3.0.6",
"husky": "3.x",
"koa": "^2.11.0",
"koa-generic-session": "^2.0.4",
"lint-staged": "^10.0.3",
"nyc": "^15.0.0",
"remark-cli": "^7.0.1",
"remark-preset-github": "^0.0.16",
"sinon": "^8.1.1",
"lint-staged": "^10.2.2",
"nyc": "^15.0.1",
"remark-cli": "^8.0.0",
"remark-preset-github": "^1.0.0",
"sinon": "^9.0.2",
"supertest": "^4.0.2",
"xo": "^0.25.3"
"xo": "0.25"
},
"engines": {
"node": ">=6.4.0"
Expand Down
20 changes: 11 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,25 @@ class I18N {
this.configure(this.config);

this.translate = this.translate.bind(this);
this.translateError = this.translateError.bind(this);
this.middleware = this.middleware.bind(this);
this.redirect = this.redirect.bind(this);
}

translate(key, locale) {
translate(key, locale, ...args) {
locale = locale || this.config.defaultLocale;
const { phrases } = this.config;
// eslint-disable-next-line prefer-rest-params
let args = Object.keys(arguments)
// eslint-disable-next-line prefer-rest-params
.map(key => arguments[key])
.slice(2);
if (typeof args === 'undefined') args = [];
const phrase = phrases[key];
if (typeof phrase !== 'string')
throw new Error(`translation key missing: ${key}`);
args = [{ phrase, locale }, ...args];
return i18n.api.t(...args);
return i18n.api.t({ phrase, locale }, ...args);
}

translateError(key, locale, ...args) {
const str = this.translate(key, locale, ...args);
const err = new Error(str);
err.no_translate = true;
return err;
}

middleware(ctx, next) {
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,11 @@ test(`logs error if saves fails for user ctx`, async t => {
t.true(spy.calledOnce);
t.true(spy.alwaysCalledWithExactly(saveError));
});

test('returns an error object with no_translate=true', t => {
const i18n = new I18N({ phrases, directory });
t.is(i18n.translate('hello', 'en'), 'hello');
const err = i18n.translateError('hello', 'es');
t.true(err.no_translate);
t.is(err.message, 'hola');
});
Loading

0 comments on commit 20b8c1c

Please sign in to comment.