user1261774
user1261774

Reputation: 3695

Django French Translation - how to handle single quotes in translation strings?

I am using Python 3.5.2 and Django 1.10.

I have received the French translation .po file and can run the compilemessages command without receiving any errors.

However, when I run the site, many pages refuse to load.

I suspect that this is because the French translation .po file contains many single quotes (') in the translation strings.

For example,

#: .\core\constants\address_country_style_types.py:274
msgid "Ascension Island"
msgstr "Île de l'Ascension"

I remember reading somewhere (but cannot find that reference anywhere) that the single quotes must have either a forward or back slash before them. So I tried that, but when I ran the compilemessage command, I got an error message of:

C:\Users\me\desktop\myapp\myapp\locale\fr\LC_MESSAGES\django.po:423:18: invalid control sequence

So how do I escape the French single quote in strings issue?

here is the header of my French language .po file:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-05-04 12:55+1000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

Upvotes: 5

Views: 1724

Answers (5)

Skratt
Skratt

Reputation: 408

Obviously something that non-french speakers will have a problem to get their mind around... but for us, that's a real issue.

I always juggle with strings defined with double or single quotes depending of what is inside ...

my_str = "I'm coming somewhere"
my_str = "c'est une espèce de truc informe"
my_str = 'Appelons ça une "patate"'

but then, when I need to put both single and double quotes within a string (yes, it happens in french, because both have a specific use in our language, and both are pretty common) ... it begins to be a mess.

my_str = "J'appuye sur le bouton "Envoyer"" 
    => does not work

My issue was solved by using the corresponding escape codes as described for instance in https://www.htmlhelp.com/reference/html40/entities/special.html That way I can put in the same string both single and double quotes ... and get django translations to work ... without visually offending all the french-speakers population

my_str = "J'appuye sur le bouton &quot;Envoyer&quot;" 
    => works ok

Upvotes: 0

ionecum
ionecum

Reputation: 121

The solution is

#: .\core\constants\address_country_style_types.py:274
msgid "Ascension Island"
msgstr "Ile de l&lsquo;Ascension"

It works, even if it will be used in some JavaScript. Don't use the numeric code &#39;, it will not work inside Form fields, it will not be rendered and you will see the ugly number. I already tested all this.

As I said in the comments, beginning a word with a uppercase accented letter is not recommended. If you put Île and you then sort the list of countries, the Î character will come after the Z and will not be sorted following a natural order, as you would expect.

This is another problem with Python sorting capabilities. It will only follow the extended ASCII code according of each letter encoding number. And Î has an ANSI code of 206, it comes after the Z, which is 90.

Maybe Python provides a solution to this, but I didn't find yet. If someone found it I would be glad to know.

Upvotes: 2

Skratt
Skratt

Reputation: 408

I'm a French speaker, so are most of my users. Very annoying bug. the normal django escaping techniques (through \' or format_html(my_translated_string)) do not work for me as well. I have used instead of ' and it works OK - the compilemessage command works and the html node works ok.

it is however not very elegant or Robust as any future message needs to take this into account, and it is not very common to use the character ´

I found out another better and more robust solution: escaping through template filters.

in html template:

<h5 class="modal-title">{{help_message_body|escape}}</h5>

and in javascript:

modal.find('.modal-message').html('<h5 class="modal-title">{{help_message_body|escapejs}}</h5>')

Upvotes: 0

Mark Derail
Mark Derail

Reputation: 59

The correct way is to "Escape" the single quote, however, you need to know the end-point consuming the text. Like you found out with the backslash, as in: L\'Ascension

Trust me, nobody that is French will like seeing the backquote. Back in the DOS days of the 90's, visually, there was almost no difference. Now with fonts, it gets ugly.

Since you're producing for the web, use a HTML replacement, like '

See this article: Why shouldn't `&apos;` be used to escape single quotes?

Upvotes: 2

user1261774
user1261774

Reputation: 3695

I am unsure what is the cause of this issue (maybe that the translator somehow corrupted the file?).

However, a workaround is instead of using the standard single quotation mark ', I have used this single quotation mark (taken from symbols in MS Word):

I am yet to check this with the French translator, but it looks and works OK.

I hope this helps someone.

Upvotes: 4

Related Questions