Last active
June 30, 2019 00:11
-
-
Save sveetch/8cd738a6f5ddc638547fbfc8f6fc5b16 to your computer and use it in GitHub Desktop.
Smart templatetag with translation and context driven from settings (save from an attempt on Richie)
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
""" | |
i18n utilities for our search app | |
""" | |
from django.conf import settings | |
def get_best_field_language(multilingual_field, best_language): | |
""" | |
Pick the best available language from a multilingual field. | |
A multilingual field is eg: | |
'title': { | |
'es': 'mi título', | |
'fr': 'mon titre', | |
} | |
1. Use the most appropriate language as determined by the consumer | |
2. Default to language #0, then #1, then #2, etc. in settings.LANGUAGES | |
""" | |
for language in [best_language] + [lang for lang, _ in settings.LANGUAGES]: | |
try: | |
return multilingual_field[language] | |
except KeyError: | |
pass |
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
# Available social networks badges with parameters | |
SOCIAL_NETWORKS_BADGES = [ | |
{ | |
"name": "facebook-page", | |
"template": "social-networks/facebook.html", | |
"context": { | |
"title": { | |
"fr": "Page Facebook", | |
"en": "Facebook page", | |
}, | |
"url": { | |
"en": "https://www.facebook.com/france.universite.numerique", | |
}, | |
}, | |
}, | |
{ | |
"name": "facebook-share", | |
"template": "social-networks/facebook.html", | |
"context": { | |
"title": { | |
"fr": "Partager sur Facebook", | |
"en": "Share on Facebook", | |
}, | |
"url": { | |
"en": "http://www.facebook.com/share.php?u={{page_url}}", | |
}, | |
}, | |
}, | |
{ | |
"name": "mailto-course", | |
"template": "social-networks/mailto.html", | |
"context": { | |
"title": { | |
"fr": "Partager par courriel", | |
"en": "Share with email", | |
}, | |
"subject": { | |
"fr": "Suivez un cours en ligne avec FUN", | |
}, | |
"body": { | |
"fr": "Je viens de m'inscrire pour {{page_title}} via FUN {{page_url}}", | |
}, | |
"url": { | |
"en": "mailto:?subject={{subject}}&body={{body}}", | |
}, | |
}, | |
}, | |
{ | |
"name": "mailto-blogpost", | |
"template": "social-networks/mailto.html", | |
"context": { | |
"title": { | |
"fr": "Partager par courriel", | |
"en": "Share with email", | |
}, | |
"subject": { | |
"fr": "Actualité FUN: {{page_title}}", | |
}, | |
"body": { | |
"fr": "{{page_url}}", | |
}, | |
"url": { | |
"en": "mailto:?subject={{subject}}&body={{body}}", | |
}, | |
}, | |
}, | |
{ | |
"name": "twitter-blogpost", | |
"template": "social-networks/twitter.html", | |
"context": { | |
"title": { | |
"fr": "Partager sur Twitter", | |
"en": "Share on Twitter", | |
}, | |
"body": { | |
"fr": "{{page_url}}", | |
}, | |
"url": { | |
"en": "https://twitter.com/intent/tweet?text={{body}}", | |
}, | |
}, | |
}, | |
{ | |
"name": "twitter-course", | |
"template": "social-networks/twitter.html", | |
"context": { | |
"title": { | |
"fr": "Partager sur Twitter", | |
"en": "Share on Twitter", | |
}, | |
"body": { | |
"fr": "Je viens de m'inscrire pour {{page_title}} via FUN {{page_url}}", | |
}, | |
"url": { | |
"en": "https://twitter.com/intent/tweet?text={{body}}", | |
}, | |
}, | |
}, | |
] |
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
{% spaceless %}<a href="{{ url }}" target="_blank" class="badge badge--{{ name }}"> | |
<span class="icon-facebook"></span> | |
</a>{% endspaceless %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment