From 19ae294c6477a2ae63fbebbba2fea29480e4a2c9 Mon Sep 17 00:00:00 2001 From: Shadi Naif Date: Thu, 22 Feb 2024 11:08:02 +0300 Subject: [PATCH] feat: add i18n_js_namespace to support atlas - FC-0012 --- .../conf/locale/ar/LC_MESSAGES/text.po | 9 ++++++ recommender/recommender.py | 31 +++++++++++++++++-- recommender/utils.py | 24 ++++++++++++++ translation_settings.py | 4 +-- 4 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 recommender/utils.py diff --git a/recommender/conf/locale/ar/LC_MESSAGES/text.po b/recommender/conf/locale/ar/LC_MESSAGES/text.po index 9f6f372..6a2c17c 100644 --- a/recommender/conf/locale/ar/LC_MESSAGES/text.po +++ b/recommender/conf/locale/ar/LC_MESSAGES/text.po @@ -56,3 +56,12 @@ msgstr "الرجاء إرسال ملف JSON الذي تم الحصول عليه #: recommender.py msgid "Tried to access flagged resources without staff permission" msgstr "تمت محاولة الدخول إلى مصادر المعلمة بعلامات بدون تصريح من الموظفين" + +#: templates/recommender.html:84 static/js/src/cats.js:158 +msgid "Show related resources" +msgstr "عرض المصادر ذات الصلة" + + +#: static/js/src/cats.js:85 +msgid "Show a list of student-recommented related resources" +msgstr "عرض قائمة المصادر ذات الصلة الموصى بها من الطلاب" diff --git a/recommender/recommender.py b/recommender/recommender.py index f4ce74c..b9eefce 100644 --- a/recommender/recommender.py +++ b/recommender/recommender.py @@ -27,6 +27,8 @@ from xblock.reference.plugins import Filesystem from xblockutils.resources import ResourceLoader +from recommender.utils import DummyTranslationService + # TODO: Should be updated once XBlocks and tracking logs have finalized APIs # and documentation. try: @@ -278,6 +280,8 @@ class RecommenderXBlock(HelperXBlock): 'url', 'title', 'description', 'descriptionText' ] + i18n_js_namespace = 'RecommenderXBlockI18N' + def _get_onetime_url(self, filename): """ Return one time url for uploaded screenshot @@ -949,9 +953,10 @@ def _construct_view_resource(self, resource): return result @staticmethod - def _get_statici18n_js_url(): # pragma: no cover + def _get_deprecated_i18n_js_url(): # pragma: no cover """ - Returns the Javascript translation file for the currently selected language, if any found by `pkg_resources` + Returns the deprecated Javascript translation file for the currently selected language, if any found by + `pkg_resources` """ lang_code = translation.get_language() if not lang_code: @@ -963,6 +968,26 @@ def _get_statici18n_js_url(): # pragma: no cover return text_js.format(lang_code=code) return None + def _get_statici18n_js_url(self): + """Return the JavaScript translation file provided by the XBlockI18NService.""" + if url_getter_func := getattr(self.i18n_service, 'get_javascript_i18n_catalog_url', None): + if javascript_url := url_getter_func(self): + return javascript_url + + if deprecated_url := self._get_deprecated_i18n_js_url(): + return self.resource_string(deprecated_url) + + return None + + @property + def i18n_service(self): + """ Obtains translation service """ + i18n_service = self.runtime.service(self, "i18n") + if i18n_service: + return i18n_service + else: + return DummyTranslationService() + def student_view(self, _context=None): # pylint: disable=unused-argument """ The primary view of the RecommenderXBlock, shown to students @@ -1011,7 +1036,7 @@ def student_view(self, _context=None): # pylint: disable=unused-argument frag.add_javascript(self.resource_string("static/js/src/jquery.tooltipster.min.js")) statici18n_js_url = self._get_statici18n_js_url() if statici18n_js_url: - frag.add_javascript(self.resource_string(statici18n_js_url)) + frag.add_javascript(statici18n_js_url) frag.add_javascript(self.resource_string("static/js/src/cats.js")) frag.add_javascript(self.resource_string("static/js/src/recommender.js")) frag.initialize_js('RecommenderXBlock', self.get_client_configuration()) diff --git a/recommender/utils.py b/recommender/utils.py new file mode 100644 index 0000000..1309429 --- /dev/null +++ b/recommender/utils.py @@ -0,0 +1,24 @@ +"""Utils for the Recommender XBlock""" + + +def _(text): + """ + Make '_' a no-op so we can scrape strings + """ + return text + + +def ngettext_fallback(text_singular, text_plural, number): + """ Dummy `ngettext` replacement to make string extraction tools scrape strings marked for translation """ + if number == 1: + return text_singular + else: + return text_plural + + +class DummyTranslationService: + """ + Dummy drop-in replacement for i18n XBlock service + """ + gettext = _ + ngettext = ngettext_fallback diff --git a/translation_settings.py b/translation_settings.py index acc9d54..1a37f71 100644 --- a/translation_settings.py +++ b/translation_settings.py @@ -11,7 +11,7 @@ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) from __future__ import absolute_import import os -BASE_DIR = os.path.dirname(os.path.dirname(__file__)) +BASE_DIR = os.path.dirname(__file__) # Quick-start development settings - unsuitable for production @@ -81,7 +81,7 @@ ('zh_CN', 'Chinese (China)'), ] -LOCALE_PATHS = [os.path.join(BASE_DIR, "locale")] +LOCALE_PATHS = [os.path.join(BASE_DIR, "recommender", "conf", "locale")] STATICI18N_DOMAIN = 'text' STATICI18N_NAMESPACE = 'RecommenderXBlockI18N'