Skip to content

Commit be2bcc2

Browse files
committed
⭐ new: support locale attr for i18n custom blocks
closes #11
1 parent c903020 commit be2bcc2

File tree

9 files changed

+86
-9
lines changed

9 files changed

+86
-9
lines changed

lib/index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
var querystring_1 = require("querystring");
34
var loader = function (source) {
45
if (this.version && Number(this.version) >= 2) {
56
try {
67
this.cacheable && this.cacheable();
7-
this.callback(null, "module.exports = " + generateCode(source));
8+
this.callback(null, "module.exports = " + generateCode(source, querystring_1.parse(this.resourceQuery)));
89
}
910
catch (err) {
1011
this.emitError(err.message);
@@ -17,11 +18,20 @@ var loader = function (source) {
1718
this.callback(new Error(message));
1819
}
1920
};
20-
function generateCode(source) {
21+
function generateCode(source, query) {
22+
var _a;
2123
var code = '';
2224
var value = typeof source === 'string'
2325
? JSON.parse(source)
24-
: source;
26+
: Buffer.isBuffer(source)
27+
? JSON.parse(source.toString())
28+
: null;
29+
if (value === null) {
30+
throw new Error('invalid source!');
31+
}
32+
if (query.locale && typeof query.locale === 'string') {
33+
value = Object.assign({}, (_a = {}, _a[query.locale] = value, _a));
34+
}
2535
value = JSON.stringify(value)
2636
.replace(/\u2028/g, '\\u2028')
2737
.replace(/\u2029/g, '\\u2029')

src/index.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import webpack from 'webpack'
2+
import { ParsedUrlQuery, parse } from 'querystring'
23

34
const loader: webpack.loader.Loader = function (source: string | Buffer): void {
5+
46
if (this.version && Number(this.version) >= 2) {
57
try {
68
this.cacheable && this.cacheable()
7-
this.callback(null, `module.exports = ${generateCode(source)}`)
9+
this.callback(null, `module.exports = ${generateCode(source, parse(this.resourceQuery))}`)
810
} catch (err) {
911
this.emitError(err.message)
1012
this.callback(err)
@@ -16,12 +18,20 @@ const loader: webpack.loader.Loader = function (source: string | Buffer): void {
1618
}
1719
}
1820

19-
function generateCode (source: string | Buffer): string {
21+
function generateCode (source: string | Buffer, query: ParsedUrlQuery): string {
2022
let code = ''
2123

2224
let value = typeof source === 'string'
2325
? JSON.parse(source)
24-
: source
26+
: Buffer.isBuffer(source)
27+
? JSON.parse(source.toString())
28+
: null
29+
if (value === null) { throw new Error('invalid source!') }
30+
31+
if (query.locale && typeof query.locale === 'string') {
32+
value = Object.assign({}, { [query.locale]: value })
33+
}
34+
2535
value = JSON.stringify(value)
2636
.replace(/\u2028/g, '\\u2028')
2737
.replace(/\u2029/g, '\\u2029')

test/__snapshots__/index.test.ts.snap

+19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ Array [
1212
]
1313
`;
1414

15+
exports[`locale attr 1`] = `
16+
Array [
17+
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
18+
]
19+
`;
20+
21+
exports[`locale attr and basic 1`] = `
22+
Array [
23+
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
24+
"{\\"ja\\":{\\"hello\\":\\"こんにちは、世界!\\"}}",
25+
]
26+
`;
27+
28+
exports[`locale attr and import 1`] = `
29+
Array [
30+
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",
31+
]
32+
`;
33+
1534
exports[`multiple 1`] = `
1635
Array [
1736
"{\\"en\\":{\\"hello\\":\\"hello world!\\"}}",

test/fixtures/locale-import.vue

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<i18n locale="en" src="./locale.json">
2+
</i18n>

test/fixtures/locale-mix.vue

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<i18n>
2+
{
3+
"en": {
4+
"hello": "hello world!"
5+
}
6+
}
7+
</i18n>
8+
9+
<i18n locale="ja">
10+
{
11+
"hello": "こんにちは、世界!"
12+
}
13+
</i18n>

test/fixtures/locale.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "hello world!"
3+
}

test/fixtures/locale.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<i18n locale="ja">
2+
{
3+
"hello": "こんにちは、世界!"
4+
}
5+
</i18n>

test/index.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,18 @@ test('import', async () => {
1919
const { module } = await bundleAndRun('import.vue')
2020
expect(module.__i18n).toMatchSnapshot()
2121
})
22+
23+
test('locale attr', async () => {
24+
const { module } = await bundleAndRun('locale.vue')
25+
expect(module.__i18n).toMatchSnapshot()
26+
})
27+
28+
test('locale attr and basic', async () => {
29+
const { module } = await bundleAndRun('locale-mix.vue')
30+
expect(module.__i18n).toMatchSnapshot()
31+
})
32+
33+
test('locale attr and import', async () => {
34+
const { module } = await bundleAndRun('locale-import.vue')
35+
expect(module.__i18n).toMatchSnapshot()
36+
})

yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -3887,9 +3887,9 @@ [email protected]:
38873887
integrity sha1-vyJJiGzlFM2hEvrpIYzcBlIR/Jw=
38883888

38893889
lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.2.1:
3890-
version "4.17.14"
3891-
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.14.tgz#9ce487ae66c96254fe20b599f21b6816028078ba"
3892-
integrity sha512-mmKYbW3GLuJeX+iGP+Y7Gp1AiGHGbXHCOh/jZmrawMmsE7MS4znI3RL2FsjbqOyMayHInjOeykW7PEajUk1/xw==
3890+
version "4.17.15"
3891+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
3892+
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
38933893

38943894
loose-envify@^1.0.0:
38953895
version "1.4.0"

0 commit comments

Comments
 (0)