-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Category snippet localization #7183
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0336252
category-snippet-localization
Pomax 1523ccd
well then
Pomax bec1293
Update network-api/networkapi/wagtailpages/templatetags/localization.py
Pomax 07fd22b
update based on Karl's advice
Pomax 770a169
Merge branch 'category-snippet-localization' of github.com:mozilla/fo…
Pomax 670aab2
Merge branch 'wagtail-localize-2021' of github.com:mozilla/foundation…
Pomax 85d7eae
some WIP removals
Pomax c3d7861
show correct categories on product pages, and relocalize urls to poin…
Pomax 2160483
let's not cache-clear
Pomax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
network-api/networkapi/wagtailpages/pagemodels/mixin/snippets.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.conf import settings | ||
from wagtail.core.models import Locale | ||
|
||
DEFAULT_LOCALE = Locale.objects.get(language_code=settings.LANGUAGE_CODE) | ||
|
||
|
||
class LocalizedSnippet(): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
@property | ||
def original(self): | ||
try: | ||
return self.get_translation(DEFAULT_LOCALE) | ||
except AttributeError: | ||
return self | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
network-api/networkapi/wagtailpages/templatetags/debug_tags.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.template import Library | ||
from django.conf import settings | ||
register = Library() | ||
|
||
if settings.DEBUG: | ||
print('→ Running in DEBUG mode: enabling debug template tag "inspect_object" in tag collection "debug_tags".\n') | ||
|
||
@register.simple_tag | ||
def inspect_object(instance, prefix_label=""): | ||
output = str(dir(instance)).replace(', ', ',\n') | ||
prefix_label = str(prefix_label) | ||
return f'<pre>{prefix_label} {output}</pre>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,10 @@ | |
from django import template | ||
from django.conf import settings | ||
from django.utils.translation import get_language_info | ||
from wagtail.contrib.routable_page.templatetags.wagtailroutablepage_tags import routablepageurl | ||
|
||
register = template.Library() | ||
|
||
DEFAULT_LOCALE = 'en_US' | ||
|
||
mappings = { | ||
'en': 'en_US', | ||
'de': 'de_DE', | ||
|
@@ -20,6 +19,9 @@ | |
'pt-BR': 'pt_BR', | ||
} | ||
|
||
DEFAULT_LOCALE_CODE = settings.LANGUAGE_CODE | ||
DEFAULT_LOCALE = mappings.get(DEFAULT_LOCALE_CODE) | ||
|
||
|
||
# This filter turns Wagtail language codes into OpenGraph locale strings | ||
@register.filter | ||
|
@@ -49,3 +51,20 @@ def get_local_language_names(): | |
@register.simple_tag() | ||
def get_unlocalized_url(page, locale): | ||
return page.get_url().replace(f'/{locale}/', '/', 1) | ||
|
||
# Force-relocalize a URL | ||
@register.simple_tag() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit of a hack, and probably should not be necessary. But... it is? =( |
||
def relocalized_url(url, locale_code): | ||
if locale_code == DEFAULT_LOCALE_CODE: | ||
return url | ||
return url.replace(f'/{DEFAULT_LOCALE_CODE}/', f'/{locale_code}/') | ||
|
||
|
||
# Overcome a limitation of the routablepageurl tag | ||
@register.simple_tag(takes_context=True) | ||
def localizedroutablepageurl(context, page, url_name, locale_code, *args, **kwargs): | ||
url = relocalized_url( | ||
routablepageurl(context, page, url_name, *args, **kwargs), | ||
locale_code, | ||
) | ||
return url |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slipping this in with the rest: having this around just makes life easier