Is it possible to make user to control app's language, not system?

#!@PYTHON@

import os
import sys
import signal
import locale
import gettext

VERSION = '@VERSION@'
pkgdatadir = '@pkgdatadir@'
localedir = '@localedir@'

sys.path.insert(1, pkgdatadir)
signal.signal(signal.SIGINT, signal.SIG_DFL)
locale.bindtextdomain('lrcmake', localedir)
locale.textdomain('lrcmake')
gettext.install('lrcmake', localedir)

if __name__ == '__main__':
    import gi

    from gi.repository import Gio
    resource = Gio.Resource.load(os.path.join(pkgdatadir, 'lrcmake-gtk.gresource'))
    resource._register()

    from lrcmake import main
    sys.exit(main.main(VERSION, localedir))

In this autogenerated file, locale is set by system locale, how can I make it to select language by user’s configuration not by system? For example from GSchema.

You can’t, because that’s not how gettext works.

Gettext uses environment variables to set up the localisation details; environment variables can only be set at startup.

You could call setlocale() to set the locale to a specific value, but there is no mechanism to change the localised strings at run time, so you’d have to do it before setting up your UI anyway.

1 Like

I have tried setlocale method, but when I’m trying to set any locale differs from ru_RU or en_US, I’ve got this error locale.Error: unsupported locale setting.
Is this because locale using system locales instead ones in lang.po files?

#!@PYTHON@

import os
import sys
import signal
import locale
import gettext

VERSION = '@VERSION@'
pkgdatadir = '@pkgdatadir@'
localedir = '@localedir@'

sys.path.insert(1, pkgdatadir)
signal.signal(signal.SIGINT, signal.SIG_DFL)
locale.bindtextdomain('lrcmake', localedir)
locale.textdomain('lrcmake')
gettext.install('lrcmake', localedir)

if __name__ == '__main__':
    import gi

    from gi.repository import Gio
    resource = Gio.Resource.load(os.path.join(pkgdatadir, 'lrcmake-gtk.gresource'))
    resource._register()
    from lrcmake import shared
    if not shared.schema.get_boolean("use-sys-lang"):
        print(locale.normalize(shared.state_schema.get_string("lang"))[:5])
        locale.setlocale(locale.LC_ALL, locale.normalize(shared.state_schema.get_string("lang"))[:5])

    from lrcmake import main
    sys.exit(main.main(VERSION, localedir))

This is because there are no installed locales for that language; how to generate/install locales is part of your distribution, and GNOME or GTK have nothing to do with it.

No, this has nothing to do with that. Just because you have a ${LANG}.po file doesn’t mean there’s a valid locale. First of all, the po files are “sources”, that get converted into a localisation manifest; then you also need the locale data for a specific language, which is part of your OS, not your application, and since locale data can be arbitrarily large, most distribution do not ship it by default: you have to install it manually.

1 Like

Well, I get it. Thank you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.