-
Notifications
You must be signed in to change notification settings - Fork 886
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
Pyramid i18n issues and documentation improvement suggestions #2807
Comments
the At first, lingua plugins, like the one you use for chameleon, worked as Babel extractors too, but Wichert changed that at some point, thus that's why the babel approach does not work for you. FWIW I am actually using Babel only, I ditched pot-create and alike, because of the problems therein, and am using that |
@ztane Out of curiosity, what are the "problems therein" that made you switch ? |
@wichert I guess one was that I needed to use babel extractors anyway for JS back then; then there were some other problems, like the extractors didn't work quite like I wanted; for example |
@raspi and you should report those bugs that are strictly lingua to lingua bug tracker |
Hi,
I just started using Pyramid few weeks ago and it also took that time to set up i18n with gettext. My setup: Python 3.5, chameleon templates and gettext. Ubuntu and Arch as dev environment and PyCharm as IDE. I added translation to templates first and code later. Here's some struggles I had.
Issues with
setup.py extract_messages
I added
To
setup.py
as was stated in manual. Running$VENV/bin/setup.py extract_messages
couldn't find chameleon extractor. I tried xml, lingua_chameleon, lingua_xml and lingua.xml, lingua.chameleon, etc. All sorts of combinations. I couldn't find a way to list those extractors. I also triedsetup.cfg
approach.So first suggestion: don't give extractor not found as error. Either list the possible extractors or give instructions how to list them.
Later I found that according to this message in SO the extraction method has been changed somewhere in or before 2015.
I abandoned
setup.py extract_messages
and moved to$VENV/bin/pot-create
.Issues with
$VENV/bin/pot-create
I ran
$VENV/bin/pot-create --output myapp/locale/default.pot myapp
.Error: Nothing found. Hmm. And the
.pot
file is completely empty. Next try:strace -e trace=open $VENV/bin/pot-create --output myapp/locale/default.pot myapp
Ok, it is scanning
.py
and.pt
files from right locations.First suggestion: Please list scanned files while
pot-create
is running or add--verbose
or other parameter to give this information. I got the impression that it was doing nothing.I found that the issue was missing
i18n:domain="myapp"
attribute in the templates. After that strings started to appear to thedefault.pot
.I created my first translation and I could change to it through
pyramid.default_locale_name
in the.ini
file. Finally some progress.Issues with changing language from URL
Next thing I wanted to change language based on URL. For example
/fi/home
,/en/home
, etc. And without adding{language}
parameter toadd_route()
. This route pattern is very common in many web frameworks and there's example(s) how to implement it and it's usually documented in URL routing and/or i18n section. I think it should be added to i18n documentation. I couldn't figure out even where to start how to implement it so I went to IRC for help.I got help from Mikko Ohtamaa and Michael Merickel and here's the current implementation:
Include file:
This wouldn't work with
'add_route'
(changing'add_localized_route'
->'add_route'
) so existing routes had to be changed to:Going to
/
would give error so I had to add some 404 logic:Custom layout would die because it lost the matchdict['lang'] var generated by
add_localized_route()
this fixed it in the__init__.py
beforemain()
:Issues with gettext domains and paths
Now I had translations changing with URL correctly and templates with stuff like
<a class="navbar-brand" href="/" tal:attributes="href request.route_path('home')" i18n:translate="">Home</a>
worked fine and language was added automatically without touching anything.Now I started to implement login and there were the first translatable strings (error messages) in the code. I don't remember all the details but it was either
pot-create
didn't find the translations from the.py
files or those translations weren't transferred to.mo
file during compile. The problem was fixed with just changing the.po
and.mo
filename tomessages.(po|mo)
, because I found from the internal code thatmessages
was the default translation domain.I couldn't find any
.ini
orconfig.
parameters to change default domain. I think this should be configurable.I also found that if
.pt
had justi18n:domain=""
pot-create
couldn't find the translations, but as long as the i18n:domain was just something the translation were found.The
<language>/LC_MESSAGES/
directory seems to be also hard-coded. What other frameworks I've used I had no problems just usingmyapp/locale/<language>.(po|mo)
.I hope this helps to improve i18n docs and future code/modules and making i18n more dynamically configurable.
Some issues may not be directly Pyramid related because I don't yet know what's belonging to what :)
The text was updated successfully, but these errors were encountered: