Skip to content

Instantly share code, notes, and snippets.

@spoutnik16
Last active May 14, 2018 13:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spoutnik16/3fd19988839cc565946c52c0973fbaee to your computer and use it in GitHub Desktop.
Save spoutnik16/3fd19988839cc565946c52c0973fbaee to your computer and use it in GitHub Desktop.
django tempalte filter for french contraction for "à les", "à le", "à la"
from django import template
register = template.Library()
@register.filter(is_safe=False)
def à(value, upper=False, capitalize=False):
"""
French contraction for "à les", "à le", "à la"
if string starts with le or Le, replace it with "au"
if string starts with les or Les, replace it with "aux"
if string starts with la, replace it with "à la"
if strings starts with l', replace it with "à l'" and
hopes for the best ( this is not sexist, this is
statistically correct )
if string starts with à, do nothing
if string starts with au, do nothing
if strings start with aux , do nothing
upper = upper(value)
capitalize = capitalize(value)
"""
def capital(value):
if upper:
return value.upper()
if capitalize:
return value.capitalize()
return value
if value.startswith(('à ', 'au ', 'aux ')):
return capital(value)
if value.startswith(('le ', 'Le ', 'Au ')):
return capital('au ' + value[3:])
if value.startswith(('les ', 'Les ', 'Aux')):
return capital('aux ' + value[4:])
if value.startswith('À '):
return capital('à ' + value[2:])
return capital('au ' + value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment