Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestion: template engine #311

Closed
ghost opened this issue May 8, 2020 · 8 comments
Closed

suggestion: template engine #311

ghost opened this issue May 8, 2020 · 8 comments

Comments

@ghost
Copy link

ghost commented May 8, 2020

Hello,

I have a suggestion for the template handling. The problem with the current solution is that the translations are not very well integrated into the django translation system. The translations have to be "hard-coded" in python files and can not be easily sent to an external translator as they would expect .po files.

I propose here a solution which is fully integrated with the django translation system. Besides is it not incompatible with the current translation schema of post-office.

This also allows to ship templates out-of-the-box with Django applications and eliminates the need to set them up manually. They will be also automatically updated.

templates.py

from django.db import connection
from post_office.models import EmailTemplate
from django.utils.translation import gettext_lazy as _

from django.template.loader import render_to_string, get_template
from django.utils.html import strip_tags

def db_table_exists(table_name):
    return table_name in connection.introspection.table_names()

if db_table_exists(  'post_office_emailtemplate' ):

    tFile =  get_template('myapp/email/generic.html')
    with open( str(tFile.template.origin), 'r') as file:
        tContentHTML = file.read()
        tContent =  strip_tags(tContentHTML)

    EmailTemplate.objects.update_or_create(
        name='generic',
        defaults={
        'subject'      :  _('Message from domain.com'),
        'description'  :  'Generic template',
        'html_content' :  tContentHTML,
        'content'      :  tContent}
        )
@jrief
Copy link
Collaborator

jrief commented May 8, 2020

The problem with the current solution is that the translations are not very well integrated into the django translation system.

could you give me an example?

I use translations with post_office all the time and haven't had any problems with it, nor did I need to "hard-code" them in python files.

@ghost
Copy link
Author

ghost commented May 8, 2020

Maybe I do not understand the documentation correctly but the following code would include the translation in the python code:

template = EmailTemplate.objects.create(
    name='hello',
    subject='Hello world!',
)

# Add an Indonesian version of this template:
indonesian_template = template.translated_templates.create(
    language='id',
    subject='Halo Dunia!'
)

This would not be collected in the .po files for a translator and would force the translator to work on python files.

The alternative that I provided renders a template that uses the conventional tags of {% load i18n %}. Hence, the strings will be collected by Django into the .po files for the translator. I guess it is a good alternative and it is no incompatible with the current solution.

I see also that I am using the plugin in a different way. I do not want to set up manually the templates but ship them out-of-the-box.

@jrief
Copy link
Collaborator

jrief commented May 8, 2020

This is just an example. Normally you would do that in Django's admin backend,
for instance here: http://localhost:8000/en/admin/post_office/emailtemplate/

@ghost
Copy link
Author

ghost commented May 9, 2020

exactly that is my point. With the current system you will have to do this manually, but what if I need to deploy the app multiple times? For that use case, having a second option that can deliver translated templates with the base code would be a nice improvement. And it would be integrated in the translation workflow.
Maybe you want to have a look and think if a second kind of class based templates would make sense.

@jrief
Copy link
Collaborator

jrief commented May 9, 2020

Now I understand your use-case.

What about doing it like:

from django.conf import settings
from django.utils.translation import gettext_lazy

from post_office.models import EmailTemplate

template = EmailTemplate.objects.create(
    name=_("hello"),
    subject='Hello world!',
)

for language, _ in settings.LANGUAGES:
    template.translated_templates.create(
        language=language,
        subject=gettext_lazy('Hello world!'),
        body=gettext_lazy("""
Hello {{user.first_name}},
you have been selected for a free vacation to the Disney Land!
        """)

With django-admin makemessages you get .po-files for all configured languages. You can then pass them to your translator(s).

@ghost
Copy link
Author

ghost commented May 10, 2020

Yes exactly that is the direction that I am suggesting.
I experienced problems with the EmailTemplate.objects.create bit, because it will create a template object every time that the application is started, leading to duplicate templates. That is why I am using EmailTemplate.objects.update_or_create instead. This worked until I had to deploy the application with an empty DB. then I had to check if the table was already available:

from django.db import connection

def db_table_exists(table_name):
    return table_name in connection.introspection.table_names()

This method could be integrated into the plugin as a class based template that takes either a string, as you suggested or a template file like in my first example. That would be a nice feature for the plugin.

@jrief
Copy link
Collaborator

jrief commented May 11, 2020

This worked until I had to deploy the application with an empty DB. then I had to check if the table was already available

The correct place to add such fixtures is in the ready()-method of your AppConfig.

@jrief
Copy link
Collaborator

jrief commented May 14, 2020

closing, since this issue seems to be solved.

@jrief jrief closed this as completed May 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant