Skip to content

Commit 3fca5d4

Browse files
committed
new babel and updated localeswitch
1 parent 7e0d450 commit 3fca5d4

File tree

4 files changed

+108
-67
lines changed

4 files changed

+108
-67
lines changed

gulpfile.js

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
import gulp from 'gulp';
2-
import mocha from 'gulp-mocha';
32
import babel from 'gulp-babel';
4-
import istanbul from 'gulp-istanbul';
5-
import coveralls from 'gulp-coveralls';
3+
import jsxCoverage from 'gulp-jsx-coverage';
64
import path from 'path';
7-
import { Instrumenter } from 'isparta';
85

9-
const babelConfig = {
10-
stage: 0,
11-
};
6+
gulp.task('test', jsxCoverage.createTask({
7+
src: ['./tests/**/*{.js,.jsx}'], // will pass to gulp.src as mocha tests
8+
istanbul: { // will pass to istanbul or isparta
9+
preserveComments: true, // required for istanbul 0.4.0+
10+
coverageVariable: '__MY_TEST_COVERAGE__',
11+
exclude: /node_modules|test[0-9]/ // do not instrument these files
12+
},
1213

13-
gulp.task('pre-test', () => {
14-
return gulp.src(['dist/**/*{.js,.jsx}'])
15-
.pipe(istanbul({
16-
dir: './coverage',
17-
instrumenter: Instrumenter,
18-
includeUntested: true,
19-
}))
20-
.pipe(istanbul.hookRequire());
21-
});
14+
threshold: 80, // fail the task when coverage lower than this
15+
// default is no threshold
16+
thresholdType: 'lines', // one of 'lines', 'statements', 'functions', 'banches'
17+
// default is 'lines'
18+
transpile: { // this is default whitelist/blacklist for transpilers
19+
babel: {
20+
include: /\.jsx?$/,
21+
exclude: /node_modules/,
22+
omitExt: false // if you wanna omit file ext when require(), put an array
23+
}, // of file exts here. Ex: ['.jsx', '.es6'] (NOT RECOMMENDED) // of file exts here. Ex: ['.cjsx'] (NOT RECOMMENDED)
24+
},
25+
coverage: {
26+
reporters: ['text-summary', 'json', 'lcov'], // list of istanbul reporters
27+
directory: 'coverage' // will pass to istanbul reporters
28+
},
29+
mocha: { // will pass to mocha
30+
reporter: 'spec'
31+
},
32+
}));
2233

23-
gulp.task('test', ['pre-test'], () => {
24-
return gulp.src('./tests/**/*.js')
25-
.pipe(babel(babelConfig))
26-
.pipe(mocha({
27-
timeout: 20000,
28-
reporter: 'spec',
29-
}))
30-
.pipe(istanbul.writeReports())
31-
.pipe(istanbul.enforceThresholds({
32-
thresholds: {
33-
global: 50,
34-
},
35-
}));
34+
gulp.task('build', () => {
35+
return gulp.src('./src/**/*.{js,jsx}')
36+
.pipe(babel())
37+
.pipe(gulp.dest('./dist'));
3638
});
3739

40+
/*
41+
3842
gulp.task('coveralls', ['test'], () => {
3943
if (!process.env.CI) {
4044
return void 0;
@@ -43,13 +47,12 @@ gulp.task('coveralls', ['test'], () => {
4347
return gulp.src(path.join(__dirname, 'coverage/lcov.info'))
4448
.pipe(coveralls());
4549
});
46-
47-
gulp.task('build', () => {
48-
return gulp.src('./src/**/*.{js,jsx}')
49-
.pipe(babel(babelConfig))
50-
.pipe(gulp.dest('./dist'));
51-
});
50+
*/
5251

5352
gulp.doneCallback = (err) => {
5453
process.exit(err ? 1 : 0);
5554
};
55+
56+
57+
58+

package.json

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-translate-maker",
3-
"version": "0.0.32",
3+
"version": "0.1.0",
44
"description": "Universal internationalization (i18n) open source library for React",
55
"main": "dist/index.js",
66
"keywords": [
@@ -32,7 +32,6 @@
3232
"prepublish": "npm run build",
3333
"test": "babel-node ./node_modules/gulp/bin/gulp.js test",
3434
"build": "babel-node ./node_modules/gulp/bin/gulp.js build",
35-
"coveralls": "babel-node ./node_modules/gulp/bin/gulp.js coveralls",
3635
"eslint": "node ./node_modules/eslint/bin/eslint.js --ext .js,.jsx ."
3736
},
3837
"repository": {
@@ -49,23 +48,29 @@
4948
"translate-maker": "^0.2.42"
5049
},
5150
"devDependencies": {
52-
"babel": "^5.8.34",
51+
"babel-cli": "^6.4.0",
52+
"babel-core": "^6.4.0",
5353
"babel-eslint": "^4.1.5",
54-
"eslint": "^1.10.1",
54+
"babel-plugin-transform-decorators": "^6.4.0",
55+
"babel-plugin-transform-class-properties": "^6.4.0",
56+
"babel-preset-es2015": "^6.3.13",
57+
"babel-preset-react": "^6.3.13",
58+
"babel-preset-stage-0": "^6.3.13",
59+
"babel-preset-stage-1": "^6.3.13",
60+
"eslint": "^1.10.3",
5561
"eslint-config-airbnb": "^0.1.1",
56-
"eslint-loader": "^1.1.1",
57-
"eslint-plugin-react": "^3.10.0",
62+
"eslint-loader": "^1.2.0",
63+
"eslint-plugin-react": "^3.15.0",
5864
"gulp": "^3.9.0",
59-
"gulp-mocha": "^2.2.0",
60-
"gulp-babel": "^5.2.1",
65+
"gulp-babel": "^6.1.1",
66+
"gulp-jsx-coverage": "^0.3.7",
6167
"gulp-util": "^3.0.7",
62-
"should": "^7.1.1",
63-
"react": "^0.14.5",
64-
"react-dom": "^0.14.5",
65-
"gulp-istanbul": "^0.10.2",
66-
"gulp-coveralls": "^0.1.4",
67-
"coveralls": "^2.11.4",
68-
"isparta": "^3.5.3"
68+
"react": "^0.14.6",
69+
"react-addons-test-utils": "^0.14.6",
70+
"react-dom": "^0.14.6",
71+
"should": "^8.1.1",
72+
"mocha": "^2.3.4",
73+
"jsdom": "^3.1.2"
6974
},
7075
"peerDependencies": {
7176
"react": "0.14.x"

src/LocaleProvider.jsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export default class LocaleProvider extends Component {
1616
variables: PropTypes.bool,
1717
combinations: PropTypes.bool,
1818
filters: PropTypes.object,
19+
controlled: PropTypes.bool,
20+
};
21+
22+
static defaultProps = {
23+
controlled: true,
1924
};
2025

2126
static childContextTypes = {
@@ -26,18 +31,40 @@ export default class LocaleProvider extends Component {
2631
constructor(props, context) {
2732
super(props, context);
2833

34+
const translate = new Translate(props);
35+
2936
this.state = {
30-
translate: new Translate(props),
37+
translate,
38+
locale: translate.getLocale(),
3139
};
3240
}
3341

42+
componentDidMount() {
43+
const { translate } = this.state;
44+
45+
translate.on('locale', this._localeChanged);
46+
}
47+
48+
componentWillUnmount() {
49+
const { translate } = this.state;
50+
51+
translate.removeListener('locale', this._localeChanged);
52+
}
53+
54+
_localeChanged = (locale) => {
55+
this.setState({ locale });
56+
};
57+
3458
componentWillReceiveProps(newProps) {
35-
this.state.translate.setLocale(newProps.locale);
59+
if (newProps.locale && newProps.controlled) {
60+
this.state.translate.setLocale(newProps.locale);
61+
// TODO add other props
62+
}
3663
}
3764

3865
t = (path, attrs, defaultValue) => {
3966
return this.get(path, attrs, defaultValue);
40-
}
67+
};
4168

4269
get(path, attrs, defaultValue) {
4370
const { translate } = this.state;
@@ -54,6 +81,10 @@ export default class LocaleProvider extends Component {
5481
}
5582

5683
setLocale(locale, callback) {
84+
if (this.props.controlled) {
85+
return callback(new Error('LocaleProvider is set as controlled component. If you want to use setLocale please set controlled = false'));
86+
}
87+
5788
return this.state.translate.setLocale(locale, callback);
5889
}
5990

src/LocaleSwitch.jsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default class LocaleSwitch extends Component {
66
...LocaleProvider.childContextTypes,
77
};
88

9-
static defaultValue = {
9+
static defaultProps = {
1010
setLocale: true,
1111
};
1212

@@ -17,31 +17,33 @@ export default class LocaleSwitch extends Component {
1717
name: PropTypes.string,
1818
className: PropTypes.string,
1919
onChange: PropTypes.func,
20+
onError: PropTypes.func,
2021
};
2122

2223
handleChange(evn) {
2324
evn.stopPropagation();
2425

25-
const nodes = evn.target.options || [];
26+
const { locales, onChange, onError, setLocale } = this.props;
27+
const translate = this.context.translate;
2628

27-
for (let index = 0; index < nodes.length; index++) {
28-
const node = nodes[index];
29-
if (!node.selected) {
30-
continue;
31-
}
29+
const value = evn.target.value;
3230

33-
const value = node.value;
34-
if (!value) {
31+
for (let index = 0; index < locales.length; index++) {
32+
const { locale, label } = locales[index];
33+
if (locale !== value) {
3534
continue;
3635
}
3736

38-
if (this.props.onChange) {
39-
this.props.onChange(value);
37+
if (setLocale && !translate.props.controlled) {
38+
translate.setLocale(locale, (err) => {
39+
if (err && onError) {
40+
onError(err);
41+
}
42+
});
4043
}
4144

42-
if (this.props.setLocale) {
43-
const translate = this.context.translate;
44-
translate.setLocale(value);
45+
if (onChange) {
46+
onChange(locale);
4547
}
4648

4749
return;

0 commit comments

Comments
 (0)