other links ...
superkaramba ...
linux ...
weather ...
superkaramba ...
is a great platform for creating...
eye candy for the kde desktop. However, up until now, it was very difficult to create unicode aware themes, as the superkaramba developers suggested the use of the richText part of the superkaramba API, which was not particularly flexible in its implimentation. However, after a reasonable amount of time, and after being hounded by some users, I have worked out how to make a unicode aware theme, without having to use the richText part of the API. This article sets out how to do this.
originally, I had been using a homemade translation file which had key pairs of english and translated text, which was parsed into a dictionary, and then used from there. However, in version 0.37 and higher, there is now support for i18n based translations, which makes the whole process much easier to code for and to manage. So, here we go ...
getting your theme i18n ready ...
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 impor 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 for kde on the computer (or should do), find the locale in the theme file (if it's available), get all the translations etc and set up the object '_' to generate the translated text.
This means that any text that looks like:
- _('translate this')
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 KDE_LANG system variable to a language you have a translation for. So, for German, type:
- KDE_LANG=de superkaramba
and assuming you've got a German translation, you should see your theme in German.
to sum up ...
If you've got this far, then well done. This is quite complex, and it might be worth digging round in the source to liquid_weather.py to try and make sense of my rambling above. If you still need help, email me and I'll try to see what I can do.