Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language mixup on traffic #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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
Expand All @@ -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]);
Expand All @@ -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
};