Package base :: Package includes :: Module language
[hide private]

Source Code for Module base.includes.language

  1  #!/usr/bin/env python 
  2  # Id: language.inc,v 1.16 2008/04/14 17:48:33 dries Exp $ 
  3   
  4  """ 
  5    Multiple language handling functionality. 
  6   
  7    @package includes 
  8    @see <a href='http://drupy.net'>Drupy Homepage</a> 
  9    @see <a href='http://drupal.org'>Drupal Homepage</a> 
 10    @note Drupy is a port of the Drupal project. 
 11    @note This file was ported from Drupal's includes/language.inc 
 12    @author Brendon Crawford 
 13    @copyright 2008 Brendon Crawford 
 14    @contact message144 at users dot sourceforge dot net 
 15    @created 2008-06-07 
 16    @version 0.1 
 17    @note License: 
 18   
 19      This program is free software; you can redistribute it and/or 
 20      modify it under the terms of the GNU General Public License 
 21      as published by the Free Software Foundation; either version 2 
 22      of the License, or (at your option) any later version. 
 23   
 24      This program is distributed in the hope that it will be useful, 
 25      but WITHOUT ANY WARRANTY; without even the implied warranty of 
 26      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 27      GNU General Public License for more details. 
 28   
 29      You should have received a copy of the GNU General Public License 
 30      along with this program; if not, write to: 
 31       
 32      The Free Software Foundation, Inc., 
 33      51 Franklin Street, Fifth Floor, 
 34      Boston, MA  02110-1301, 
 35      USA 
 36  """ 
 37   
 38  __version__ = "$Revision: 1 $" 
 39   
 40  from lib.drupy import DrupyPHP as php 
 41  import bootstrap as lib_bootstrap 
 42   
 43   
44 -def initialize():
45 """ 46 Choose a language for the page, based on language negotiation settings. 47 """ 48 # Configured presentation language mode. 49 mode = variable_get('language_negotiation', \ 50 lib_bootstrap.LANGUAGE_NEGOTIATION_NONE) 51 # Get a list of enabled languages. 52 languages = lib_bootstrap.language_list('enabled') 53 languages = languages[1] 54 if mode == lib_bootstrap.LANGUAGE_NEGOTIATION_NONE: 55 return language_default() 56 elif mode == lib_bootstrap.LANGUAGE_NEGOTIATION_DOMAIN: 57 for language in languages: 58 parts = php.parse_url(language.domain) 59 if (not php.empty(parts['host']) and \ 60 (php.SERVER['php.SERVER_NAME'] == parts['host'])): 61 return language 62 return language_default() 63 elif mode == lib_bootstrap.LANGUAGE_NEGOTIATION_PATH_DEFAULT or \ 64 mode == lib_bootstrap.LANGUAGE_NEGOTIATION_PATH: 65 # _GET['q'] might not be available at this time, because 66 # path initialization runs after the language bootstrap phase. 67 args = (php.explode('/', _GET['q']) if php.isset(_GET, 'q') else []) 68 prefix = php.array_shift(args) 69 # Search prefix within enabled languages. 70 for language in languages: 71 if (not php.empty(language.prefix) and language.prefix == prefix): 72 # Rebuild php.GET['q'] with the language removed. 73 php.GET['q'] = php.implode('/', args) 74 return language 75 if (mode == LANGUAGE_NEGOTIATION_PATH_DEFAULT): 76 # If we did not found the language by prefix, choose the default. 77 return language_default() 78 # User language. 79 if (lib_appglobals.user.uid and \ 80 php.isset(languages[lib_appglobals.user.language])): 81 return languages[lib_appglobals.user.language] 82 # Browser accept-language parsing. 83 language = language_from_browser() 84 if (language): 85 return language 86 # Fall back on the default if everything else fails. 87 return language_default()
88 89 90
91 -def from_browser():
92 """ 93 Identify language from the Accept-language HTTP php.header we got. 94 """ 95 # Specified by the user via the browser's Accept Language setting 96 # Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5" 97 browser_langs = [] 98 if (php.isset(php.SERVER, 'HTTP_ACCEPT_LANGUAGE')): 99 browser_accept = php.explode(",", php.SERVER['HTTP_ACCEPT_LANGUAGE']) 100 for i in range(php.count(browser_accept)): 101 # The language part is either a code or a code with a quality. 102 # We cannot do anything with a * code, so it is skipped. 103 # If the quality is missing, it is assumed to be 1 according to the RFC. 104 if (php.preg_match("not ([a-z-]+)(;q=([0-9\\.]+))?not ", \ 105 php.trim(browser_accept[i]), found)): 106 browser_langs[found[1]] = (float(found[3]) if \ 107 php.isset(found, 3) else 1.0) 108 # Order the codes by quality 109 arsort(browser_langs) 110 # Try to find the first preferred language we have 111 languages = language_list('enabled') 112 for langcode,q in browser_langs.items(): 113 if (php.isset(languages['1'], langcode)): 114 return languages['1'][langcode]
115 116 117
118 -def url_rewrite(path, options):
119 """ 120 Rewrite URL's with language based prefix. Parameters are the same 121 as those of the url() function. 122 """ 123 # Only modify relative (insite) URLs. 124 if (not options['external']): 125 # Language can be passed as an option, or we go for current language. 126 if (not php.isset(options, 'language')): 127 options['language'] = lib_appglobals.language 128 lang_type = variable_get('language_negotiation', \ 129 lib_bootstrap.LANGUAGE_NEGOTIATION_NONE) 130 if lang_type == lib_bootstrap.LANGUAGE_NEGOTIATION_NONE: 131 # No language dependent path allowed in this mode. 132 del(options['language']) 133 return 134 elif lang_type == lib_bootstrap.LANGUAGE_NEGOTIATION_DOMAIN: 135 if (options['language'].domain): 136 # Ask for an absolute URL with our modified base_url. 137 options['absolute'] = True 138 options['base_url'] = options['language'].domain 139 return 140 elif lang_type == lib_bootstrap.LANGUAGE_NEGOTIATION_PATH_DEFAULT: 141 default = language_default() 142 if (options['language'].language == default.language): 143 return 144 if lang_type == lib_bootstrap.LANGUAGE_NEGOTIATION_PATH: 145 if (not php.empty(options['language'].prefix)): 146 options['prefix'] = options['language'].prefix + '/' 147 return
148