HowtosHome > howto documents > superkaramba > translating themes

 

translating themes

howto translate a superkaramba theme into different languages

If you have a superkaramba theme that is widely used, you may wish to incorporate different translations of the theme for international users. In order to use the i18n translation stuff, you need to get your theme ready. There is a python module called gettext which provides some very useful stuff when it comes to i18n translations. So you'll need to import this. I actually only import the GNUTranslations part, so I use:

  • from gettext import GNUTranslations

I also need the StringIO module, so I import that as well:

  • from StringIO import StringIO

The other thing I do is to create a function to get, and translate the text in my theme. I use this function to do that:

  • def initTranslation(widget, lang = ''):
    • import re
    • global _, encoding, language
    • if(lang == ''):
      • lang = karamba.language(widget)
    • language = lang
    • mo = 'locale/%s/LC_MESSAGES/%s.mo' % (lang, __name__)
    • s = karamba.readThemeFile(widget, mo)
    • if len(s) > 0:
      • fp = StringIO(s)
      • gt = GNUTranslations(fp)
      • _ = gt.ugettext
    • else:
      • _ = lambda(x): x

This will pick up the locale from the "lang" variable (which is read from the config file), get the .mo file for that locale, and set up the object '_' to generate the translated text.

This means that any text that looks like:

  • _('translate this')

or like:

  • _(aVariable)

will be translated from the files in the locale directory in your theme.

Next thing to do is to mark all of the text in the theme that you want translated as set out above. If you have dynamic text - ie. it is contained in a variable and changes from time to time, you will have to create a function that just holds all the possible texts that could be required by that variable. For example, in liquid weather, the sky conditions are defined by a variable that picks up the text from the on-line weather feed, so I have to create each sky condition in the theme, so that when I create my translation files, all the text that I need translating is picked up:

  • def createSkyConditionPot(widget):
    • a = [_('Rain / snow showers / wind'),
      • _('Thunder in the vicinity'),
      • _('Rain / wind'),
      • _('Haze'),
      • _('Pm light rain'),
      • _('Cloudy and windy')]

This function doesn't actually do anything other than create text with the '_' marker, so that it can be picked up for translation later.

So now you've got all this running, you need to create a .pot file. A .pot file is a template file for your translatable text that translators can use to translate stuff. To do this I use the command:

  • xgettext --no-wrap -o liquid_weather.pot liquid_weather.py

I use the '--no-wrap' option, so that it doesn't try to wrap long sentences onto two lines, which would stop the translations working properly. All this command does is go through your .py file, and pick up every text phrase marked '_('text')' and puts it in a specially formatted file for translation. You should then be able to open this .pot file in kbabel, and translate it.

Once the translation is complete, you should put it in a locale directory in the root of your theme directory (now use the .po suffix), and then have a language and messages directory underneath it. You'll need to use the proper i18n codes for the languages. For example German would be:

  • theme_directory/locale/de/LC_MESSAGES/theme_name.po

The gettext module can't read this file :). So you'll have to convert it to a binary file called a .mo file. This is very easy, and is done by using this command:

  • msgfmt theme_name.po -o theme_name.mo

done ... you now have a i18n ready theme. You can test it by starting superkaramba and setting the "lang" variable to a language you have a translation for. So, for German, it would be:

  • lang = 'de'

and assuming you've got a German .mo file in the right place, you should see your theme in German.

You can discuss this howto on the forums.