diff --git a/ecommerce/core/context_processors.py b/ecommerce/core/context_processors.py index 6c62ba12ba4..818a4298b09 100644 --- a/ecommerce/core/context_processors.py +++ b/ecommerce/core/context_processors.py @@ -1,5 +1,8 @@ +from babel.numbers import get_currency_symbol +from django.conf import settings + from ecommerce.core.url_utils import get_favicon_url, get_lms_dashboard_url, get_lms_url, get_logo_url @@ -16,3 +19,20 @@ def core(request): 'favicon_url': get_favicon_url(), 'optimizely_snippet_src': site_configuration.optimizely_snippet_src, } + + +def localization(_request): + defaults = getattr(settings, "COURSE_MODE_DEFAULTS", {}) + default_currency = defaults.get("currency") + registration_currency = getattr(settings, "PAID_COURSE_REGISTRATION_CURRENCY", None) + currency_code = registration_currency or default_currency or "USD" + + if not isinstance(currency_code, str): + raise ValueError(f"Currency code must be a string; currently: {currency_code}") + + return { + # Note: babel returns code if not found + # get_currency_symbol("XYZ") => "XYZ" + "currency_symbol_": get_currency_symbol(currency_code), + "currency_code_": currency_code, + } diff --git a/ecommerce/courses/tests/test_views.py b/ecommerce/courses/tests/test_views.py index 313ec1ef556..ea875fb612c 100644 --- a/ecommerce/courses/tests/test_views.py +++ b/ecommerce/courses/tests/test_views.py @@ -133,6 +133,25 @@ def test_credit_providers_in_context(self): json.loads(provider_json), ) + @responses.activate + def test_currency_localization(self): + """Verify the currency symbol is localized based on settings""" + self._create_and_login_staff_user() + self.mock_access_token_response() + __ = self.mock_credit_api_providers() + + response = self.client.get(self.path) + self.assertEqual(response.status_code, 200) + # default is USD + self.assertEqual(response.context["currency_symbol_"], "$") + self.assertEqual(response.context["currency_code_"], "USD") + + with self.settings(PAID_COURSE_REGISTRATION_CURRENCY="GBP"): + response = self.client.get(self.path) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context["currency_symbol_"], "£") + self.assertEqual(response.context["currency_code_"], "GBP") + @responses.activate def test_credit_providers_in_context_cached(self): """ Verify the cached context data includes a list of credit providers. """ diff --git a/ecommerce/settings/base.py b/ecommerce/settings/base.py index 6432d0969a8..76e8e4689ae 100644 --- a/ecommerce/settings/base.py +++ b/ecommerce/settings/base.py @@ -208,6 +208,7 @@ 'oscar.apps.communication.notifications.context_processors.notifications', 'oscar.core.context_processors.metadata', 'ecommerce.core.context_processors.core', + "ecommerce.core.context_processors.localization", 'ecommerce.extensions.analytics.context_processors.analytics', ), 'debug': True, # Django will only display debug pages if the global DEBUG setting is set to True. diff --git a/ecommerce/static/templates/_course_credit_seats.html b/ecommerce/static/templates/_course_credit_seats.html index b8d147b2319..804ea19acaa 100644 --- a/ecommerce/static/templates/_course_credit_seats.html +++ b/ecommerce/static/templates/_course_credit_seats.html @@ -21,7 +21,7 @@ <% _.each(creditSeats, function (seat) { %>
- $<%= course.attributes.stockrecords.price_excl_tax %> + {{ currency_symbol_ }}<%= course.attributes.stockrecords.price_excl_tax %>
- <%- gettext('Now') %> $<%= course.attributes.new_price %> + <%- gettext('Now') %> {{ currency_symbol_ }}<%= course.attributes.new_price %>