From 13b36d452e96b36622898b54b53a33c1e5049a7b Mon Sep 17 00:00:00 2001 From: Ayman Chit Date: Fri, 16 Dec 2016 03:13:21 +0200 Subject: [PATCH] Locale options in the helper are being propagated Ok so i noticed if i format a specific language in my handlebars, the change is then propagated to the rest of my node project i made the following changes, and i hope this helps. i've noticed this module under a lot of different names although i posted it here for it being used under handlebars-helpers Hope it helps, Best, Ayman. The changes are mentioned below ... // moment.locale('en'); ---- So the thing is that: // The changes you do in this helper via something like {{moment lang='fr'}} // will affect your moment package settings being used in your project. if u're doing multilingual it's a problem // You want to remove this and be dependant on the locale in your project whatever it may be. // english being the default anyway ... // set the language to use // moment.locale(opts.lang); ---- Ok So The Same thing here. // you want to use the locale setting option inside the moment() function instead for it to be a one time thing if (opts.datejs === false) { return moment(new Date(str), null, opts.lang).format(pattern); // As such } ... --- index.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index e3233bd..1cb16e9 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,11 @@ var extend = require('extend-shallow'); module.exports = function momentHelper(str, pattern, options) { // if no args are passed, return a formatted date if (str == null && pattern == null) { - moment.locale('en'); + // moment.locale('en'); ---- So the thing is that: + // The changes you do in this helper via something like {{moment lang='fr'}} + // will affect your moment package settings being used in your project. if u're doing multilingual it's a problem + // You want to remove this and be dependant on the locale in your project whatever it may be. + // english being the default anyway return moment().format('MMMM DD, YYYY'); } @@ -21,16 +25,17 @@ module.exports = function momentHelper(str, pattern, options) { opts = extend({}, opts, opts.hash); // set the language to use - moment.locale(opts.lang); - + // moment.locale(opts.lang); ---- Ok So The Same thing here. + // you want to use the locale setting option inside the moment() function instead for it to be a one time thing + if (opts.datejs === false) { - return moment(new Date(str)).format(pattern); + return moment(new Date(str), null, opts.lang).format(pattern); // As such } // if both args are strings, this could apply to either lib. // so instead of doing magic we'll just ask the user to tell // us if the args should be passed to date.js or moment. if (typeof str === 'string' && typeof pattern === 'string') { - return moment(date(str)).format(pattern); + return moment(date(str), null, opts.lang).format(pattern); // Same thing here } // If handlebars, expose moment methods as hash properties @@ -39,7 +44,7 @@ module.exports = function momentHelper(str, pattern, options) { extend(opts.hash, opts.context); } - var res = moment(str); + var res = moment(str, null, opts.lang); // And here for (var key in opts.hash) { if (res[key]) { return res[key](opts.hash[key]); @@ -50,13 +55,13 @@ module.exports = function momentHelper(str, pattern, options) { } if (typeOf(str) === 'object') { - return moment(str).format(pattern); + return moment(str, null, opts.lang).format(pattern); // Here } // if only a string is passed, assume it's a date pattern ('YYYY') if (typeof str === 'string' && !pattern) { - return moment().format(str); + return moment(null, null, opts.lang).format(str); // Here } - return moment(str).format(pattern); + return moment(str, null, opts.lang).format(pattern); // And finally here };