Skip to content

Commit

Permalink
fix #17: failed to fallback to root.
Browse files Browse the repository at this point in the history
  • Loading branch information
TooBug committed Oct 16, 2017
1 parent 1009231 commit 8a94930
Show file tree
Hide file tree
Showing 17 changed files with 184 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.history
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests
.travis.yml
.history
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ It's ok to use both `enable` and `disable`, but if any one disables a lang, the

## History

### v0.6.2 (2017-10-16)

- Fix: failed to fallback to root. [#17](https://github.com/futuweb/webpack-amdi18n-loader/issues/17)

### v0.6.1 (2017-06-29)

- Add lang files to webpack dependencies, so they can be watched and live-reloaded. See [#15](https://github.com/futuweb/webpack-amdi18n-loader/issues/15). (By jou)
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ module.exports = function (content) {

// amdi18n is the final lang definition.
var retStr = 'var amdi18n=' + JSON.stringify(ret) + ';';

// this function would be exported
// and running in browser
// it's used to determin which lang to use
Expand All @@ -153,6 +153,13 @@ module.exports = function (content) {
this[name] = target[name];
}
}

// fallback to root
for(var name in this.__root){
if(typeof this[name] === 'undefined'){
this[name] = this.__root[name];
}
}
};

// loader-related issue, nothing matters.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amdi18n-loader",
"version": "0.6.1",
"version": "0.6.2",
"description": "webpack i18n loader similar to require.js i18n plugin",
"main": "index.js",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions tests/basic/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ module.exports =
this[name] = target[name];
}
}

// fallback to root
for(var name in this.__root){
if(typeof this[name] === 'undefined'){
this[name] = this.__root[name];
}
}
};amdi18n.init();module.exports=amdi18n;

/***/ })
Expand Down
86 changes: 86 additions & 0 deletions tests/fallback/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;

/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = true;

/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }


/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;

/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(2);



/***/ }),
/* 1 */,
/* 2 */
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world","FALLBACK":"FALLBACK"},"__zh-hk":{"HELLO":"雷吼"}};amdi18n.init=function (language){
// get the default language
if(!language){
if(window._i18n && window._i18n.locale){
language = window._i18n.locale;
}else if(document.documentElement.lang){
language = document.documentElement.lang;
}else{
language = 'root';
}
}
var target = this['__' + language] || this.__root;

// copy definition to root level
if (target) {
for (var name in target) {
this[name] = target[name];
}
}

// fallback to root
for(var name in this.__root){
if(typeof this[name] === 'undefined'){
this[name] = this.__root[name];
}
}
};amdi18n.init();module.exports=amdi18n;

/***/ })
/******/ ]);
2 changes: 2 additions & 0 deletions tests/fallback/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = require('../../index?!./nls/lang');

7 changes: 7 additions & 0 deletions tests/fallback/nls/lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
root: {
HELLO: "world",
FALLBACK: "FALLBACK"
},
'zh-hk': true
};
3 changes: 3 additions & 0 deletions tests/fallback/nls/zh-hk/lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
HELLO: '雷吼'
};
12 changes: 10 additions & 2 deletions tests/format-amd/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ module.exports =
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(2);
module.exports = __webpack_require__(3);



/***/ }),
/* 1 */,
/* 2 */
/* 2 */,
/* 3 */
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"},"__en":{"HELLO":"world-en"}};amdi18n.init=function (language){
Expand All @@ -73,6 +74,13 @@ module.exports =
this[name] = target[name];
}
}

// fallback to root
for(var name in this.__root){
if(typeof this[name] === 'undefined'){
this[name] = this.__root[name];
}
}
};amdi18n.init();module.exports=amdi18n;

/***/ })
Expand Down
12 changes: 10 additions & 2 deletions tests/format-coffee/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ module.exports =
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(3);
module.exports = __webpack_require__(4);


/***/ }),
/* 1 */,
/* 2 */,
/* 3 */
/* 3 */,
/* 4 */
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"}};amdi18n.init=function (language){
Expand All @@ -73,6 +74,13 @@ module.exports =
this[name] = target[name];
}
}

// fallback to root
for(var name in this.__root){
if(typeof this[name] === 'undefined'){
this[name] = this.__root[name];
}
}
};amdi18n.init();module.exports=amdi18n;

/***/ })
Expand Down
23 changes: 15 additions & 8 deletions tests/format-json/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@ module.exports =
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/******/ ({

/***/ 0:
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(4);
module.exports = __webpack_require__(5);



/***/ }),
/* 1 */,
/* 2 */,
/* 3 */,
/* 4 */

/***/ 5:
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"}};amdi18n.init=function (language){
Expand All @@ -75,7 +74,15 @@ module.exports =
this[name] = target[name];
}
}

// fallback to root
for(var name in this.__root){
if(typeof this[name] === 'undefined'){
this[name] = this.__root[name];
}
}
};amdi18n.init();module.exports=amdi18n;

/***/ })
/******/ ]);

/******/ });
10 changes: 10 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ describe('basic function', function() {
lang.init('zh-hk');
lang.HELLO.should.equal('雷吼');
});

});

describe('fallback', function() {
global.window._i18n = {locale:'zh-hk'};
var lang = require('./fallback/bundle');
delete global.window._i18n;
it('fallback to root', function() {
lang.FALLBACK.should.equal('FALLBACK');
});
});

describe('format-amd', function() {
Expand Down
11 changes: 9 additions & 2 deletions tests/query/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ module.exports =
/***/ 0:
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(5);
module.exports = __webpack_require__(6);


/***/ }),

/***/ 5:
/***/ 6:
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__en-us":{"HELLO":"hello-us"}};amdi18n.init=function (language){
Expand All @@ -73,6 +73,13 @@ module.exports =
this[name] = target[name];
}
}

// fallback to root
for(var name in this.__root){
if(typeof this[name] === 'undefined'){
this[name] = this.__root[name];
}
}
};amdi18n.init();module.exports=amdi18n;

/***/ })
Expand Down
11 changes: 9 additions & 2 deletions tests/root-true/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ module.exports =
/***/ 0:
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(6);
module.exports = __webpack_require__(7);



/***/ }),

/***/ 6:
/***/ 7:
/***/ (function(module, exports) {

var amdi18n={"__root":{"HELLO":"world"},"__zh-cn":{"HELLO":"你好"},"__zh-hk":{"HELLO":"雷吼"}};amdi18n.init=function (language){
Expand All @@ -74,6 +74,13 @@ module.exports =
this[name] = target[name];
}
}

// fallback to root
for(var name in this.__root){
if(typeof this[name] === 'undefined'){
this[name] = this.__root[name];
}
}
};amdi18n.init();module.exports=amdi18n;

/***/ })
Expand Down
1 change: 1 addition & 0 deletions tests/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
entry:{
'basic/bundle':'./basic/main',
'fallback/bundle':'./fallback/main',
'format-amd/bundle':'./format-amd/main',
'format-json/bundle':'./format-json/main',
'format-coffee/bundle':'./format-coffee/main.coffee',
Expand Down

0 comments on commit 8a94930

Please sign in to comment.