I am trying to change the language of the date which is being set by moment.js. The default one is English, but I want to set the German language. These is what I tried:
With momentjs 2.8+, do the following:
moment.locale("de").format('LLL');
http://momentjs.com/docs/#/i18n/
Change the moment js language as per Version
Version: 2.8+
moment.locale('hi');
Version: 2.5.1
moment.lang('hi');
As I was using webpack with gulp and friends (this generator set up everything for me) I had to make a change to the bower.json file. I had to override the default import for the moment package and select the file that comes with all the languages:
"overrides": {
"moment": {
"main": [
"min/moment-with-locales.min.js"
]
}
}
This is my full bower.json file:
{
"name": "html5",
"version": "0.0.0",
"dependencies": {
"angular-animate": "~1.4.2",
"angular-cookies": "~1.4.2",
"angular-touch": "~1.4.2",
"angular-sanitize": "~1.4.2",
"angular-messages": "~1.4.2",
"angular-ui-router": "~0.2.15",
"bootstrap-sass": "~3.3.5",
"angular-bootstrap": "~0.13.4",
"malarkey": "yuanqing/malarkey#~1.3.1",
"angular-toastr": "~1.5.0",
"moment": "~2.10.6",
"animate.css": "~3.4.0",
"angular": "~1.4.2",
"lodash": "^4.13.1",
"angular-moment": "^0.10.3",
"angularLocalStorage": "ngStorage#^0.3.2",
"ngstorage": "^0.3.10"
},
"devDependencies": {
"angular-mocks": "~1.4.2"
},
"overrides": {
"bootstrap-sass": {
"main": [
"assets/stylesheets/_bootstrap.scss",
"assets/fonts/bootstrap/glyphicons-halflings-regular.eot",
"assets/fonts/bootstrap/glyphicons-halflings-regular.svg",
"assets/fonts/bootstrap/glyphicons-halflings-regular.ttf",
"assets/fonts/bootstrap/glyphicons-halflings-regular.woff",
"assets/fonts/bootstrap/glyphicons-halflings-regular.woff2"
]
},
"moment": {
"main": [
"min/moment-with-locales.min.js"
]
}
},
"resolutions": {
"angular": "~1.4.2"
}
}
For those working in asynchronous environments, moment
behaves unexpectedly when loading locales on demand.
Instead of
await import('moment/locale/en-ca');
moment.locale('en-ca');
reverse the order
moment.locale('en-ca');
await import('moment/locale/en-ca');
It seems like the locales are loaded into the current selected locale, overriding any previously set locale information. So switching the locale first, then loading the locale information does not cause this issue.
I just installed moment with bower and linked de.js
as javascript resource in my html project.
bower install moment --save
You can also manually download the moment.js
and de.js
.
Linking the de.js
in my main project file automatically changed the locale for all accesses to the moment class and its methods.
There will be no need anymore to do a moment.locale("de").
or
moment.lang("de").
in the source code.
Just link your desired locale like this:
<script src="/bower_components/moment/moment.js"></script>
<script src="/bower_components/moment/locale/de.js"></script>
Or you can link the libraries without the bower_components
path, if you downloaded moment.js 1990ies-style via right-click, which still works fine in most scenarios.
work fine like that: return moment(status.created_at).locale('es').fromNow();