Changeset ac3af33 in OpenWorkouts-current for ow/views/user.py


Ignore:
Timestamp:
Feb 26, 2019, 11:54:11 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
68d00f5
Parents:
e171dc2
Message:

(#69) Ensure month and weekday names are translated.

This is a dirty hack, which basically uses static strings marked for
translation within openworkouts, instead of relying on the python
locale and calendar translations.

We need this hack, as in some operating systems (OpenBSD), using
locale.setlocale() does not make any difference when using calendar.month_name
(names appear always in the system locale/LANG).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/user.py

    re171dc2 rac3af33  
    11import json
    2 from calendar import month_name
    32from datetime import datetime, timezone, timedelta
    43from decimal import Decimal
     
    2524    timedelta_to_hms,
    2625    get_verification_token,
    27     get_available_locale_names
     26    get_gender_names,
     27    get_available_locale_names,
     28    get_month_names,
     29    get_week_day_names
    2830)
    2931from ..mail import send_verification_email
    3032
    3133_ = TranslationStringFactory('OpenWorkouts')
     34month_name = get_month_names()
     35weekday_name = get_week_day_names()
    3236
    3337
     
    411415    name='week')
    412416def week_stats(context, request):
     417    localizer = get_localizer(request)
    413418    stats = context.week_stats
    414419    json_stats = []
    415420    for day in stats:
    416421        hms = timedelta_to_hms(stats[day]['time'])
     422        name = localizer.translate(weekday_name[day.weekday()])[:3]
    417423        day_stats = {
    418             'name': day.strftime('%a'),
     424            'name': name,
    419425            'time': str(hms[0]).zfill(2),
    420426            'distance': int(round(stats[day]['distance'])),
     
    436442    Return a json-encoded stream with statistics for the last 12 months
    437443    """
     444    localizer = get_localizer(request)
    438445    stats = context.yearly_stats
    439446    # this sets which month is 2 times in the stats, once this year, once
     
    444451    for month in stats:
    445452        hms = timedelta_to_hms(stats[month]['time'])
    446         name = month_name[month[1]][:3]
     453        name = localizer.translate(month_name[month[1]])[:3]
    447454        if month[1] == repeated_month:
    448455            name += ' ' + str(month[0])
     
    473480    in a per-week basis
    474481    """
     482    localizer = get_localizer(request)
    475483    stats = context.weekly_year_stats
    476484    # this sets which month is 2 times in the stats, once this year, once
     
    481489    for week in stats:
    482490        hms = timedelta_to_hms(stats[week]['time'])
    483         name = month_name[week[1]][:3]
     491        name = localizer.translate(month_name[week[1]])[:3]
    484492        if week[1] == repeated_month:
    485493            name += ' ' + str(week[0])
Note: See TracChangeset for help on using the changeset viewer.