diff --git a/lib/multilang.js b/lib/multilang.js index 92a2a3b..0de6e03 100644 --- a/lib/multilang.js +++ b/lib/multilang.js @@ -6,7 +6,7 @@ var extname = require('path').extname; function Multilang(ops) { this.default = ops.default; this.locales = ops.locales; - this.pattern = RegExp('.*_('+ ops.locales.join('|') +')(?:\..*)?$'); + this.pattern = RegExp('.*_('+ ops.locales.join('|') +')(?=\\..*?$)'); this.pathPattern = RegExp('(^(' + ops.locales.join('|') +')/|/(' + ops.locales.join('|') +')/)'); } @@ -15,8 +15,8 @@ Multilang.prototype.getAltFilename = function (file, fromLocale, toLocale) { // Locale in the path. if (this.pathPattern.test(file)) { - var replacementString = file.match(this.pathPattern)[0].replace(fromLocale, toLocale); - return file.replace(this.pathPattern, replacementString); + var replacementString = file.match(this.pathPattern)[0].replace(fromLocale, toLocale); + return file.replace(this.pathPattern, replacementString); } // Locale in the filename. @@ -33,11 +33,11 @@ Multilang.prototype.getBaseFilename = function (file) { // Locale in the path. if (this.pathPattern.test(file)) { - var replacementString = file.match(this.pathPattern)[0].replace( - RegExp('(/?)('+ this.locales.join('|') +')(/)'), - '$1' + this.default + '$3' - ); - return file.replace(this.pathPattern, replacementString); + var replacementString = file.match(this.pathPattern)[0].replace( + RegExp('(/?)('+ this.locales.join('|') +')(/)'), + '$1' + this.default + '$3' + ); + return file.replace(this.pathPattern, replacementString); } // Locale in the filename. @@ -49,8 +49,8 @@ Multilang.prototype.getLocale = function (file) { // Locale in the path. if (this.pathPattern.test(file)) { return file.match(this.pathPattern)[0].replace( - RegExp('(/?)('+ this.locales.join('|') +')(/)'), - '$2' + RegExp('(/?)('+ this.locales.join('|') +')(/)'), + '$2' ); } @@ -71,7 +71,7 @@ Multilang.prototype.getPlugin = function () { return function (files, ms, done) { ms.metadata().locales = self.locales.reduce((locObj, locale) => - Object.assign(locObj, {[locale]: []}), {}); + Object.assign(locObj, {[locale]: {}}), {}); ms.metadata().defaultLocale = self.default; for (var file in files) { @@ -84,9 +84,11 @@ Multilang.prototype.getPlugin = function () { // This lets to have base some generic properties // applied only in the 'default' locale, e.g.: template if (base !== file) { - var contents = files[file].contents; - files[file] = deepmerge(files[base], files[file], {clone:true}); - files[file].contents = contents; + if(files[base] && files[file]){ + var contents = files[file].contents; + files[file] = deepmerge(files[base], files[file], {clone:true}); + files[file].contents = contents; + } } } else { files[file].locale = self.default; @@ -105,7 +107,7 @@ Multilang.prototype.getPlugin = function () { files[file].lang = lang.bind(files[file]); // Ad file to locale index. - ms.metadata().locales[files[file].locale].push(files[file]); + ms.metadata().locales[files[file].locale][file] = files[file]; } // Index handling @@ -113,9 +115,10 @@ Multilang.prototype.getPlugin = function () { // Other index-es in '/:locale/index.html' for (file of Object.keys(files)) { if (files[file].index) { - var name = file.split('/').pop().replace(/_.*(?=\.)/, ''); + var name = file.replace(this.pattern, ''); if (files[file].locale === self.default) { files[file].path = ''; + name = name.substr(name.lastIndexOf('/') + 1);1 files[name] = Object.assign({},files[file]); } else { files[file].path = files[file].locale +'/';