Changeset ac3af33 in OpenWorkouts-current


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).

Location:
ow
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ow/templates/dashboard.pt

    re171dc2 rac3af33  
    2525      <section class="workout-list">
    2626
    27         <h3 tal:condition="viewing_month is not None">
    28           <tal:d tal:content="month_name[viewing_month] + ' ' + str(viewing_year)"></tal:d> -
     27        <h3 tal:condition="viewing_month is not None"
     28            tal:define="localizer get_localizer(request)">
     29          <tal:d tal:content="localizer.translate(month_name[viewing_month]) + ' ' + str(viewing_year)"></tal:d> -
    2930          <tal:n tal:content="len(workouts)"></tal:n>
    3031          <tal:t i18n:translate="">Workouts</tal:t>
     
    174175                              <tal:months tal:repeat="month sorted(tree[year].keys())">
    175176                                  <li tal:define="is_viewing_month is_viewing_year and month == viewing_month">
    176                                       <a href=""
    177                                          tal:attributes="href request.resource_url(context, query={'year': year, 'month': month});
    178                                                          class 'viewing-month' if is_viewing_month else ''">
    179                                            <span tal:content="month_name[month]"></span>
    180                                            <span tal:content="sum([tree[year][month][sport] for sport in tree[year][month]])"></span>
    181                                       </a>
     177                                    <a href=""
     178                                       tal:attributes="href request.resource_url(context, query={'year': year, 'month': month});
     179                                             class 'viewing-month' if is_viewing_month else ''"
     180                                       tal:define="localizer get_localizer(request)">
     181                                      <span tal:content="localizer.translate(month_name[month])"></span>
     182                                      <span tal:content="sum([tree[year][month][sport] for sport in tree[year][month]])"></span>
     183                                    </a>
    182184                                  </li>
    183185                              </tal:months>
  • ow/utilities.py

    re171dc2 rac3af33  
    282282
    283283
     284def get_month_names():
     285    """
     286    Return a list with the names of the months, marked for translation.
     287
     288    This should be done automatically by the calendar module:
     289
     290    >>> import calendar
     291    >>> calendar.month_name[1]
     292    'January'
     293    >>>
     294
     295    But even trying setting the proper locale (using locale.setlocale()),
     296    in some operating systems the names are not translated (OpenBSD).
     297
     298    So, for now, we use this dirty trick
     299    """
     300    return [
     301        '',
     302        _('January'),
     303        _('February'),
     304        _('March'),
     305        _('April'),
     306        _('May'),
     307        _('June'),
     308        _('July'),
     309        _('August'),
     310        _('September'),
     311        _('October'),
     312        _('November'),
     313        _('December')
     314    ]
     315
     316
     317def get_week_day_names():
     318    """
     319    Return a list with the names of the week days, marked for translation.
     320
     321    As with get_month_names(), this is a dirty workaround for some locale
     322    problem in some operating systems
     323    """
     324    return [
     325        _('Monday'),
     326        _('Tuesday'),
     327        _('Wednesday'),
     328        _('Thursday'),
     329        _('Friday'),
     330        _('Saturday'),
     331        _('Sunday'),
     332    ]
     333
     334
    284335def part_of_day(dt):
    285336    """
  • 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.