Back to index

plone3  3.1.7
language.py
Go to the documentation of this file.
00001 from plone.app.form.widgets import LanguageDropdownChoiceWidget
00002 
00003 from zope.formlib.form import FormFields
00004 from zope.interface import Interface
00005 from zope.component import adapts
00006 from zope.interface import implements
00007 from zope.schema import Bool
00008 from zope.schema import Choice
00009 
00010 from Acquisition import aq_inner
00011 
00012 from Products.CMFCore.utils import getToolByName
00013 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00014 from Products.CMFPlone import PloneMessageFactory as _
00015 from Products.CMFPlone.interfaces import IPloneSiteRoot
00016 from Products.statusmessages.interfaces import IStatusMessage
00017 
00018 from plone.app.controlpanel.form import ControlPanelForm
00019 
00020 
00021 class ILanguageSelectionSchema(Interface):
00022 
00023     use_combined_language_codes = Bool(
00024         title=_(u'label_allow_combined_language_codes',
00025                 default=u"Show country-specific language variants"),
00026         description=_(u"help_allow_combined_language_codes",
00027                       default=u"Examples: pt-br (Brazilian Portuguese), "
00028                                "en-us (American English) etc."),
00029         default=False,
00030         required=False)
00031 
00032     default_language = Choice(
00033         title=_(u"heading_site_language",
00034                 default=u"Site language"),
00035         description=_(u"description_site_language",
00036                       default=u"The language used for the content and the UI "
00037                                "of this site."),
00038         required=True,
00039         vocabulary="plone.app.vocabularies.AvailableContentLanguages")
00040 
00041 
00042 class LanguageControlPanelAdapter(SchemaAdapterBase):
00043 
00044     adapts(IPloneSiteRoot)
00045     implements(ILanguageSelectionSchema)
00046 
00047     def __init__(self, context):
00048         super(LanguageControlPanelAdapter, self).__init__(context)
00049         self.context = getToolByName(context, 'portal_languages')
00050 
00051     def get_default_language(self):
00052         return aq_inner(self.context).getDefaultLanguage()
00053 
00054     def set_default_language(self, value):
00055         context = aq_inner(self.context)
00056         if isinstance(value, tuple):
00057             value = value[0]
00058         supported_langs = context.getSupportedLanguages()
00059         if value not in supported_langs:
00060             context.supported_langs = [value]
00061         context.setDefaultLanguage(value)
00062 
00063     default_language = property(get_default_language,
00064                                 set_default_language)
00065 
00066     def get_use_combined_language_codes(self):
00067         return aq_inner(self.context).use_combined_language_codes
00068 
00069     def set_use_combined_language_codes(self, value):
00070         context = aq_inner(self.context)
00071         # We are disabling the combined codes, but still have one selected
00072         # as the default.
00073         default = context.getDefaultLanguage()
00074         if len(default.split('-')) > 1:
00075             # XXX This should be done in some kind of validate method instead,
00076             # but I have no time to figure out that part of formlib right now
00077             request = context.REQUEST
00078             message = _(u"You cannot disable country-specific language "
00079                          "variants, please choose a different site "
00080                          "language first.")
00081             IStatusMessage(request).addStatusMessage(message, type='error')
00082         else:
00083             context.use_combined_language_codes = value
00084 
00085     use_combined_language_codes = property(get_use_combined_language_codes,
00086                                            set_use_combined_language_codes)
00087 
00088 
00089 class LanguageControlPanel(ControlPanelForm):
00090 
00091     form_fields = FormFields(ILanguageSelectionSchema)
00092     form_fields['default_language'].custom_widget = LanguageDropdownChoiceWidget
00093 
00094     label = _(u"heading_language_settings", default="Language Settings")
00095     description = _(u"description_language_settings",
00096                     default="Settings related to interface languages and "
00097                             "content translations.")
00098     form_name = _(u"heading_language_settings", default="Language Settings")