Language functions

Each session has a language associated with.

Active language is “negotiated” by plone.i18n.negotiator module. Several factors may involve determining what the language should be:

  • Cookies (setting from the language selector)
  • Domain name (like .fi for Finnish, .se for Swedish)
  • Context (current content) language
  • Browser language headers

Language is negotiated at the beginning of the page view.

Getting the current language

Example

from Acquisition import aq_inner
from zope.component import getMultiAdapter

context = aq_inner(self.context)
portal_state = getMultiAdapter((context, self.request), name=u'plone_portal_state')

current_language = portal_state.language()

Getting language of content item

All content object don’t necessarily support Language() look-up defined by IDublinCore interface. Below is the safe way to extract the served language on the content.

Example BrowserView method:

from Acquisition import aq_inner

def language(self):
    """ Get the language of the context.

    Useful in producing <html> tag.
    You need to output language for every HTML page, see http://www.w3.org/TR/xhtml1/#strict


    @return: The two letter language code of the current content.
    """
    portal_state = self.context.unrestrictedTraverse("@@plone_portal_state")

    return aq_inner(self.context).Language() or portal_state.default_language()

Set site language settings

Manually:

# Setup site langauge settings
portal = context.getSite()
ltool = portal.portal_languages
defaultLanguage = 'en'
supportedLanguages = ['en','es']
ltool.manage_setLanguageSettings(defaultLanguage, supportedLanguages,
                                      setUseCombinedLanguageCodes=False)

For unit testing, you need to run this in afterSetUp() after setting up the languages:

# THIS IS FOR UNIT TESTING ONLY
# Normally called by pretraverse hook,
# but must be called manually for the unit tests
# Goes only for the current request
ltool.setLanguageBindings()

Using GenericSetup and propertiestool.xml

On Linguaplone enabled sites, using GenericSetup XML portal_languages.xml

Customizing language selector

Multilingual Plone has two kinds of language selector viewlets

  • Plone vanilla
  • LinguaPlone - LinguaPlone has its own language selector which replaces the default Plone selector if the add on product is installed

More information

Making language flags point to different top level domains

If you use multiple domain names for different language it is often desirable to make the language selector point to a different domain. Search engines do not really like the dynamic language switcher and will index switching links, messing up your site search results.

Example