Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add translations to countries in shop query

fowczarek4ca2538167e8
diff --git a/saleor/graphql/shop/types.py b/saleor/graphql/shop/types.py
index 265c117464..066a7f7db8 100644
--- a/saleor/graphql/shop/types.py
+++ b/saleor/graphql/shop/types.py
@@ -1,5 +1,6 @@
import graphene
from django.conf import settings
+from django.utils import translation
from django_countries import countries
from django_prices_vatlayer.models import VAT
from phonenumbers import COUNTRY_CODE_TO_REGION_CODE
@@ -71,6 +72,10 @@ class Shop(graphene.ObjectType):
)
countries = graphene.List(
CountryDisplay,
+ language_code=graphene.Argument(
+ LanguageCodeEnum,
+ description="A language code to return the translation for.",
+ ),
description="List of countries available in the shop.",
required=True,
)
@@ -149,14 +154,15 @@ class Shop(graphene.ObjectType):
return site_models.AuthorizationKey.objects.all()
@staticmethod
- def resolve_countries(_, _info):
+ def resolve_countries(_, _info, language_code=LanguageCodeEnum.EN.value):
taxes = {vat.country_code: vat for vat in VAT.objects.all()}
- return [
- CountryDisplay(
- code=country[0], country=country[1], vat=taxes.get(country[0])
- )
- for country in countries
- ]
+ with translation.override(language_code):
+ return [
+ CountryDisplay(
+ code=country[0], country=country[1], vat=taxes.get(country[0])
+ )
+ for country in countries
+ ]
@staticmethod
def resolve_currencies(_, _info):
diff --git a/tests/api/test_shop.py b/tests/api/test_shop.py
index f1235423e1..3ad57824cd 100644
--- a/tests/api/test_shop.py
+++ b/tests/api/test_shop.py
@@ -1,4 +1,5 @@
import graphene
+import pytest
from django_countries import countries
from saleor.core.permissions import MODELS_PERMISSIONS
@@ -30,21 +31,45 @@ def test_query_authorization_keys(
assert data["authorizationKeys"][0]["key"] == authorization_key.key
-def test_query_countries(user_api_client):
- query = """
+COUNTRIES_QUERY = """
query {
shop {
- countries {
+ countries%(attributes)s {
code
country
}
}
}
- """
- response = user_api_client.post_graphql(query)
+"""
+
+
+def test_query_countries(user_api_client):
+ response = user_api_client.post_graphql(COUNTRIES_QUERY % {"attributes": ""})
+ content = get_graphql_content(response)
+ data = content["data"]["shop"]
+ assert len(data["countries"]) == len(countries)
+
+
+ "language_code, expected_value",
+ (
+ ("", "Afghanistan"),
+ ("(languageCode: EN)", "Afghanistan"),
+ ("(languageCode: PL)", "Afganistan"),
+ ("(languageCode: DE)", "Afghanistan"),
+ ),
+)
+def test_query_countries_with_translation(
+ language_code, expected_value, user_api_client
+):
+ response = user_api_client.post_graphql(
+ COUNTRIES_QUERY % {"attributes": language_code}
+ )
content = get_graphql_content(response)
data = content["data"]["shop"]
assert len(data["countries"]) == len(countries)
+ assert data["countries"][0]["code"] == "AF"
+ assert data["countries"][0]["country"] == expected_value
def test_query_currencies(user_api_client, settings):