0

Is there any possibility in Python to create below conditional string formatting.
At the beginning I create this dictionary of different language messages:

MB_MESSAGE = {
    'en_EN': '{0} day/days ago.\n',
    'pl_PL': '{0} dzień minął/dni minęło',
}  

I would like to achieve selecting either day or days basing on number of days, same for polish language dzień minął for 1 day and dni mnięło for 2 or more days.
In later code I use this dict as shown below:

import locale
lang = locale.getdefaultlocale()
message = MB_MESSAGE[lang].format(days_between(d1, d2))

I use Python35.

Marek
  • 1,189
  • 3
  • 13
  • 33
  • More importantly, [this](https://docs.python.org/3/library/stdtypes.html#str.format_map) particular mention – idjaw Feb 27 '16 at 15:28
  • There is a generally used library called gettext (GNU). It is a bit more complex than some ad-hoc simple formatting but it does also what you need. Search for "Additional functions for plural forms".There is the standard Python module `gettext`. The usual way is to build external files with the text definitions (separated from the source code). String in one language is used in the source code, and it is wrapped to translate it using the gettext library. I suggest to use Poedit (available for free). The text at the home page says, that also Django uses the gettext. – pepr Feb 27 '16 at 16:16
  • The question should not have been closed with the given reasoning. It is not related to a simple English pluralization. It is related also to internationalization. – pepr Feb 27 '16 at 16:18
  • Of course you know that "2 dni minęło" is an incorrect grammar. It is "2 dni minęły", but "5 dni minęło". I can speak Polish, what's your superpower? – Tadeusz Kopec for Ukraine Feb 27 '16 at 17:26
  • kurde, faktycznie :D – Marek Feb 28 '16 at 01:22

0 Answers0