Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 956b47d

Browse files
Working draft.
1 parent 5f08ae1 commit 956b47d

File tree

10 files changed

+2960
-1
lines changed

10 files changed

+2960
-1
lines changed

‎README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
# laravel-localization-loader
1+
# Laravel Localization loader
2+
23
Laravel Localization loader for webpack. Convert Laravel Translation strings to JavaScript Objects.
4+
5+
## Installation
6+
7+
```shell
8+
npm install laravel-localization-loader
9+
```

‎loader/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const phpArrayParser = require('./parser/php-array')
2+
const jsonParser = require('json-loader');
3+
4+
const laravelLocalizationLoader = function(source) {
5+
const isPHP = ~source.indexOf('<?php')
6+
if (isPHP) {
7+
return phpArrayParser(source)
8+
} else {
9+
return jsonParser(source)
10+
}
11+
}
12+
13+
module.exports = laravelLocalizationLoader

‎loader/parser/php-array.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const engine = require('php-parser')
2+
const parser = new engine({
3+
parser: { extractDoc: true },
4+
ast: { withPositions: true },
5+
})
6+
7+
const phpArrayParser = function (source) {
8+
const ast = parser.parseCode(source)
9+
const ret = ast.children.find((child) => child.kind === 'return')
10+
const parsed = parse(ret.expr)
11+
.reduce((acc, val) => Object.assign({}, acc, val), {})
12+
return `module.exports = ${JSON.stringify(parsed)};`
13+
}
14+
15+
function parse(expr) {
16+
switch(expr.kind) {
17+
case 'array':
18+
return expr.items.map(parse)
19+
case 'entry':
20+
return { [parse(expr.key)]: parse(expr.value) }
21+
case 'string':
22+
return expr.value
23+
case 'number':
24+
return parseInt(expr.value, 10)
25+
default:
26+
throw new Error(`Unexpected PHP token ${expr.kind}`)
27+
}
28+
}
29+
30+
module.exports = phpArrayParser

‎package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "laravel-localization-loader",
3+
"version": "1.0.0",
4+
"description": "Laravel Localization loader for webpack. Convert Laravel Translation strings to JavaScript Objects.",
5+
"main": "loader/index.js",
6+
"repository": "git@github.com:rmariuzzo/laravel-localization-loader.git",
7+
"author": "Rubens Mariuzzo <rubens@mariuzzo.com>",
8+
"license": "MIT",
9+
"scripts": {
10+
"test": "jest --config tests/jest.config.json"
11+
},
12+
"devDependencies": {
13+
"jest": "^19.0.2",
14+
"json-loader": "^0.5.4",
15+
"webpack": "^2.2.1",
16+
"webpack-merge": "^4.0.0"
17+
},
18+
"dependencies": {
19+
"php-parser": "^2.0.2"
20+
}
21+
}

‎tests/fixtures/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const message = require('./resources/lang/en/messages.php')
2+
console.log(message);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'string' => 'Rubens',
5+
'number' => 123,
6+
];

‎tests/jest.config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"verbose": true,
3+
"notify": false,
4+
"modulePathIgnorePatterns": ["/tests/output/"]
5+
}

‎tests/loader.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const path = require('path')
2+
const webpack = require('webpack')
3+
const merge = require('webpack-merge')
4+
5+
test('should load PHP array translation file', () => {
6+
return runWebpack({
7+
entry: path.join(__dirname, './fixtures/resources/lang/en/messages.php'),
8+
})
9+
.then((result) => {
10+
expect(result).toBeDefined()
11+
expect(result).toHaveProperty('string', 'Rubens')
12+
expect(result).toHaveProperty('number', 123)
13+
})
14+
})
15+
16+
function runWebpack(config) {
17+
return new Promise((resolve, reject) => {
18+
const webpackConfig = merge({
19+
output: {
20+
path: path.join(__dirname, 'output'),
21+
filename: 'translation.js',
22+
libraryTarget: 'umd',
23+
},
24+
module: {
25+
rules: [
26+
{
27+
test: /resources\/lang.+\.php$/,
28+
loader: 'laravel-localization-loader',
29+
}
30+
]
31+
},
32+
resolveLoader: {
33+
alias: {
34+
'laravel-localization-loader': path.resolve(__dirname, '../loader/index.js'),
35+
}
36+
}
37+
}, config)
38+
39+
webpack(webpackConfig, (webpackError, stats) => {
40+
const error = webpackError ||
41+
(stats.hasErrors() && stats.compilation.errors[0]) ||
42+
(stats.hasWarnings() && stats.compilation.warnings[0])
43+
if (error) {
44+
return reject(error)
45+
}
46+
47+
delete require.cache[path.resolve(__dirname, './output/translation.js')]
48+
return resolve(require('./output/translation.js'))
49+
})
50+
})
51+
}

‎tests/output/translation.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
(function webpackUniversalModuleDefinition(root, factory) {
2+
if(typeof exports === 'object' && typeof module === 'object')
3+
module.exports = factory();
4+
else if(typeof define === 'function' && define.amd)
5+
define([], factory);
6+
else {
7+
var a = factory();
8+
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
9+
}
10+
})(this, function() {
11+
return /******/ (function(modules) { // webpackBootstrap
12+
/******/ // The module cache
13+
/******/ var installedModules = {};
14+
/******/
15+
/******/ // The require function
16+
/******/ function __webpack_require__(moduleId) {
17+
/******/
18+
/******/ // Check if module is in cache
19+
/******/ if(installedModules[moduleId])
20+
/******/ return installedModules[moduleId].exports;
21+
/******/
22+
/******/ // Create a new module (and put it into the cache)
23+
/******/ var module = installedModules[moduleId] = {
24+
/******/ i: moduleId,
25+
/******/ l: false,
26+
/******/ exports: {}
27+
/******/ };
28+
/******/
29+
/******/ // Execute the module function
30+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31+
/******/
32+
/******/ // Flag the module as loaded
33+
/******/ module.l = true;
34+
/******/
35+
/******/ // Return the exports of the module
36+
/******/ return module.exports;
37+
/******/ }
38+
/******/
39+
/******/
40+
/******/ // expose the modules object (__webpack_modules__)
41+
/******/ __webpack_require__.m = modules;
42+
/******/
43+
/******/ // expose the module cache
44+
/******/ __webpack_require__.c = installedModules;
45+
/******/
46+
/******/ // identity function for calling harmony imports with the correct context
47+
/******/ __webpack_require__.i = function(value) { return value; };
48+
/******/
49+
/******/ // define getter function for harmony exports
50+
/******/ __webpack_require__.d = function(exports, name, getter) {
51+
/******/ if(!__webpack_require__.o(exports, name)) {
52+
/******/ Object.defineProperty(exports, name, {
53+
/******/ configurable: false,
54+
/******/ enumerable: true,
55+
/******/ get: getter
56+
/******/ });
57+
/******/ }
58+
/******/ };
59+
/******/
60+
/******/ // getDefaultExport function for compatibility with non-harmony modules
61+
/******/ __webpack_require__.n = function(module) {
62+
/******/ var getter = module && module.__esModule ?
63+
/******/ function getDefault() { return module['default']; } :
64+
/******/ function getModuleExports() { return module; };
65+
/******/ __webpack_require__.d(getter, 'a', getter);
66+
/******/ return getter;
67+
/******/ };
68+
/******/
69+
/******/ // Object.prototype.hasOwnProperty.call
70+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
71+
/******/
72+
/******/ // __webpack_public_path__
73+
/******/ __webpack_require__.p = "";
74+
/******/
75+
/******/ // Load entry module and return exports
76+
/******/ return __webpack_require__(__webpack_require__.s = 0);
77+
/******/ })
78+
/************************************************************************/
79+
/******/ ([
80+
/* 0 */
81+
/***/ (function(module, exports) {
82+
83+
module.exports = {"string":"Rubens","number":123};
84+
85+
/***/ })
86+
/******/ ]);
87+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /