def change_lang(request): """ Change current documentation language. """ lang = request.GET.get('lang_code', 'en') # By default, intend to redirect to the home page. response = redirect('/') # Needs to set the preferred language first in case the following code reads lang from portal_helper. portal_helper.set_preferred_language(request, response, lang) # Use the page the user was on, right before attempting to change the language. # If there is a known page the user was on, attempt to redirect to it's root contents. from_path = urllib.unquote(request.GET.get('path', None)) if from_path: # Get which content the user was reading. content_id = request.GET.get('content_id') if content_id: # Get the proper version. docs_version = portal_helper.get_preferred_version(request) # Grabbing root_navigation to check if the current lang supports this book # It also makes sure that all_links_cache is ready. root_navigation = sitemap_helper.get_sitemap(docs_version, lang) if content_id in root_navigation: all_links_cache = cache.get( sitemap_helper.get_all_links_cache_key(docs_version, lang), None) key = url_helper.link_cache_key(from_path) if all_links_cache and key in all_links_cache: response = redirect(all_links_cache[key]) else: # There is no translated path. Use the first link in the contents instead response = _redirect_first_link_in_contents( request, docs_version, content_id) # If the user happens to be coming from the blog. elif from_path.startswith('/blog'): # Blog doesn't a content_id and translated version. Simply redirect back to the original path. response = redirect(from_path) portal_helper.set_preferred_language(request, response, lang) return response
def _transform_urls(version, sitemap, node, all_links_cache, language): all_node_links = [] if sitemap and node: if 'link' in node and language in node['link']: transformed_path = node['link'][language] if not transformed_path.startswith('http'): # We only append the document root/version if this is not an absolute URL path_with_prefix = url_helper.append_prefix_to_path( version, transformed_path) if path_with_prefix: transformed_path = path_with_prefix if transformed_path: node['link'][language] = transformed_path all_node_links.append(transformed_path) if all_links_cache != None: key = url_helper.link_cache_key(transformed_path) all_links_cache[key] = transformed_path if 'sections' in node: for child_node in node['sections']: child_node_links = _transform_urls(version, sitemap, child_node, all_links_cache, language) all_node_links.extend(child_node_links) node['links'] = all_node_links if ('link' not in node or not node['link'][language]): # After we process the node's children, we check if the node has a default link. # If not, then we set the node's first link if len(all_node_links) > 0: node['link'] = {language: all_node_links[0]} return all_node_links