Skip to content

Instantly share code, notes, and snippets.

@izidormatusov
Last active August 29, 2015 13:56
Show Gist options
  • Save izidormatusov/9188108 to your computer and use it in GitHub Desktop.
Save izidormatusov/9188108 to your computer and use it in GitHub Desktop.
Add {% load i18n %} to templates which are missing it
# Add {% load i18n %} to templates which are missing it
#
# How this works?
# Step 1: Find all files in templates folder
# Step 2: Print file name of that file if file has at least one trans or blocktrans tag
# Step 3: Put load tag on the first or second line, depending if the first line is extends tag
#
# Elaborate on the third step:
#
# 1 { # Operate only on the first line
# # If the first line is extends tag
# /^{% extends/ {
# p # Dump it immediately to output
# x # And replace it by empty line (actually hides into another buffer)
# }
# # Insert the line with the tag
# # (text for command i ends by new line character;
# # in a one liner we can split it using -e parameter)
# i{% load i18n %}
# # If the first line was extends tag, we have empty line in buffer, delete it
# /^$/d
# }
find templates/ -type f -exec \
awk '/{% load i18n %}/ { ++l } /{% trans|{% blocktrans/ {++t} END { if (t && !l) print FILENAME}' \
{} \; | xargs \
sed -i -e '1{/^{% extends/ {p;x}; i{% load i18n %}' -e '/^$/d}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment