Skip to content

Commit

Permalink
refactor: use async function and support egg@2 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Nov 10, 2017
1 parent 3ef32ac commit 80e591d
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 75 deletions.
1 change: 0 additions & 1 deletion .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = {
],
devdep: [
'autod',
'egg',
'egg-bin',
'egg-ci',
'egg-mock',
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: false
language: node_js
node_js:
- '6'
- '7'
- '8'
- '9'
install:
- npm i npminstall && npminstall
script:
Expand Down
7 changes: 3 additions & 4 deletions app/middleware/i18n.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';

module.exports = () => {
return function* i18n(next) {
const ctx = this;
return function i18n(ctx, next) {
function gettext() {
return ctx.__.apply(ctx, arguments);
}
this.locals = {
ctx.locals = {
gettext,
__: gettext,
};

yield next;
return next();
};
};
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- nodejs_version: '6'
- nodejs_version: '7'
- nodejs_version: '8'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
Expand All @@ -10,6 +10,6 @@ install:
test_script:
- node --version
- npm --version
- npm run ci
- npm run test

build: off
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@
},
"homepage": "https:/eggjs/egg-i18n#readme",
"engines": {
"node": ">= 6.0.0"
"node": ">= 8.0.0"
},
"files": [
"app",
"config",
"app.js"
],
"ci": {
"version": "6, 7"
"version": "8, 9"
},
"dependencies": {
"debug": "^2.6.0",
"koa-locales": "^1.5.2"
"debug": "^3.1.0",
"koa-locales": "^1.7.0"
},
"devDependencies": {
"autod": "^2.7.1",
"egg": "^0.7.0",
"egg-bin": "^1.10.0",
"egg-ci": "^1.1.0",
"egg-mock": "^2.0.0",
"eslint": "^3.13.1",
"eslint-config-egg": "^3.2.0",
"pedding": "^1.1.0",
"supertest": "^2.0.1"
"autod": "^2.10.1",
"egg": "next",
"egg-bin": "^4.3.5",
"egg-ci": "^1.8.0",
"egg-mock": "^3.13.1",
"egg-view-nunjucks": "^2.1.4",
"eslint": "^4.10.0",
"eslint-config-egg": "^5.1.1",
"pedding": "^1.1.0"
}
}
}
25 changes: 12 additions & 13 deletions test/fixtures/apps/i18n/app/controller/message.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict';

module.exports = function*() {
this.body = {
message: this.__('Hello %s, how are you today? How was your %s.', 'fengmk2', 18),
empty: this.__(),
notexists_key: this.__('key not exists'),
empty_string: this.__(''),
novalue: this.__('key %s ok'),
// __ 别名 gettext 也可以使用
arguments3: this.gettext('%s %s %s', 1, 2, 3),
arguments4: this.gettext('%s %s %s %s', 1, 2, 3, 4),
arguments5: this.__('%s %s %s %s %s', 1, 2, 3, 4, 5),
arguments6: this.__('%s %s %s %s %s.', 1, 2, 3, 4, 5, 6),
values: this.__('{0} {1} {0} {1} {2} {100}', [ 'foo', 'bar' ]),
module.exports = async ctx => {
ctx.body = {
message: ctx.__('Hello %s, how are you today? How was your %s.', 'fengmk2', 18),
empty: ctx.__(),
notexists_key: ctx.__('key not exists'),
empty_string: ctx.__(''),
novalue: ctx.__('key %s ok'),
arguments3: ctx.gettext('%s %s %s', 1, 2, 3),
arguments4: ctx.gettext('%s %s %s %s', 1, 2, 3, 4),
arguments5: ctx.__('%s %s %s %s %s', 1, 2, 3, 4, 5),
arguments6: ctx.__('%s %s %s %s %s.', 1, 2, 3, 4, 5, 6),
values: ctx.__('{0} {1} {0} {1} {2} {100}', [ 'foo', 'bar' ]),
};
};
14 changes: 7 additions & 7 deletions test/fixtures/apps/loader/app/router.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';

module.exports = app => {
app.get('/', function*() {
this.body = this.__(this.query.key);
app.get('/', ctx => {
ctx.body = ctx.__(ctx.query.key);
});

app.get('/renderString', function*() {
const tpl = `<li>\${__('Email')}: \${user.email || ''}</li>
<li>\${gettext('Hello %s, how are you today?', user.name)}</li>
<li>\${__('%s %s', 'foo', 'bar')}</li>
app.get('/renderString', async ctx => {
const tpl = `<li>\{{__('Email')}}: \{{user.email}}</li>
<li>\{{gettext('Hello %s, how are you today?', user.name)}}</li>
<li>\{{__('%s %s', 'foo', 'bar')}}</li>
`;

this.body = yield this.renderString(tpl, {
ctx.body = await ctx.renderString(tpl, {
user: {
name: 'fengmk2',
},
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/apps/loader/config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ exports.keys = 'loader';
exports.i18n = {
dirs: [ path.join(__dirname, 'locales2') ],
};
exports.view = {
defaultViewEngine: 'nunjucks',
};
4 changes: 4 additions & 0 deletions test/fixtures/apps/loader/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ module.exports = {
enable: true,
path: path.join(__dirname, '../c'),
},
nunjucks: {
enable: true,
package: 'egg-view-nunjucks',
},
};
16 changes: 0 additions & 16 deletions test/fixtures/custom_egg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,10 @@ const egg = require('egg');
const EGG_PATH = __dirname;
const startCluster = egg.startCluster;

class View {
* render() {
throw new Error('not implements');
}
/* eslint-disable no-new-func */
* renderString(tpl, locals) {
const render = new Function('local', `with(local) { return \`${tpl}\` }`);
return render(locals);
}
/* eslint-enable no-new-func */
}

class CustomApplication extends egg.Application {
get [Symbol.for('egg#eggPath')]() {
return EGG_PATH;
}

get [Symbol.for('egg#view')]() {
return View;
}
}

class BeggAgent extends egg.Agent {
Expand Down
29 changes: 14 additions & 15 deletions test/i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const mm = require('egg-mock');
const assert = require('assert');
const pedding = require('pedding');
const join = require('path').join;
const request = require('supertest');

describe('test/i18n.test.js', () => {

Expand All @@ -20,7 +19,7 @@ describe('test/i18n.test.js', () => {
after(() => app.close());

it('should return locale de', done => {
request(app.callback())
app.httpRequest()
.get('/message?locale=de')
.expect(200)
.expect('Set-Cookie', /locale=de; path=\/; expires=[^;]+ GMT/)
Expand All @@ -39,7 +38,7 @@ describe('test/i18n.test.js', () => {
});

it('should return default locale en_US', function(done) {
request(app.callback())
app.httpRequest()
.get('/message?locale=')
.expect(200)
.expect('Set-Cookie', /locale=en-us; path=\/; expires=[^;]+ GMT/)
Expand Down Expand Up @@ -96,51 +95,51 @@ describe('test/i18n.test.js', () => {
after(() => app.close());

it('should return locale from plugin a', function(done) {
request(app.callback())
app.httpRequest()
.get('/?key=pluginA')
.set('Accept-Language', 'zh-CN,zh;q=0.5')
.expect('true', done);
});

it('should return locale from plugin b', function(done) {
request(app.callback())
app.httpRequest()
.get('/?key=pluginB')
.set('Accept-Language', 'zh-CN,zh;q=0.5')
.expect('true', done);
});

it('should return locale from framework', function(done) {
request(app.callback())
app.httpRequest()
.get('/?key=framework')
.set('Accept-Language', 'zh-CN,zh;q=0.5')
.expect('true', done);
});

it('should return locale from locales2', function(done) {
request(app.callback())
app.httpRequest()
.get('/?key=locales2')
.set('Accept-Language', 'zh-CN,zh;q=0.5')
.expect('true', done);
});

it('should use locale/ when both exist locales/ and locale/', function(done) {
request(app.callback())
app.httpRequest()
.get('/?key=pluginC')
.set('Accept-Language', 'zh-CN,zh;q=0.5')
.expect('i18n form locale', done);
});

describe('view renderString with __(key, value)', () => {
it('should render with default locale: en-US', function(done) {
request(app.callback())
app.httpRequest()
.get('/renderString')
.expect(200)
.expect('Set-Cookie', /locale=en-us; path=\/; expires=[^;]+ GMT/)
.expect('<li>Email: </li>\n<li>Hello fengmk2, how are you today?</li>\n<li>foo bar</li>\n', done);
});

it('should render with query locale: zh_CN', function(done) {
request(app.callback())
app.httpRequest()
.get('/renderString?locale=zh_CN')
.expect(200)
.expect('Set-Cookie', /locale=zh-cn; path=\/; expires=[^;]+ GMT/)
Expand All @@ -152,21 +151,21 @@ describe('test/i18n.test.js', () => {
// Accept-Language: zh-CN
it('should render with Accept-Language: zh-CN,zh;q=0.5', function(done) {
done = pedding(3, done);
request(app.callback())
app.httpRequest()
.get('/renderString')
.set('Accept-Language', 'zh-CN,zh;q=0.5')
.expect(200)
.expect('Set-Cookie', /locale=zh-cn; path=\/; expires=[^;]+ GMT/)
.expect('<li>邮箱: </li>\n<li>fengmk2,今天过得如何?</li>\n<li>foo bar</li>\n', done);

request(app.callback())
app.httpRequest()
.get('/renderString')
.set('Accept-Language', 'zh-CN;q=1')
.expect(200)
.expect('Set-Cookie', /locale=zh-cn; path=\/; expires=[^;]+ GMT/)
.expect('<li>邮箱: </li>\n<li>fengmk2,今天过得如何?</li>\n<li>foo bar</li>\n', done);

request(app.callback())
app.httpRequest()
.get('/renderString')
.set('Accept-Language', 'zh_cn')
.expect(200)
Expand All @@ -175,7 +174,7 @@ describe('test/i18n.test.js', () => {
});

it('should render set cookie locale: zh-CN if query locale not equal to cookie', function(done) {
request(app.callback())
app.httpRequest()
.get('/renderString?locale=en-US')
.set('Cookie', 'locale=zh-CN')
.expect(200)
Expand All @@ -184,7 +183,7 @@ describe('test/i18n.test.js', () => {
});

it('should render with cookie locale: zh-cn', () => {
return request(app.callback())
return app.httpRequest()
.get('/renderString')
.set('Cookie', 'locale=zh-cn')
.expect(200)
Expand Down

0 comments on commit 80e591d

Please sign in to comment.