Find untranslated strings in HTML templates - python

Is there way to find untranslated strings in the HTML templates of my Django application i.e. blocks of text that are not wrapped in trans and blocktrans tags.
Since we have many templates, it would be a very time-consuming process to go through them manually and check but if there isn't an option, I guess it has to be done the long and tedious way.
Thanks

Found this recently, but have not tried it yet.
Doc: http://www.technomancy.org/python/django-template-i18n-lint/
Code: https://github.com/rory/django-template-i18n-lint
Looks like it hasn't been updated in a year but it might provide a good starting spot.

You can use the builtin template parser to parse your templates, and recurse into all tags that are not instances of BlockTransTag

Related

How to dinamically inject HTML code in Django

In a project of mine I need to create an online encyclopedia. In order to do so, I need to create a page for each entry file, which are all written in Markdown, so I have to covert it to HTML before sending them to the website. I didn't want to use external libraries for this so I wrote my own python code that receives a Markdown file and returns a list with all the lines already formatted in HTML. The problem now is that I don't know how to inject this code to the template I have in Django, when I pass the list to it they are just printed like normal text. I know I could make my function write to an .html file but I don't think it's a great solution thinking about scalability.
Is there a way to dynamically inject HTML in Django? Is there a "better" approach to my problem?
You could use the safe filter in your template! So it would look like that.
Assuming you have your html in a string variable called my_html then in your template just write
{{ my_html | safe }}
And don’t forget to import it!

Pelican - How can I render html pages instead of markup?

I'm redoing my Flask site in Pelican as I intend to start blogging. I have html files already for some specific pages (i.e. contact, books, etc) that use bootstrap cards and other features that are not possible in markdown (as far as I know).
How can I render these in the build cycle. There has to be a config variable for that? I found TEMPLATE_PAGES but it didn't seem to work. This has to be possible, right?
I was trying to do the same thing as you and finally found the relevant documentation (you can find it here where it defines the variable DIRECT_TEMPLATES):
DIRECT_TEMPLATES = ['index', 'categories', 'authors', 'archives']
List of templates that are used directly to render content. Typically direct templates are used to generate index pages for collections of content (e.g., tags and category index pages). If the tag and category collections are not needed, set DIRECT_TEMPLATES = ['index', 'archives']
For example, let's say you have a file my_file.html in your templates directory. If you want to render it like other templates files in your theme instead of using only Markdown, you could append an item to the list in the variable DIRECT_TEMPLATES that you will have to define in your configuration file (it is pelicanconf.py by default). The result should look similar to this:
DIRECT_TEMPLATES = [
'index', 'categories', 'authors', 'archives', # (default)
'my_file' # other HTML template to render
]
By proceeding this way, my_file.html would be rendered as any other HTML file with Jinja2 syntax and all the good stuff you want to use.
I know this comes late but hopefully someone (maybe still you!) will benefit from this answer. It is definitely a very well hidden feature for sure...
from their official documentation
http://docs.getpelican.com/en/stable/content.html#writing-content
Pelican can also process HTML files ending in .html and .htm. Pelican
interprets the HTML in a very straightforward manner, reading metadata
from meta tags, the title from the title tag, and the body out from
the body tag:

Django - Avoid writing duplicate code

I want to create 2 web pages which use the same code but I don't want to copy/paste the code in 2 places. I'm sure I don't need to have the same code in 2 different places to use it on multiple pages. Can someone suggest a way to avoid creating duplicate code?
If required, I can provide some sample code which I've already written.
For generating HTML on the frontend, you can use templates to create static content which can be used across multiple pages on your site.
For more information on how to use django templates, see: https://docs.djangoproject.com/en/1.9/topics/templates/
As for the backend, you'll need to write your code as re-usable as possible and then import/reuse as much as you can. Pylint has a check for similar/duplicate code:
For more information on Pylint similarities-checker, see: https://docs.pylint.org/features.html#similarities-checker
Use this built-in django template tag for that:
{% include "subtemplate.html" %}

Can I nest multiple template objects inside another chameleon template?

Say I had a chameleon template file for a user object with something like this:
<h2><tal:content="user.name"></h2>
<h4><tal:content="user.occupation"></h4>
<p><tal:content="user.bio"></p>
Can I loop over a list of users in another template file to fill multiple content slots like below?
<tal:block repeat="user users">
<div metal:define-slot='user'></div> <!-- ??? -->
</tal:block>
I imagine this may be useful if a page has a list of complicated objects with a lot of data to display, but I can't find anything about it and I don't know what search terms to give google.
Right now I just have something like this:
<tal:block repeat="user users">
<h2><tal:content="user.name"</h2>
<h4><tal:content="user.occupation"></h4>
<p><tal:content="user.bio"></p>
</tal:block>
which is good enough for me, but I was just wondering if what I am asking is possible.
What you're looking for are metal macros. They are confusing at first, but super powerful once you get your head around them.
https://chameleon.readthedocs.org/en/latest/reference.html?highlight=metal#metal
HTH

Issues with multiple languages in mezzanine

I am using multiple language in mezzanine and it is working fine with its own core templates.But I try to use it in my own templates then it is not working.
I have use all things in proper steps. I have locale folder in which django.po consists of all text which I can translate and there encoded django.mo file.
Buy when I choose other then English Lang. in my browser it does not translate my templates text but change the text of core templates.
I don't know why it does not translate my templates data Because if there are any kind of errors then it must not translate core templates but it does.
Please help me to sort out my problem , any suggestion would be appreciated.
Thanks.
Does your .po file contain the word "fuzzy"? If so, you might want to read the following article: http://blog.e-shell.org/124, as mentioned in this previous SO question: Django missing translation of some strings. Any idea why?
If that is not the cause of your issue, are you sure you are using the internationalisation template tags appropriately?
First of all, ensure you are loading the relevant template tags at the top of each of your templates:
{% load i18n %}
Then, for any text you want to make translatable:
{% trans "Insert your text here" %}
See also: https://docs.djangoproject.com/en/dev/topics/i18n/translation/#internationalization-in-template-code and https://docs.djangoproject.com/en/dev/topics/i18n/translation/#implementation-notes.

Categories

Resources