Translating content in filesystem for a Plone product - python

I'm trying to get certain strings in a .py file translated, using the i18n machinery. Translating .pt files is not a problem, but whenever I try to translate using _('Something') in Python code on the filesystem, it always gives English text (which is the default) instead of the Norwegian text that should be there. So I can see output from python code in English, while other Page Templates bits are correctly translated.
Is there a how-to or something similar for this?

Is the domain name used for _('Something') the same as what you use in the Norwegian .po file that has the translation? They should be the same, so do not use 'plone' in one case and 'my.domain' in the other.
Also, the call to the underscore function does not in itself translate the string; it only creates a string that can be translated. If this string ends up on its own directly in a template, you should add i18n:translate="" to that tag, probably with a matching i18n:domain.
Otherwise you should manually call the translate method, as in http://readthedocs.org/docs/collective-docs/en/latest/i18n/localization.html#manually-translated-message-ids. Read the Plone 4 migration guide for some differences between Plone 3 and 4 that might bite you here.

if you are seeking for how-tos you should probably read these docs:
http://plone.org/documentation/kb/i18n-for-developers
http://readthedocs.org/docs/collective-docs/en/latest/i18n/localization.html
Bye,
Giacomo

be aware that _() does not translate the text at call, but returns a Message object which will be translated when rendered in a template.
That means:
do not concat Message objects. "text %s" % _('translation') will not work, as well as "text" + _('translation')
if you do not send the text to the browser through a template, it may not be translated. for example if you generate a email you need to translate the Message object manually

Related

PyQt5 incorrect label formatting with links

I have two issues with how PyQt is formatting my QLabels
Issue 1:
When hyperlinks are added it displays as if there were no newlines in the string.
For the input text:
https://www.google.co.uk/
https://www.google.co.uk/
https://www.google.co.uk/
It's shown like this without newlines
Issue 2: Sometimes PyQt just doesn't even detect the 'a' tag this happens when the start of string is not a hyperlink but it is then followed by newlines with hyperlinks e.g. this input:
test
https://www.google.co.uk/
https://www.google.co.uk/
https://www.google.co.uk/
As you can see the newlines are properly shown but PyQt has no longer detected the hyperlinks
From the text property documentation of QLabel:
The text will be interpreted either as plain text or as rich text, depending on the text format setting; see setTextFormat(). The default setting is Qt::AutoText; i.e. QLabel will try to auto-detect the format of the text set.
The AutoText flag can only make a guess using simple tag syntax checks (basic tags without arguments, such as <b>, or document type declaration headers, like <html>).
This is obviously done for performance reasons.
If you are sure that you're always setting rich text content, use the appropriate Qt.TextFormat enum:
label.setTextFormat(QtCore.Qt.RichText)
Using the HTML-like syntax of rich text will obviously use the same basic concept HTML had since its birth, almost 30 years ago: line breaks between any word in the document (text or tag) are ignored, as much as multiple spaces are always considered as one.
So, if you want to add line breaks, you have to use the appropriate <br> (or <br/> for xhtml) tag.
Also remember that Qt rich text engine has a limited support, as described in the documentation about the Supported HTML Subset.

Why the html file converted from rst file containing question mark can't displayed on browser when to click the catalog?

Please download the file simple.7z and install in your sphinx to reproduce issues what i described here,in order to reproduce it, you can run:
make clean
make html
download and install in your sphinx to reproduce issues
There are two articles in sample/source,the content are same,only difference is the title.
cd sample
ls source |grep "for-loop"
What does "_" in Python mean in a for-loop.rst
What does "_" in Python mean in a for-loop?.rst
One contains ? in it,the other doesn't contains ?.
Strange thing happend after running make html.
make html
ls build/html|grep "for-loop"
What does "_" in Python mean in a for-loop.html
What does "_" in Python mean in a for-loop?.html
Issue 1:
Why firefox show all of then as What does “_” in Python mean in a for-loop?
Note:
"_" was combiled into “_” ;
The title which contain no ? such as What does "_" in Python mean in a for-loop.html was compiled into What does “_” in Python mean in a for-loop?,which add a ? in it?
Issue 2:
To click the first title What does “_” in Python mean in a for-loop?,browser jump into a status no url was not found on this server
To click the second title What does “_” in Python mean in a for-loop?,it works fine.
Please give explanation on my issue.
Issue 1 is actually not an issue, it is the normal behaviour of Sphinx: The displayed title is not the filename but the top level title of the ResT document, which is ended with a question mark in both case.
See the Table of contents > .. toctree:: > Entries section of this page which states:
Document titles in the toctree will be automatically read from the title of the referenced document.
If you need to be convinced, just change the title of any of the document, rebuild the HTML and see.
Issue 2 is caused by the presence of a non urlencoded question mark in the href of the link. In an url, the question mark means the start of a query string.
That is trying to access What does "_" in Python mean in a for-loop?.html, means requesting the What does "_" in Python mean in a for-loop HTML document, giving it a .html parameter. And obviously, the requested document is not found since it does not exist.
In the address bar of your browser, you can replace the question mark by its urlencoded form %3F, and observe it works then.
I did not find any way to make sphinx automatically urlencode document names in link's href (it actually is considered a bug) or any doc clearly expressing naming restriction for ResT documents.
But I know from experience that naming a file with punctuation is often source of problems. I would advice you to rename your document with less exotic characters (slugify them). Naming your ResT document underscore_in_for_loop.rst instead of What does "_" in Python mean in a for-loop?.rst will save you a lot of time and headaches.
And remember that it should not be a problem since, as we seen in the first issue, the document name is used only for the URL.
? is a wildcard for part word matching in URL and hence will be treated as a special character.
wild cards operate with two type of jokers:
? - any character (one and only one)
* - any characters (zero or more)
For this, You have to Encode the URL
import urllib.parse
>>> urllib.parse.quote(<<url>>)
In Python 3.x, you need to import urllib.parse.quote:
import urllib.parse
urllib.parse.quote(<<url>>)

Trying to substitute one variable for another before writing to an HTML File

I have a python program that reads a csv file, makes some changes, then writes to an HTML file. The issue is a block of code where I'm trying to search for a string assigned to one variable, then replace it with another string assigned to another variable. I am able to read a line in the csv file that looks like this:
Link:,www.google.com
And I am successful in writing an html file with the following:
<tr><td>Link:</td><td>www.google.com</td></tr>
Essentially I want to go further with an added step to find www.google.com between the anchor tags and replace it with "GOOGLE".
I've researched 'find and replace' functions built into python, and I came up with the substitution function inside the regular expressions module (re.sub()). This might not be the best way to do it and I'm trying to figure out if there's a better function/module out there I should look into.
python
for line in file:
newHTML.write(re.sub(var1,var2,line,flags=re.MULTILINE), end='')
newHTML.write(re.sub(var3,var4,line,flags=re.MULTILINE), end='')
The error I am receiving is:
newHTML.write(re.sub(var1,var2,line,flags=re.MULTILINE), end='')
TypeError: write() takes no keyword arguments
If I comment out this code, the rest of the program runs fine albeit without finding and replacing these variables.
Perhaps re.sub() doesn't go well with write()?
The error says what the problem is: as #furas commented, write() is not the same as print(), and doesn't accept the end='' keyword argument. file.write() by default doesn't include newlines if you don't explicitly put any \n's, so it should work if you change the line to:
newHTML.write(re.sub(var1,var2,line,flags=re.MULTILINE))
Also, regex and HTML aren't the best of friends... Your case is simple enough that using regex is fine, but you mentioned looking for a better module to generate HTML. This SO question had some good suggestions in the answers. Notable mentions for creating HTML templates on there were xml.etree, jinja2 (Flask's default engine), and yattag.

Is there any way to use "f-string" for a multiline string, which is stored in a variable?

I want to send email with html template.The template is in a html file and there are some variable in this. If I read the file and store its text in a variable :
with open('test.html', 'r') as f:
html = f.read()
Is it possible to use f-string in 'html' variable?
The f-strings (also called formatted string literals), are a part of the language syntax and are a way to describe strings in code. What you want to achieve here is something different.
What you describe is commonly known as templates. The Python standard library provides Template strings, but they work a little different then f-strings.
Furthermore, a number of third-party template libraries are mentioned in comments, and you could search on internet for other template libraries (modules/packages) that best suits your application.

How to create a word docx using python docx in other than english?

I am building a program creating printed outputs from python code. Further, the final print containing the other language (Sinhala). I want to use python docx to save this output into a word document. How to write into word in another language?
My aim is to produce a report making program from another language (Sinhala). I take all user inputs from widgets and managed to print the resulted lines in another language in python.
Now, I want to write these lines into word file using the Sinhala language.
a= "කණ්ඩියේ උස මීටර් 5.0 ක් පළල මීටර් 2.0 හා දිග මීටර් 2.0 ක් පමණ වන කොටසක්
අස්ථාවර වී"
document = Document()
document.add_heading("python word doc")
document.add_paragraph(a)
document.save('****\\report.docx')
when I use English, the code does the job. But, for the Sinhala language, I'm not sure how to do that?
I get the following error message for sinala language.
ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
The error code you're seeing is not directly related to the language. The only thing Word knows about language is which spelling dictionary to use. Otherwise its text is just an arbitrary sequence of unicode characters.
What I suspect is that the Unicode encoding of the Sinhala strings you're trying to write is not UTF-8. The other possibility is that the string contains some control characters (as mentioned in the error message), particularly the vertical-tab (VT, 0xB or decimal 11) which can arise in copy and paste from PowerPoint.
This latter one is easier to check for, so perhaps start there.
import re
def sanitize_str(s):
control_chars = "\x00-\x1f\x7f-\x9f"
control_char_re = re.compile("[%s]" % control_chars)
return control_char_re.sub("", s)
document.add_paragraph(sanitize_str(a))

Categories