weird django translations behaviour - django

I have weird problem with django translations that i need help figuring out. Problem is that instead of getting translated string every time i seem to get randomly either translated string or default string.
I have created a class for putting "action" buttons on several pages. Many of those buttons are reusable so why should i put
blabla
into several templates when i can create simple toolbar and use
toolbar.add(tool)
in view and then just use templatetag for rendering all the tools.... anyway.
This is example of one tool:
class NewUserGroupButton(MyTool):
tool_id = 'new-user-group'
button_label = ugettext(u'Create new user group')
tool_href = 'new/'
js = ()
def render(self):
s = '<a id="%(tool_id)s" href="%(tool_href)s" class="button blue">%(button_label)s</a>'
return s % {'tool_id': self.tool_id, 'tool_href': self.tool_href, 'button_label':self.button_label}
The tools are rendered like this:
class ToolbarTools:
def __init__(self):
self.tools = []
def add(self, tool):
self.tools.append(tool)
def render(self):
# Organize tools by weight
self.tools.sort(key=lambda x: x.weight)
# Render tools
output = '<ul id="toolbar" class="clearfix">'
for tool in self.tools:
output += '<li id="%s">' % ('tool-'+ tool.tool_id)
output += tool.render() if tool.renderable else ''
output += '</li>'
output += '</ul>'
# Reset tools container
self.tools = []
return mark_safe(output)
im using ugettext to translate the string. using (u)gettext=lambda s:s or ugettext_lazy gives me either no translations or proxy object corresponding to function choices.
And like i said - while rest of the page is in correct language the toolbar buttons seem to be randomly either translated or not. The faulty behaviour remains consistent if i move between different pages with different "tools" or even refresh the same page several times.
Could someone please help me to figure out what is causing this problem and how to fix it?

Problem solved.
The issue itself (random translating) was perhaps caused by using ugettext. But at the same time i should have used ugettext_lazy instead.
And thus the problem really came from ugettext_lazy returning proxy object not translated string... and that is caused by this:
[Quote]
Working with lazy translation objects
The result of a ugettext_lazy() call can be used wherever you would use a unicode string (an object with type unicode) in Python. If you try to use it where a bytestring (a str object) is expected, things will not work as expected, since a ugettext_lazy() object doesn't know how to convert itself to a bytestring. You can't use a unicode string inside a bytestring, either, so this is consistent with normal Python behavior. For example:
This is fine: putting a unicode proxy into a unicode string.
u"Hello %s" % ugettext_lazy("people")
This will not work, since you cannot insert a unicode object
into a bytestring (nor can you insert our unicode proxy there)
"Hello %s" % ugettext_lazy("people")
[/Quote]
taken from here:
https://docs.djangoproject.com/en/dev/topics/i18n/translation/#working-with-lazy-translation-objects
Alan

I had the same issue just now. The problem was caused by incorrect Middleware order - I set LocaleMiddleware after CommonMiddleware. After I placed it between SessionMiddleware and CommonMiddleware, it seems to work correctly.
See more here: https://lokalise.com/blog/advanced-django-internationalization/
I know, the problem was solved a long ago, but it can be helpful for someone.

Related

Text Encoding Between Python File, Maya Script Editor, and Maya UI (Python 2.7, Maya 2015, Windows 7)

I've got a python file that I'm loading as a script into the maya script editor. The python file is currently encoded as UTF-8.
I have the need to use the ↑ and ↓ characters (or any other arrow substitutes within Unicode, such as ➘ or ➚, I just want to convey up and down). I'm using the characters as the label of a button. Here's the script:
import Maya.cmds as cmds
def initInterface():
cmds.window("mywin")
cmds.rowColumnLayout("my_rcl", nc=1)
cmds.button(label=u'\↑')
cmds.button(label=u'\↓')
cmds.showWindow("mywin")
initInterface()
The script is saved as myPythonScript.py and is then loaded into the Maya script editor using the load script button.
On execution, I get a UI window and buttons as expected, but the labels for the buttons are now "?" (question marks). I can't seem to get Maya to display the arrows.
To solve this, I've tried a couple of in-code things. Here are a few of my attempts:
# Attempt 1
upArrow = u'\↑'
upArrowEncoded = upArrow.encode("utf-8")
cmds.button(label=upArrowEncoded)
# Result: "?"
# Attempt 2
upArrow = u'\U+2B06'
cmds.button(label=upArrow)
# Result: "?B06"
# Attempt 3
upArrow = u'\U+2B06'
upArrowEncoded = upArrow.encode("utf-8")
cmds.button(label=upArrowEncoded)
# Result: "?"
To be honest (and is likely to be apparent from my code snippets) I've never experimented with text encoding and know next to nothing about it. I'm not sure if I need to change the encoding of my .py file, or encode the string with UTF-16 or something. This is way outside of my area of expertise and I'm having a hard time finding resources to help me understand text and string encoding.
I did check out this:
Unicode Within Maya
And this:
Convert a Unicode String to a String in Python Containing Extra Symbols
But I wasn't able to understand a lot of what I read, and I'm not sure if they relate to this issue or not.
I'm the type of person who doesn't enjoy using code I don't understand (how do people even document that?), so I'm here to ask for links to learning resources and for general advice on the subject, moreso than for a code snippet that does what I want. If it turns out this is not possible, I can use image buttons instead. But they are less efficient and time consuming to produce for each special character I may use.
Thank you for reading through this, and thank you in advance to anyone who can point me in the right direction here. Cheers!
As far as I can tell, the native MayaUi uses/has access to the Code Page 1252 Windows Latin 1 (ANSI) character set (at least on Windows...) as mentioned here, and after some noodling these *all appear to work as advertised.
I'd be curious to see an answer that explained how to change that and access what OP is looking for, but as an alternative to anyone that really truly wants more special characters, may I suggest learning PySide / Qt for building your UI.
Caveats
A lot more boilerplate and setup when it comes to making 'something simple'
Several mayaControls do not have direct Qt implementations (gradientControlNoAttr being a recent discovery, and case in point)
Example is written under the assumption that user has installed and uses Qt.py
Lets dive right in:
import maya.cmds as cmds
import maya.OpenMayaUI as omui
from Qt import QtCore, QtGui
from Qt.QtWidgets import *
from shiboken import wrapInstance
def maya_main_window():
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QWidget)
class TestUi(QDialog):
def __init__(self, parent=maya_main_window()):
super(TestUi, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
def create(self):
self.setWindowTitle("TestUi : Unicode")
self.setWindowFlags(QtCore.Qt.Tool)
self.create_controls()
self.create_layout()
self.create_connections()
def create_controls(self):
"""
Create the widgets for the dialog.
"""
# using "Python source code" unicode values
# ie: https://www.fileformat.info/info/unicode/char/2191/index.htm
self.up_button = QPushButton(u'\u2191')
self.down_button = QPushButton(u'\u2193')
self.left_button = QPushButton(u'\u2190')
self.right_button = QPushButton(u'\u2192')
def create_layout(self):
"""
Create the layouts & add widgets
"""
main_layout = QVBoxLayout()
main_layout.setContentsMargins(6, 6, 6, 6)
main_layout.addWidget(self.up_button)
main_layout.addWidget(self.down_button)
main_layout.addWidget(self.left_button)
main_layout.addWidget(self.right_button)
main_layout.addStretch()
self.setLayout(main_layout)
def create_connections(self):
"""
Create the signal/slot connections
"""
self.up_button.clicked.connect(self.on_button_pressed)
self.down_button.clicked.connect(self.on_button_pressed)
self.left_button.clicked.connect(self.on_button_pressed)
self.right_button.clicked.connect(self.on_button_pressed)
def on_button_pressed(self):
print "Button Pressed"
def LaunchUI():
if __name__ == "__main__":
# Development workaround for PySide winEvent error (Maya 2014)
# Make sure the UI is deleted before recreating
try:
test_ui.deleteLater()
test_ui.close()
except:
pass
# Create minimal UI object
test_ui = TestUi()
# Delete the UI if errors occur to avoid causing winEvent
# and event errors (in Maya 2014)
try:
test_ui.create()
test_ui.show()
except:
test_ui.deleteLater()
traceback.print_exc()
LaunchUI()
There's an awful lot to unpack there for not a terribly huge payoff, but the relevant piece of information is living under "create_controls".

Generate url for Django Simple History historical object

Given an model called Stuff, I want the url to a HistoricalStuff object.
In other words, how does one implement get_historical_url in the below code snippet?
stuff = Stuff.objects.first()
stuff.pk
-> 100
historical_stuff = stuff.history.first() # we want to get url for this
historical_stuff.pk
-> 1
get_historical_url(historical_stuff)
-> /path/to/admin/stuff/100/history/1
Obviously the dumb solution would be to use a format string but I'd rather use urlresolvers
After much digging around, I found in the simple history source code that the url name is similar to the admin change names, namely admin_%s_%s_simple_history.
With this knowledge, get_historical_url looks like
def get_simplehistory_url(history_obj):
parent_obj = history_obj.history_object
return urlresolvers.reverse('admin:{}_{}_simple_history'.format(
parent_obj._meta.app_label, parent_obj._meta.model_name), args=(parent_obj.id, history_obj.pk))

django unicode encode/decode errors

My site needs to be able to serve data in different languages. I set it so it uses utf-8 and the db settings are set to that as well. I've been getting different different unicode errors over the admin.
For example:
In the admin list, when a field from the list contains a non ascii char. (i get UnicodeDecodeError)
When adding a new entry, a UnicodeEncodeError if the unicode method for the model returns an utf-8 decode (which fixes #1).
When using a filter_horizontal in the admin, if data from the used model contains non ascii chars, then the filter disappears from the form.
If I set the unicode method for the model to return for example:
return u'%s' % unicode(self.tag)
That seems to fix #1 and #2, but then that's when I get #3.
I have been looking a lot for a solution, but can't find something that fixes all different errors. What's the best way to deal with those?
from django.utils.encoding import smart_unicode
...
def __unicode__(self):
return smart_unicode(self.tag)
It is noteworthy that you can bypass unicode by simply encoding your data in hexadecimal before storing it in your database.
Something like this is sufficient
MyModel(name=name.encode('hex'), password=password).save()
You can then execute name.decode('hex') to return the data back to its former representation.

Drill down the haystack search results with facets but not changing the facet results

I have search results showing with facet options for drilling down the data. When a facet is selected it then changes the facet results. So if I am originally showing "places (10)", "images (5)", "people (3)", and "All (18)" as faceting options and I click on images I would still like to see all the others too even though the search results changed. Is there a good way to do this?
I just did this and it's actually quite achievable without rerunning the original search query. you just need to use session to store the original facets.
Here's my actual working code:
from haystack.views import FacetedSearchView
class StickyFacetedSearchView (FacetedSearchView):
def top_level_facets(self):
"""
When selecting a facet to drill down the results,
we need to keep the top level facet counts
"""
stored_query = self.request.session.get('query', None)
if stored_query != self.query:
self.request.session['query'] = self.query
self.request.session['facet_counts'] = self.results.facet_counts()
return self.request.session['facet_counts'] # Fail loudly
def extra_context(self):
""" add base_facets to extra_context"""
extra = super(StickyFacetedSearchView, self).extra_context()
extra['base_facets'] = self.top_level_facets()
return extra
Stick the above view in an app called 'mysearch' or similar, then use mysearch.StickyFacetedSearchView in urls.py instead of FacetedSearchView.
To access them use base_facets in the template, rather than facets.
It works perfectly.
I've run into problems with this too. The only answer we could find was to re-run the query each time without the drill-down to get the top-level facet results.

Django Logging i18n ugettext_lazy Variables

I'm having trouble logging the output of an internationalized django variable.
My application code used the 'message' vaiable OK and it displays
in English and German (the two languages I am working with)
My problem is when I try to make a log of the said 'message' variable output.
I want to log in English and/or German (depending on what the end user is seeing)
I'm using string_concat and it says:
the lazy translations in result will only be converted to strings when result
itself is used in a string (usually at template rendering time).
Here is a code sample:
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import string_concat
message = _('Profile Updated OK')
# Build a log message
fmt_msg = _(message)
log_message = 'Message Sent: '
log_message = string_concat(log_message, fmt_msg)
logger.info(log_message)
The log entry is not translated:
Message Sent: <django.utils.functional.__proxy__ object at 0x9c3c4ac>
Can anyone suggest how I can have the proxy object to be translated
when used with logging?
It's translating OK in my regular Django code just the logging.info() issue.
Cheers!
From the docs:
The result of a ugettext_lazy() call can be used wherever you would use a unicode string (an object with type unicode) in Python. If you try to use it where a bytestring (a str object) is expected, things will not work as expected, since a ugettext_lazy() object doesn't know how to convert itself to a bytestring.
Also, don't call _() twice.
logger.info(u'Message Sent: %s' % message)
You have to force Unicode strings, as documented in http://www.djangobook.com/en/2.0/chapter19/
u"Hello %s" % ugettext_lazy("people")
And do not call the function twice ;)