Description
Moment locale was not changing according to defaultLocale cookie value. Idea was to set Moment locale in frontend-v2 itself, not from Theme, so this can be reused in all cases that needs translations. In this particular case we use moment.fromNow() function which returns human readable value from datetime value. Our starting point was:
i18n.configure({
cookie: 'defaultLocale',
directory: __dirname + translationsDir,
locales: config.get('LOCALES') || ['en'],
defaultLocale: config.get('DEFAULT_LOCALE') || 'en'
})
// also set it up for moment:
moment.locale(i18n.getLocale())
Implementation
In this solution I didn't use locales: config.get('LOCALES') || ['en'],, because i18n by default reads /i18n folder's translations files, so it overrides previous setting in i18n.configure.
Function getLocale() always returns en locale, no matter what defaultLocale value is. So I decided to use express middleware to obtain locale value for Moment:
Conclusion
This solution is tested in Montreal theme and works as we expected. One more important thing is that this do not change implementation of i18n, so there is no need to change documentation.
@anuveyatsu Please, would you review this PR.
This fixes configuring of Moment locale.
Description Moment locale was not changing according to
defaultLocale
cookie value. Idea was to set Moment locale infrontend-v2
itself, not from Theme, so this can be reused in all cases that needs translations. In this particular case we usemoment.fromNow()
function which returns human readable value fromdatetime
value. Our starting point was:Implementation
In this solution I didn't use
locales: config.get('LOCALES') || ['en'],
, becausei18n
by default reads/i18n
folder's translations files, so it overrides previous setting ini18n.configure
.Function
getLocale()
always returnsen
locale, no matter whatdefaultLocale
value is. So I decided to use express middleware to obtain locale value for Moment:Conclusion This solution is tested in Montreal theme and works as we expected. One more important thing is that this do not change implementation of
i18n
, so there is no need to change documentation.