feat: Add more debug information on a 'silly' level

master
Roger Fos Soler 2017-04-13 18:19:42 +02:00 committed by fengmk2
parent 2d9f68ab8c
commit c46d049a5e
3 changed files with 94 additions and 24 deletions

3
.gitignore vendored
View File

@ -12,4 +12,5 @@ results
node_modules
npm-debug.log
coverage/
coverage
.idea

View File

@ -7,17 +7,6 @@ koa-locales
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]
[npm-image]: https://img.shields.io/npm/v/koa-locales.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-locales
[travis-image]: https://img.shields.io/travis/koajs/locales.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/locales
[cov-image]: https://codecov.io/github/koajs/locales/coverage.svg?branch=master
[cov-url]: https://codecov.io/github/koajs/locales?branch=master
[david-image]: https://img.shields.io/david/koajs/locales.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/locales
[download-image]: https://img.shields.io/npm/dm/koa-locales.svg?style=flat-square
[download-url]: https://npmjs.org/package/koa-locales
koa locales, i18n solution for koa:
1. All locales resources location on `options.dirs`.
@ -57,7 +46,7 @@ Patch locales functions to koa app.
- {String} defaultLocale: default locale. Optional, default is `en-US`.
- {String} queryField: locale field name on query. Optional, default is `locale`.
- {String} cookieField: locale field name on cookie. Optional, default is `locale`.
- {Object} localeAlias: locale cookie value map. Optional, default is {}.
- {Object} localeAlias: locale value map. Optional, default is {}.
- {String|Number} cookieMaxAge: set locale cookie value max age. Optional, default is `1y`, expired after one year.
```js
@ -65,7 +54,20 @@ locales({
app: app,
dirs: [__dirname + '/app/locales'],
defaultLocale: 'zh-CN',
}));
});
```
#### Aliases
The key `options.localeAlias` allows to not repeat dictionary files, as you can configure to use the same file for *es_ES* for *es*, or *en_UK* for *en*.
```js
locales({
localeAlias: {
es: es_ES,
en: en_UK
}
});
```
### `context.__(key[, value1[, value2, ...]])`
@ -102,15 +104,62 @@ __('{a} {a} {b} {b} {b}', {a: 'foo', b: 'bar'})
this.state.__ = this.__.bind(this);
```
[nunjucks] example:
[Nunjucks] example:
```html
{{ __('Hello, %s', user.name) }}
```
[Pug] example:
```pug
p= __('Hello, %s', user.name)
```
[Koa-pug] integration:
You can set the property *locals* on the KoaPug instance, where the default locals are stored.
```js
app.use(async (ctx, next) => {
koaPug.locals.__ = ctx.__.bind(ctx);
await next()
});
```
## Debugging
If you are interested on knowing what locale was chosen and why you can enable the debug messages from [debug].
There is two level of verbosity:
```sh
$ DEBUG=koa-locales node .
```
With this line it only will show one line per request, with the chosen language and the origin where the locale come from (queryString, header or cookie).
```sh
$ DEBUG=koa-locales:silly node .
```
Use this level if something doesn't work as you expect. This is going to debug everything, including each translated line of text.
## License
[MIT](LICENSE)
[nunjucks]: https://www.npmjs.com/package/nunjucks
[debug]: https://www.npmjs.com/package/debug
[pug]: https://www.npmjs.com/package/pug
[koa-pug]: https://www.npmjs.com/package/koa-pug
[npm-image]: https://img.shields.io/npm/v/koa-locales.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-locales
[travis-image]: https://img.shields.io/travis/koajs/locales.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/locales
[cov-image]: https://codecov.io/github/koajs/locales/coverage.svg?branch=master
[cov-url]: https://codecov.io/github/koajs/locales?branch=master
[david-image]: https://img.shields.io/david/koajs/locales.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/locales
[download-image]: https://img.shields.io/npm/dm/koa-locales.svg?style=flat-square
[download-url]: https://npmjs.org/package/koa-locales

View File

@ -1,6 +1,8 @@
'use strict';
const debug = require('debug')('koa-locales');
const Debug = require('debug');
const debug = Debug('koa-locales');
const debugSilly = Debug('koa-locales:silly');
const ini = require('ini');
const util = require('util');
const fs = require('fs');
@ -64,7 +66,7 @@ module.exports = function (app, options) {
}
}
debug('init locales with %j, got %j resources', options, Object.keys(resources));
debug('Init locales with %j, got %j resources', options, Object.keys(resources));
app.context[functionName] = function (key, value) {
if (arguments.length === 0) {
@ -80,7 +82,7 @@ module.exports = function (app, options) {
text = key;
}
debug('%s: %j => %j', locale, key, text);
debugSilly('%s: %j => %j', locale, key, text);
if (!text) {
return '';
}
@ -128,7 +130,18 @@ module.exports = function (app, options) {
}
const cookieLocale = this.cookies.get(cookieField, { signed: false });
let locale = this.query[queryField] || cookieLocale;
// 1. Query
let locale = this.query[queryField];
let localeOrigin = 'query';
// 2. Cookie
if (!locale) {
locale = cookieLocale;
localeOrigin = 'cookie';
}
// 3. Header
if (!locale) {
// Accept-Language: zh-CN,zh;q=0.5
// Accept-Language: zh-CN
@ -143,32 +156,36 @@ module.exports = function (app, options) {
const lang = formatLocale(languages[i]);
if (resources[lang] || localeAlias[lang]) {
locale = lang;
localeOrigin = 'header';
break;
}
}
if (!locale) {
// set the first one
locale = languages[0];
}
}
} else {
locale = languages;
localeOrigin = 'header (only one accepted language)';
}
}
// all missing, set it to defaultLocale
if (!locale) {
locale = defaultLocale;
localeOrigin = 'default';
}
}
// cookie alias
if (locale in localeAlias) locale = localeAlias[locale];
if (locale in localeAlias) {
const originalLocale = locale;
locale = localeAlias[locale];
debugSilly('Used alias, received %s but using %s', originalLocale, locale);
}
locale = formatLocale(locale);
// validate locale
if (!resources[locale]) {
debugSilly('Locale %s is not supported. Using default (%s)', locale, defaultLocale);
locale = defaultLocale;
}
@ -181,7 +198,10 @@ module.exports = function (app, options) {
maxAge: cookieMaxAge,
signed: false,
});
debugSilly('Saved cookie with locale %s', locale);
}
debug('Locale: %s from %s', locale, localeOrigin);
debugSilly('Locale: %s from %s', locale, localeOrigin);
this.__locale = locale;
return locale;
};