I'm workin in a mobile application with DRF as backend and using naturaltime
built-in function, but since this is a mobile app, screen space is a big problem (atleast for me). So for an arbitrary date I get:
"1 week, 2 days ago"
Which is nice, but I wonder if it is possible get
"1w, 2d ago"
I know I could replace words in string, but I hope there's a better way. Any suggestion? Thanks!
Probably the best way to do this would be to “localize” the names. You might want to look at the source of django.contrib.humanize
.To translate, back up and change the file at django/contrib/humanize/locale/<your language>/LC_MESSAGES/django.po
, particularly starting at line 240. Then, run manage.py compilemessages
to update the .mo
files.
An alternate way to do this is without altering the Django installation is:
Add the following lines to settings.py
, unless you have already done so:
LOCALE_PATHS = ( "/path/to/my/project/locale/", )
Add a subdirectory called en
to the above path
django.po
to the en
directory you just created. naturaltime
translations into the file. manage.py compilemessages
to update the .mo
files from the directory that manage.py
is in , then wait for the changes to propagate. You can try this shortnaturaltime filter. https://github.com/ollieglass/django-shortnaturaltime
It doesn't seem like something you can customise:
https://github.com/django/django/blob/master/django/contrib/humanize/templatetags/humanize.py#L189
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.