1

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!

Gocht
  • 9,924
  • 3
  • 42
  • 81

3 Answers3

3

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.
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
  • This sounds pro, I like it. But when I update the code in the production server, will this take effect? What if I take the `humanize.py` and rewrite the involved code? – Gocht Sep 03 '15 at 01:29
  • If you update `humanize.py`, it will invalidate the translation files because you have changed the `msgid` text.. This should probably take effect on the production server if you run `manage.py compilemessages` unless you are using caches. If you are using caches, you may have to wait for them to expire. – Jed Fox Sep 03 '15 at 10:10
  • `.mo` files are compiled translations that are not meant to be edited. A `.po` file contains, for each translation, a line starting with `msgid` that is the text in the translation function, and a line starting with `msgstr` that is the translated version. There are also more complicated syntaxes for different things, including pluralization. – Jed Fox Sep 03 '15 at 15:18
  • So if I modifiy something I could broken translations or pluralizations..? – Gocht Sep 03 '15 at 15:36
  • Only if you changed the text in the translation strings ([example](https://github.com/django/django/blob/master/django/contrib/humanize/templatetags/humanize.py#L235-L239)). – Jed Fox Sep 03 '15 at 17:09
  • I like your solution, but I am not sure the future consequences. – Gocht Sep 03 '15 at 17:18
  • What do you mean by “future consequences?” – Jed Fox Sep 03 '15 at 18:12
  • `django/contrib/humanize/locale//LC_MESSAGES/django.po` is not tracked by git. I have to do the same in my server? – Gocht Sep 03 '15 at 19:33
  • What do you mean by “not tracked by git?” If you are using the GitHub repo, the URL for the English one is [django/django/blob/master/django/contrib/humanize/locale/en/LC_MESSAGES/django.po](https://github.com/django/django/blob/master/django/contrib/humanize/locale/en/LC_MESSAGES/django.po) – Jed Fox Sep 03 '15 at 21:30
  • I mean, you suggest I modify a django file, but those files are not the same that in my production server, because in my prod. server there's another django installation, so I would have to do the same modification in my prod. server, right? – Gocht Sep 03 '15 at 21:32
  • Yes. you would also have to modify the production server’s installation. – Jed Fox Sep 03 '15 at 21:34
  • And every new installation... And if Django changes something could stop working. – Gocht Sep 03 '15 at 21:38
  • Wow, it looks nice and makes sense. Should I copy the lines that you highlighted or the complete file? – Gocht Sep 03 '15 at 22:16
  • If you just want to customize `naturaltime`, those lines will suffice. – Jed Fox Sep 03 '15 at 22:25
  • I can not give it a try right now, because I am working in other issue, but I will and accept it if it works. Thank you for your patience and support. – Gocht Sep 03 '15 at 22:52
1

You can try this shortnaturaltime filter. https://github.com/ollieglass/django-shortnaturaltime

kanatti
  • 775
  • 7
  • 10
0

It doesn't seem like something you can customise:

https://github.com/django/django/blob/master/django/contrib/humanize/templatetags/humanize.py#L189

satoru
  • 31,822
  • 31
  • 91
  • 141