How to create Django translations for django-rosetta from a model field

How to create Django translations from a model field for django-rosetta?



I am using Django-Rosetta to handle all of my project translations.  Unfortunately I have only one model and one field of data that needs translation, the name of the POI.  To get around this I have created a little script to query my Django Database directly to deliver the list of names to be translated.  The output is a new html file with "trans" tags.  This html file is then placed in my templates folder so that "makemessages" can find the file and mark them for translation.

Anytime I want to update this I can run this script and bingo, new template file and new translations.

Note to self:  When adding msgid and msgstr  by hand the command "makemessages" will remove them in the .po file every time the command is called.


import psycopg2
conn = psycopg2.connect(host='localhost', user='someuser', port='5432', password='verysecretpwd', database='myDbName')
cur = conn.cursor()
def generate_poi_translation_template(filename):
# query the database table for the list of names you want to translate
sel_building_floor_id = """SELECT f.cat_name FROM (
SELECT DISTINCT ON (cat_name) cat_name, parent_id
FROM django.poi_manager_poicategory) AS f ORDER BY parent_id DESC ;"""
cur.execute(sel_building_floor_id)
res_floor_id = cur.fetchall()
with open(filename, 'w') as f:
f.write('{% load i18n %}' + '\n\n')
for r in res_floor_id:
f.write('{% trans "' + r[0] + '" %}\n')
generate_poi_translation_template('poi-trans.html')

Comments

Post a Comment