Note that there are some explanatory texts on larger screens.

plurals
  1. POmatplotlib in gtk window with i18n (gettext) support
    text
    copied!<p>I am trying to show a matplotlib plot with axes labeled using gettext's _("label") construct. Trying to create a minimal example, I came up with the following python code. It runs fine through the NULLTranslations() like this:</p> <p>python mpl_i18n_test.py</p> <p>But when I switch to japanese, I get nothing but small squares in the plot -- though on the command-line, the translations look fine:</p> <p>LANG=ja_JP.utf8 python mpl_i18n_test.py</p> <p>Here is the file mpl_i18n_test.py Note that this requires the mona-sazanami font installed, and the various python modules: pygtk, numpy, matplotlib, gettext and polib</p> <p>So my question: Is there some trick to getting matplotlib play nicely with gettext? Am I missing something obvious here? Thank you.</p> <pre class="lang-python prettyprint-override"><code>#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import unicode_literals import gtk import numpy as np import matplotlib as mpl from matplotlib.figure import Figure from matplotlib.backends.backend_gtkagg import \ FigureCanvasGTKAgg as FigureCanvas from matplotlib.backends.backend_gtkagg import \ NavigationToolbar2GTKAgg as NavigationToolbar import locale import gettext import polib mpl.rcParams['font.family'] = 'mona-sazanami' def append(po, msg): occurances = [] for i,l in enumerate(open(__file__,'r')): if "_('"+msg[0]+"')" in l: occurances += [(__file__,str(i+1))] entry = polib.POEntry(msgid=msg[0], msgstr=msg[1], occurrences=occurances) print msg print occurances po.append(entry) def generate_ja_mo_file(): po = polib.POFile() msgs = [ (u'hello', u'こんにちは'), (u'good-bye', u'さようなら'), ] for msg in msgs: append(po, msg) po.save('mpl_i18n_test.po') po.save_as_mofile('mpl_i18n_test.mo') return 'mpl_i18n_test.mo' def initialize(): '''prepare i18n/l10n''' locale.setlocale(locale.LC_ALL, '') loc,enc = locale.getlocale() lang,country = loc.split('_') l = lang.lower() if l == 'ja': filename = generate_ja_mo_file() trans = gettext.GNUTranslations(open(filename, 'rb')) else: trans = gettext.NullTranslations() trans.install() if __name__ == '__main__': initialize() # provides _() method for translations win = gtk.Window(gtk.WINDOW_TOPLEVEL) win.connect("destroy", lambda x: gtk.main_quit()) win.connect("delete_event", lambda x,y: False) win.set_default_size(400,300) win.set_title("Test of unicode in plot") fig = Figure() fig.subplots_adjust(bottom=.14) ax = fig.add_subplot(1,1,1) xx = np.linspace(0,10,100) yy = xx*xx + np.random.normal(0,1,100) ax.plot(xx,yy) print 'hello --&gt; ', _('hello') print 'good-bye --&gt; ', _('good-bye') ax.set_title(u'こんにちは') ax.set_xlabel(_('hello')) ax.set_ylabel(_('good-bye')) can = FigureCanvas(fig) tbar = NavigationToolbar(can,None) vbox = gtk.VBox() vbox.pack_start(can, True, True, 0) vbox.pack_start(tbar, False, False, 0) win.add(vbox) win.show_all() gtk.main() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload