flopp / GpxTrackPoster

Create a visually appealing poster from your GPX tracks
MIT License
412 stars 50 forks source link

Incorrect case of month names #50

Closed sikmir closed 3 years ago

sikmir commented 3 years ago

Month names are printed in the nominative case for all languages except Russian, where it should be январь instead of января in the example bellow, compare:

That's caused by: https://github.com/flopp/GpxTrackPoster/blob/2a405da9dbdb153c584beed055fd84879724a075/gpxtrackposter/calendar_drawer.py#L105 Let's check it:

$ python
>>> from datetime import datetime
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'ru_RU.UTF-8')
'ru_RU.UTF-8'
>>> datetime.today().strftime('%B')
'сентября'
yihong0618 commented 3 years ago

@sikmir

I also find something like this when translate to Chinese, for my case, I added a special case

image

flopp commented 3 years ago

So, strftime('%B') returns a wrong string when using the ru_RU.UTF-8 locale?

sikmir commented 3 years ago

So, strftime('%B') returns a wrong string when using the ru_RU.UTF-8 locale?

Yes, the reason is that strftime knows nothing about grammatical cases. Month name substituted by %B is correct in such context as strftime('%d %B') (01 сентября), but that is wrong in case of strftime('%B') (сентябрь is expected).

flopp commented 3 years ago

I guess, I will add a localization.py file with locale-aware functions that return month & day names, e.g. something like

def localized_month_name(locale_name: str, date: datetime.date) -> str:
    # special case for russian
    if locale_name == "ru_RU":
        return ...
    # default
    return date.strftime("%B")

def localized_day_of_week_name(locale_name: str, day_of_week: int, short: bool) -> str:
    # special case for chinese
    if locale_name == "zh_CN":
        ...
        return ...
    # default case
   ...
   return ...

Would that work?

yihong0618 commented 3 years ago

I think so.

sikmir commented 3 years ago

What about adding util function with all month names and maintaining their proper translations for us without relying on strftime?

flopp commented 3 years ago

@sikmir for now, I've added localization.py with functions localized_day_of_week_name and localized_month_name that use the day/month names of the current locale by default. If a language (like Russian) requires some special treatment, the functions can be adapted accordingly; I've already done this for Chinese short day names. You can now extend localized_month_name with the special cases for Russian.

sikmir commented 3 years ago

That treatment is not like for Chinese short day names, here we have no proper translations, so we need something like https://github.com/django/django/blob/master/django/utils/dates.py#L13-L17

flopp commented 3 years ago

@sikmir I've just added explicit month translations (with initial translations based on strftime('%B')).

I would be great if you could fix the Russian translation file (locale/ru_RU/LC_MESSAGES/gpxposter.po) such that the month names are correct.