Commit 3aff96c2 by Ned Batchelder

Add strftime to the i18n runtime service.

parent be170060
...@@ -828,13 +828,17 @@ class CourseDescriptor(CourseFields, SequenceDescriptor): ...@@ -828,13 +828,17 @@ class CourseDescriptor(CourseFields, SequenceDescriptor):
Returns the desired text corresponding the course's start date. Prefers .advertised_start, Returns the desired text corresponding the course's start date. Prefers .advertised_start,
then falls back to .start then falls back to .start
""" """
i18n = self.runtime.service(self, "i18n")
_ = i18n.ugettext
strftime = i18n.strftime
def try_parse_iso_8601(text): def try_parse_iso_8601(text):
try: try:
result = Date().from_json(text) result = Date().from_json(text)
if result is None: if result is None:
result = text.title() result = text.title()
else: else:
result = result.strftime("%b %d, %Y") result = strftime(result, "SHORT_DATE")
except ValueError: except ValueError:
result = text.title() result = text.title()
...@@ -843,12 +847,12 @@ class CourseDescriptor(CourseFields, SequenceDescriptor): ...@@ -843,12 +847,12 @@ class CourseDescriptor(CourseFields, SequenceDescriptor):
if isinstance(self.advertised_start, basestring): if isinstance(self.advertised_start, basestring):
return try_parse_iso_8601(self.advertised_start) return try_parse_iso_8601(self.advertised_start)
elif self.start_date_is_still_default: elif self.start_date_is_still_default:
_ = self.runtime.service(self, "i18n").ugettext
# Translators: TBD stands for 'To Be Determined' and is used when a course # Translators: TBD stands for 'To Be Determined' and is used when a course
# does not yet have an announced start date. # does not yet have an announced start date.
return _('TBD') return _('TBD')
else: else:
return (self.advertised_start or self.start).strftime("%b %d, %Y") when = self.advertised_start or self.start
return strftime(when, "SHORT_DATE")
@property @property
def start_date_is_still_default(self): def start_date_is_still_default(self):
...@@ -865,7 +869,11 @@ class CourseDescriptor(CourseFields, SequenceDescriptor): ...@@ -865,7 +869,11 @@ class CourseDescriptor(CourseFields, SequenceDescriptor):
If the course does not have an end date set (course.end is None), an empty string will be returned. If the course does not have an end date set (course.end is None), an empty string will be returned.
""" """
return '' if self.end is None else self.end.strftime("%b %d, %Y") if self.end is None:
return ''
else:
strftime = self.runtime.service(self, "i18n").strftime
return strftime(self.end, "SHORT_DATE")
@property @property
def forum_posts_allowed(self): def forum_posts_allowed(self):
......
...@@ -25,8 +25,6 @@ from lms.lib.xblock.runtime import LmsModuleSystem, unquote_slashes ...@@ -25,8 +25,6 @@ from lms.lib.xblock.runtime import LmsModuleSystem, unquote_slashes
from edxmako.shortcuts import render_to_string from edxmako.shortcuts import render_to_string
from psychometrics.psychoanalyze import make_psychometrics_data_update_handler from psychometrics.psychoanalyze import make_psychometrics_data_update_handler
from student.models import anonymous_id_for_user, user_by_anonymous_id from student.models import anonymous_id_for_user, user_by_anonymous_id
from util.json_request import JsonResponse
from util.sandboxing import can_execute_unsafe_code
from xblock.core import XBlock from xblock.core import XBlock
from xblock.fields import Scope from xblock.fields import Scope
from xblock.runtime import KvsFieldData, KeyValueStore from xblock.runtime import KvsFieldData, KeyValueStore
...@@ -42,6 +40,10 @@ from xmodule_modifiers import replace_course_urls, replace_jump_to_id_urls, repl ...@@ -42,6 +40,10 @@ from xmodule_modifiers import replace_course_urls, replace_jump_to_id_urls, repl
from xmodule.lti_module import LTIModule from xmodule.lti_module import LTIModule
from xmodule.x_module import XModuleDescriptor from xmodule.x_module import XModuleDescriptor
from util.date_utils import strftime_localized
from util.json_request import JsonResponse
from util.sandboxing import can_execute_unsafe_code
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -428,10 +430,7 @@ def get_module_for_descriptor_internal(user, descriptor, field_data_cache, cours ...@@ -428,10 +430,7 @@ def get_module_for_descriptor_internal(user, descriptor, field_data_cache, cours
wrappers=block_wrappers, wrappers=block_wrappers,
get_real_user=user_by_anonymous_id, get_real_user=user_by_anonymous_id,
services={ services={
# django.utils.translation implements the gettext.Translations 'i18n': ModuleI18nService(),
# interface (it has ugettext, ungettext, etc), so we can use it
# directly as the runtime i18n service.
'i18n': django.utils.translation,
}, },
get_user_role=lambda: get_user_role(user, course_id), get_user_role=lambda: get_user_role(user, course_id),
) )
...@@ -652,3 +651,20 @@ def _check_files_limits(files): ...@@ -652,3 +651,20 @@ def _check_files_limits(files):
return msg return msg
return None return None
class ModuleI18nService(object):
"""
Implement the XBlock runtime "i18n" service.
Mostly a pass-through to Django's translation module.
django.utils.translation implements the gettext.Translations interface (it
has ugettext, ungettext, etc), so we can use it directly as the runtime
i18n service.
"""
def __getattr__(self, name):
return getattr(django.utils.translation, name)
def strftime(self, *args, **kwargs):
return strftime_localized(*args, **kwargs)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment