Skip to content

Commit

Permalink
remove imcomplete json support and update README.md (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
u9520107 committed Nov 29, 2017
1 parent 0c2e247 commit c370b79
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
53 changes: 50 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ Sample File Structure:
|--en-US.js
|--fr-FR.js
|--localeLoader.js
|--index.js
```

Locale files
---
Can be js or json, as long as it can be imported as an object.
Does not support nested structures.

1. Must be ES6 module.
2. No template literals (``).
3. No nested structures.

```javascript
import constants from './constants';
Expand All @@ -44,3 +45,49 @@ If there is a need to not separate the bundles, the following comment can be use
```javascript
/* loadLocale noChunk */
```

I18n class
---
The ```index.js``` file in the sample structure can be used to export a I18n object.

```javascript
import I18n from 'locale-loader/lib/I18n';
import loadLocale from './loadLocale';

export default new I18n(loadLocale);
```

locale-loader
---

locale-oader is a webpack loader, this must be placed before babel-loader.


Example webpack config
```javascript
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: [
'babel-loader',
'locale-loader',
],
exclude: /node_modules/,
},
}
}
```

transformLocaleLoader
---
For building libraries and releasing, often we only compile the source to es2015 with babel transform and not webpack. The transformLocaleLoader is a gulp transform that can transform the loader files with generated code so the final result is ready to use.

gulpfile.js
```javascript
gulp.src('./src')
.pipe(transformLocaleLoader())
.pipe(babel(...babelConfig))
.pipe(gulp.dest('./build'));
```
2 changes: 1 addition & 1 deletion src/lib/generateLoaderContent/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import formatLocale from '../formatLocale';
/* global describe it */
const files = [
'en_us.js',
'FR-FR.JSON',
'FR-FR.JS',
'aa-AAAA-ZZ.JS',
];

Expand Down
3 changes: 1 addition & 2 deletions src/lib/isLocaleFile/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const localeFileRegExp = /^([a-z]{2}(-|_)([A-Z]{2}|[0-9]{3}|[A-Z][a-z]{3}(-|_)[A-Z]{2})|[a-z]{3}(-|_)[A-Z]{2})$/;
const fileRegExp = /\.(js|json)$/i;
const fileRegExp = /\.(js)$/i;

/**
* @function
Expand All @@ -9,7 +9,6 @@ const fileRegExp = /\.(js|json)$/i;
*/
export default function (filename) {
if (!fileRegExp.test(filename)) {
// no js or json file
return false;
}
const name = filename.replace(fileRegExp, '');
Expand Down
8 changes: 2 additions & 6 deletions src/lib/isLocaleFile/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ describe('isLocaleFile', () => {
'aa_AA',
'aaa_AA',
].forEach((fileName) => {
it(`should return true for ${fileName}.js or ${fileName}.json`, () => {
it(`should return true for ${fileName}.js`, () => {
expect(isLocaleFile(`${fileName}.js`)).to.equal(true);
expect(isLocaleFile(`${fileName}.json`)).to.equal(true);
});
});
[
Expand All @@ -33,9 +32,8 @@ describe('isLocaleFile', () => {
'aa3_AAe',
'aaaa_AA',
].forEach((fileName) => {
it(`should return false for ${fileName}.js or ${fileName}.json`, () => {
it(`should return false for ${fileName}.js`, () => {
expect(isLocaleFile(`${fileName}.js`)).to.equal(false);
expect(isLocaleFile(`${fileName}.json`)).to.equal(false);
});
});

Expand All @@ -50,8 +48,6 @@ describe('isLocaleFile', () => {
[
'.js',
'.JS',
'.JSON',
'.json',
].forEach((ext) => {
it(`should return true for extension "${ext}"`, () => {
expect(isLocaleFile(`en-US${ext}`)).to.equal(true);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/localeLoader/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const testFolder = './testData';

const files = [
'en_us.js',
'FR-FR.JSON',
'FR-FR.JS',
'aa-AAAA-ZZ.JS',
];

Expand Down
2 changes: 1 addition & 1 deletion src/lib/transformLocaleLoader/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const testFolder = './testData';

const files = [
'en_us.js',
'FR-FR.JSON',
'FR-FR.JS',
'aa-AAAA-ZZ.JS',
];

Expand Down

0 comments on commit c370b79

Please sign in to comment.