Get a translated object in a particular language

Sometimes there is a need to get a translated piece of content in a particular language. Some examples include messing around with ATVM or stitching together a front page for a multilanguage website from editable content (site editors love pretty interfaces where they can tweak and edit every piece of a website). This piece of code is a bit older, but still does the job well. The boundLanguages/getLanguageBindings thing could be replaced with getPreferredLanguage() and I'm pretty sure LinguaPlone got a prettier API than this, but it's a starting point in the right direction. I probably should replace this code more "modern" stuff when I get the time.

#bind context=context

page='training_description'

boundLanguages = context.portal_languages.getLanguageBindings()
prefLang = boundLanguages[0]
obj = getattr(context, page, None)
if obj == None:
return ""
trans = obj.getTranslations()

if trans.has_key(prefLang):
return trans[prefLang][0].CookedBody()
else:
return obj.CookedBody()

Comments