简体   繁体   中英

'd' instead of 'days' in Django naturaltime

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.

EDIT:

An alternate way to do this is without altering the Django installation is:

  1. Add the following lines to settings.py , unless you have already done so:

     LOCALE_PATHS = ( "/path/to/my/project/locale/", ) 
  2. Add a subdirectory called en to the above path

  3. Add a file called django.po to the en directory you just created.
  4. Paste the contents of the naturaltime translations into the file.
  5. Change them to your liking.
  6. Run 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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM