pythonpython-2.7pyqt4localeqtranslator

How does QTranslator handle locale modifiers PyQT?


I'm experiencing a problem loading a translation file using QTranslator because it has a locale modifier in it. The code I'm using is,

from PyQt4 import QtCore, QtGui
from locale import getdefaultlocale

locale = getdefaultlocale()

app = QtGui.QApplication(sys.argv)

translator = QtCore.QTranslator(app)
translator.load('/usr/share/my_application/tr/qt_%s.qm' % locale[0])
app.installTranslator(translator)

This code works perfect for just about every locale except ones that contain a modifier (E.G. sr_RS@latin.UTF-8). In that case, it doesn't load the translation file regardless of what's it's named.

I can't seem to figure out what to name the translation file, or what new code I would need to implement in order to get this code functioning.

Thanks in advance, Josh


Solution

  • Locale names are usually in the form:

        language[_territory][.codeset][@modifier]
    

    but the order of the last two elements can vary.

    From the point of view of Qt translator files, it's hard to see how the last two components could be in any way relevant. So the obvious thing to do would be to just trim them off and only use the language and territory.

    The simplest way to do this would be to use QLocale:

        qm = 'qt_%s.qm' % QLocale().name()