1

When using GTK2 with Python, there are some things like 'gtk-yes', 'gtk-open' which help in getting e.g. button names translated to the according user language.

What I am searching for are more of these: especially the string 'recently used', like in the file open dialog, and strings for 'files' and 'folders'.

Finally I would like to use something like (…yes, I know Python 2 is old…)

label = "{} {}".format(GTK_STOCK_FILES, GTK_STOCK_RECENTLY_USED)
my_button.set_label(label)

to ensure that in (most) languages the label makes at least some sense to show the users that it opens recent files/folders. Unfortunately something like gtk.stock_lookup(gtk.STOCK_DIRECTORY) only returns an empty string (although it works for STOCK_OPEN), also there is nothing like that for 'files' and 'recently' I could find.

How can I find, list, and access all the GTK built-in translated strings?

2
  • as I know it may uses files .po and .mo with translations. On Linux it keeps it in folder /usr/share/locale/ Commented Oct 20 at 12:58
  • it seems there is source code for .po files in GTK2 Commented Oct 20 at 13:03

1 Answer 1

1

OK, looks like to find/list the GTK2 translations with Python2, the answer is the following:

import gettext
from collections import OrderedDict

# Define the domain (filename without extension)
domain = 'gtk20'

# Locale directory leading to language subdirectories
localedir = '/usr/share/locale'

# Languages to pick translation from
#languages = ['de']

# Load the translation, will be based on current user language environment, but use fallback (english) in case it does not exists
gt_trans = gettext.translation(domain, localedir=localedir, fallback=True) #, languages=languages)
# Activate it for the _() global function
gt_trans.install()
# get all translation items
all_trans = gt_trans._catalog

# ordered output of all translations
all_trans = OrderedDict(sorted(all_trans.items(), key=lambda t: t[0]))
for key, value in all_trans.items():
    print(key, value)

def search_translation(search_key = 'file'):
    # translation to search for
    search_key = search_key.lower()
    # Checking if the value contains the substring
    found = {k: v for k, v in all_trans.items() if search_key in "{}".format(k).lower()}
    # ordered output
    found = OrderedDict(sorted(found.items(), key=lambda t: t[0]))
    for k, v in found.items():
        print(k, v)

search_translation()
search_translation('Folder')

print _("_Files")
print _("Select a File")
print _("Folders")
print _("Please select a folder below")
print _("Recently Used")
  • For other translations, than GTK2, change the domain, and you are fine.

  • for testing languages use e. g. export LANGUAGE=es_ES.UTF-8 in console for (here) using spanish, before starting your program from the same console

Thanks for comments above nevertheless.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.