From 1911529aae08471b7798bb2f90a20e72886706b3 Mon Sep 17 00:00:00 2001 From: Shadi Naif Date: Wed, 28 Feb 2024 12:15:06 +0300 Subject: [PATCH] feat!: remove Transifex calls for FC-0012 - OEP-58 --- .tx/config | 9 ------ CHANGELOG.rst | 8 +++++ README.rst | 11 ------- lti_consumer/__init__.py | 2 +- lti_consumer/conf/locale/config.yaml | 11 ------- lti_consumer/lti_xblock.py | 44 +++++++++++++++------------- lti_consumer/utils.py | 16 ++++++++++ 7 files changed, 49 insertions(+), 52 deletions(-) delete mode 100644 .tx/config diff --git a/.tx/config b/.tx/config deleted file mode 100644 index 15bc669f..00000000 --- a/.tx/config +++ /dev/null @@ -1,9 +0,0 @@ -[main] -host = https://www.transifex.com - -[o:open-edx:p:xblocks:r:lti-consumer] -file_filter = lti_consumer/translations//LC_MESSAGES/text.po -source_file = lti_consumer/translations/en/LC_MESSAGES/text.po -source_lang = en -type = PO - diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 37d1839b..14423aba 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,14 @@ Please See the `releases tab `_ and run this command while -inside project root directory: - -.. code:: bash - - $ tx pull -f --mode=reviewed - Further Development Info ------------------------ diff --git a/lti_consumer/__init__.py b/lti_consumer/__init__.py index cd9ee742..0ba711e2 100644 --- a/lti_consumer/__init__.py +++ b/lti_consumer/__init__.py @@ -4,4 +4,4 @@ from .apps import LTIConsumerApp from .lti_xblock import LtiConsumerXBlock -__version__ = '9.8.3' +__version__ = '9.9.0' diff --git a/lti_consumer/conf/locale/config.yaml b/lti_consumer/conf/locale/config.yaml index 19ef2783..cc218b26 100644 --- a/lti_consumer/conf/locale/config.yaml +++ b/lti_consumer/conf/locale/config.yaml @@ -2,17 +2,6 @@ locales: - en # English - Source Language - - ar # Arabic - - es_419 # Spanish (Latin America) - - fr # French - - he # Hebrew - - hi # Hindi - - ja_JP # Japanese (Japan) - - ko_KR # Korean (Korea) - - pt_BR # Portuguese (Brazil) - - pt_PT # Portuguese (Portugal) - - ru # Russian - - zh_CN # Chinese (China) # Directories we don't search for strings. ignore_dirs: diff --git a/lti_consumer/lti_xblock.py b/lti_consumer/lti_xblock.py index bf18b4b2..400b15df 100644 --- a/lti_consumer/lti_xblock.py +++ b/lti_consumer/lti_xblock.py @@ -55,11 +55,10 @@ import urllib.parse from collections import namedtuple from importlib import import_module -import pkg_resources import bleach from django.conf import settings -from django.utils import timezone, translation +from django.utils import timezone from web_fragments.fragment import Fragment from webob import Response @@ -87,6 +86,7 @@ external_user_id_1p1_launches_enabled, database_config_enabled, EXTERNAL_ID_REGEX, + DummyTranslationService, ) log = logging.getLogger(__name__) @@ -257,6 +257,7 @@ class LtiConsumerXBlock(StudioEditableXBlockMixin, XBlock): """ block_settings_key = 'lti_consumer' + i18n_js_namespace = 'XBlockLtiConsumerI18N' display_name = String( display_name=_("Display Name"), @@ -662,20 +663,14 @@ def workbench_scenarios(): ] return scenarios - @staticmethod - def _get_statici18n_js_url(loader): # pragma: no cover - """ - Returns the Javascript translation file for the currently selected language, if any found by - `pkg_resources` - """ - lang_code = translation.get_language() - if not lang_code: - return None - text_js = 'public/js/translations/{lang_code}/text.js' - country_code = lang_code.split('-')[0] - for code in (translation.to_locale(lang_code), lang_code, country_code): - if pkg_resources.resource_exists(loader.module_name, text_js.format(lang_code=code)): - return text_js.format(lang_code=code) + 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 + return None def validate_field_data(self, validation, data): @@ -776,6 +771,15 @@ def get_pii_sharing_enabled(self): # because the service is not defined in those contexts. return True + @property + def i18n_service(self): + """ Obtains translation service """ + i18n_service = self.runtime.service(self, "i18n") + if i18n_service: + return i18n_service + else: + return DummyTranslationService() + @property def editable_fields(self): """ @@ -1197,9 +1201,9 @@ def author_view(self, context): ) fragment.add_css(loader.load_unicode('static/css/student.css')) fragment.add_javascript(loader.load_unicode('static/js/xblock_lti_consumer.js')) - statici18n_js_url = self._get_statici18n_js_url(loader) + statici18n_js_url = self._get_statici18n_js_url() if statici18n_js_url: - fragment.add_javascript_url(self.runtime.local_resource_url(self, statici18n_js_url)) + fragment.add_javascript_url(statici18n_js_url) fragment.initialize_js('LtiConsumerXBlock') return fragment @@ -1224,9 +1228,9 @@ def student_view(self, context): fragment.add_content(loader.render_mako_template('/templates/html/student.html', context)) fragment.add_css(loader.load_unicode('static/css/student.css')) fragment.add_javascript(loader.load_unicode('static/js/xblock_lti_consumer.js')) - statici18n_js_url = self._get_statici18n_js_url(loader) + statici18n_js_url = self._get_statici18n_js_url() if statici18n_js_url: - fragment.add_javascript_url(self.runtime.local_resource_url(self, statici18n_js_url)) + fragment.add_javascript_url(statici18n_js_url) fragment.initialize_js('LtiConsumerXBlock') return fragment diff --git a/lti_consumer/utils.py b/lti_consumer/utils.py index a6acbd4d..fb0a36d4 100644 --- a/lti_consumer/utils.py +++ b/lti_consumer/utils.py @@ -31,6 +31,14 @@ def _(text): 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 + + def get_lti_api_base(): """ Returns base url to be used as issuer on OAuth2 flows @@ -361,3 +369,11 @@ def model_to_dict(model_object, exclude=None): return object_fields except (AttributeError, TypeError): return {} + + +class DummyTranslationService: + """ + Dummy drop-in replacement for i18n XBlock service + """ + gettext = _ + ngettext = ngettext_fallback