Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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