Esempio n. 1
0
    def _decide_page(self, req):
        """decide page name to use regarding client language.
        This function is called from hook with self as WikiModule instance.
        Returns page name to be displayed.
        """
        orig = req.args.get('page', 'WikiStart')

        # This negotiation is only for viewing.
        # If without this rule, you may edit 'SomePage.en' by edit
        # button on 'SomePage.' page.
        if req.args.get('action', None):
            return orig

        # Get language from session to keep selected language if possible.
        lang = req.session.get('wiki_lang', None)
        
        # Use requested page itself if the page name has language
        # suffix. As special case, if the name ends with period '.',
        # use non-suffixed page is used.
        # This action means user choose the language.
        if orig.endswith('.'):
            req.session['wiki_lang'] = 'default'
            return orig[:-1]                    # force un suffixed page

        page, lang = util.split_lang(orig)
        if lang:
            #debug('case 1 : requested page name has lang suffix')
            req.session['wiki_lang'] = lang
            return orig
        page = page or 'WikiStart'

        # At this point, page name does not have language suffix.

        # List preffered language codes from http request.
        # Check language suffixed page existance in order of preffered
        # language codes in http request and use it if exist.
        wiki = WikiSystem(self.env)
        for lang in util.get_preferred_langs(req):
            if lang in ['default', 'other']:
                lpage = page
            else:
                lpage = '%s.%s' % (page, lang)
            if wiki.has_page(lpage):
                #debug('case 2 : have suffixed page: %s' % lpage)
                return lpage
            # Consideration of default_lang setting.
            # This is required not access to lower ordered
            # language page. For example,
            #   preferred language:  ja, en, fr
            #   default_lang: en
            #   existing pages:  'page', 'page.fr'
            # We should search page in order:
            #   'page.ja', 'page.en', 'page', 'page.fr'
            if lang == self._default_lang and wiki.has_page(page):
                return page

        # No page matched with preffered language.
        # Use requested page.
        #debug('case 3 : no page for preffered lang')
        return orig
Esempio n. 2
0
    def _decide_page(self, req):
        """decide page name to use regarding client language.
        This function is called from hook with self as WikiModule instance.
        Returns page name to be displayed.
        """
        orig = req.args.get('page', 'WikiStart')

        # This negotiation is only for viewing.
        # If without this rule, you may edit 'SomePage.en' by edit
        # button on 'SomePage.' page.
        if req.args.get('action', None):
            return orig

        # Get language from session to keep selected language if possible.
        lang = req.session.get('wiki_lang', None)

        # Use requested page itself if the page name has language
        # suffix. As special case, if the name ends with period '.',
        # use non-suffixed page is used.
        # This action means user choose the language.
        if orig.endswith('.'):
            req.session['wiki_lang'] = 'default'
            return orig[:-1]  # force un suffixed page

        page, lang = util.split_lang(orig)
        if lang:
            #debug('case 1 : requested page name has lang suffix')
            req.session['wiki_lang'] = lang
            return orig
        page = page or 'WikiStart'

        # At this point, page name does not have language suffix.

        # List preffered language codes from http request.
        # Check language suffixed page existance in order of preffered
        # language codes in http request and use it if exist.
        wiki = WikiSystem(self.env)
        for lang in util.get_preferred_langs(req):
            if lang in ['default', 'other']:
                lpage = page
            else:
                lpage = '%s.%s' % (page, lang)
            if wiki.has_page(lpage):
                #debug('case 2 : have suffixed page: %s' % lpage)
                return lpage
            # Consideration of default_lang setting.
            # This is required not access to lower ordered
            # language page. For example,
            #   preferred language:  ja, en, fr
            #   default_lang: en
            #   existing pages:  'page', 'page.fr'
            # We should search page in order:
            #   'page.ja', 'page.en', 'page', 'page.fr'
            if lang == self._default_lang and wiki.has_page(page):
                return page

        # No page matched with preffered language.
        # Use requested page.
        #debug('case 3 : no page for preffered lang')
        return orig
Esempio n. 3
0
 def pre_process_request(self, req, handler):
     """Replace requested wiki page name by content negotiation.
     Nothing changed for non-wiki request.
     """
     if isinstance(handler, WikiModule):
         orig = req.args.get('page', 'WikiStart')
         # set altered page name
         page = self._decide_page(req)
         if page != orig:
             req.args['page'] = page
             self.env.log.debug('Negotiated: %s' % page)
             # Set Content-Language only when language specific
             # page is selected.
             # TODO: I don't know we should set default language
             #       for base page.
             _, lang = util.split_lang(page)
             req.send_header('Content-Language', lang or self._default_lang)
         # always send Vary header to tell language negitiation is
         # available
         req.send_header('Vary', 'Accept-Language')
     return handler
Esempio n. 4
0
 def pre_process_request(self, req, handler):
     """Replace requested wiki page name by content negotiation.
     Nothing changed for non-wiki request.
     """
     if isinstance(handler, WikiModule):
         orig = req.args.get('page', 'WikiStart')
         # set altered page name
         page = self._decide_page(req)
         if page != orig:
             req.args['page'] = page
             self.env.log.debug('Negotiated: %s' % page)
             # Set Content-Language only when language specific
             # page is selected.
             # TODO: I don't know we should set default language
             #       for base page.
             _, lang = util.split_lang(page)
             req.send_header('Content-Language',
                             lang or self._default_lang)
         # always send Vary header to tell language negitiation is
         # available
         req.send_header('Vary', 'Accept-Language')
     return handler
Esempio n. 5
0
    def make_menu_bar(self, req):
        langs = self._get_available_langs()
        if not langs:
            return []                   # no need for lang menu
        
        dflt = None                            # default language
        if not self._default_in_menu:
            dflt = self._default_lang
            # add default lang for the mean of default lang page
            if self._default_lang not in langs:
                langs.append(self._default_lang)
                langs.sort()
        
        page = req.args.get('page', 'WikiStart')
        thisname, thislang = util.split_lang(page, 'default')
        selected = req.session.get('wiki_lang', 'default') # selected lang
        css = 'wikinegotiator/css/langmenu-%s.css' % self._menu_style
        add_stylesheet(req, css)
        lang_name_map = util.make_kvmap(self._lang_names)
        
        # add selected lang if need
        if selected not in langs:
            langs.append(selected)
        # Move 'default' to tail in menu bar if option is set or having
        # default_lang page other than default page.
        if 'default' in langs:
            langs.remove('default')
        if self._default_in_menu:
            langs.append('default')             # always
        elif dflt and self._has_page(thisname, dflt) and self._has_page(thisname):
            # both default_lang and default page are exist.
            langs.append('default')

        # make list of menu item
        result = [(tag.span('Language:',
                            title='Select a language of wiki content'),
                   'first')]
        for i, lang in enumerate(langs):
            cls = ''
            acls = ''
            title = ''
            if lang == selected:
                acls += 'selected'
                title = 'selected language'
            if lang == thislang or (lang == dflt and thislang == 'default'
                                    and 'default' not in langs):
                cls += ' active'
                title = 'displaying language (default)'
            if lang == selected  == thislang:
                title = 'selected and displaying language'
            if (lang == dflt and 'default' not in langs) or lang == 'default':
                # map default lang to default page
                suffix = ''
            else:
                suffix = lang
            if not self._has_page(thisname, suffix):
                acls += ' notexist'
                title += ' (not available)'
            if i == len(langs)-1:
                cls += ' last'
            a = tag.a(lang_name_map.get(lang, lang),
                      href=req.href.wiki('%s.%s' % (thisname, suffix)),
                      class_=acls,
                      title=title.strip())
            result.append((a, cls))
        return result
Esempio n. 6
0
    def make_menu_bar(self, req):
        langs = self._get_available_langs()
        if not langs:
            return []  # no need for lang menu

        dflt = None  # default language
        if not self._default_in_menu:
            dflt = self._default_lang
            # add default lang for the mean of default lang page
            if self._default_lang not in langs:
                langs.append(self._default_lang)
                langs.sort()

        page = req.args.get('page', 'WikiStart')
        thisname, thislang = util.split_lang(page, 'default')
        selected = req.session.get('wiki_lang', 'default')  # selected lang
        css = 'wikinegotiator/css/langmenu-%s.css' % self._menu_style
        add_stylesheet(req, css)
        lang_name_map = util.make_kvmap(self._lang_names)

        # add selected lang if need
        if selected not in langs:
            langs.append(selected)
        # Move 'default' to tail in menu bar if option is set or having
        # default_lang page other than default page.
        if 'default' in langs:
            langs.remove('default')
        if self._default_in_menu:
            langs.append('default')  # always
        elif dflt and self._has_page(thisname,
                                     dflt) and self._has_page(thisname):
            # both default_lang and default page are exist.
            langs.append('default')

        # make list of menu item
        result = [(tag.span('Language:',
                            title='Select a language of wiki content'),
                   'first')]
        for i, lang in enumerate(langs):
            cls = ''
            acls = ''
            title = ''
            if lang == selected:
                acls += 'selected'
                title = 'selected language'
            if lang == thislang or (lang == dflt and thislang == 'default'
                                    and 'default' not in langs):
                cls += ' active'
                title = 'displaying language (default)'
            if lang == selected == thislang:
                title = 'selected and displaying language'
            if (lang == dflt and 'default' not in langs) or lang == 'default':
                # map default lang to default page
                suffix = ''
            else:
                suffix = lang
            if not self._has_page(thisname, suffix):
                acls += ' notexist'
                title += ' (not available)'
            if i == len(langs) - 1:
                cls += ' last'
            a = tag.a(lang_name_map.get(lang, lang),
                      href=req.href.wiki('%s.%s' % (thisname, suffix)),
                      class_=acls,
                      title=title.strip())
            result.append((a, cls))
        return result