Changeset 964
- Timestamp:
- 10/19/05 18:55:37 (3 years ago)
- Files:
-
- django/branches/i18n/django/bin/django-admin.py (modified) (1 diff)
- django/branches/i18n/django/middleware/locale.py (modified) (2 diffs)
- django/branches/i18n/django/utils/translation.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/i18n/django/bin/django-admin.py
r943 r964 7 7 # switch to english, because django-admin creates database content 8 8 # like permissions, and those shouldn't contain any translations 9 translation.activate(' *', 'en-us')9 translation.activate('en-us') 10 10 11 11 ACTION_MAPPING = { django/branches/i18n/django/middleware/locale.py
r851 r964 3 3 from django.utils.cache import patch_vary_headers 4 4 from django.utils import translation 5 6 # this is a cache that will build a map from modules to applications7 _module_to_app = {}8 5 9 6 class LocaleMiddleware: … … 16 13 """ 17 14 18 def process_view(self, request, view_func, param_dict): 19 global _module_to_app 20 21 def findapp(module): 22 app = _module_to_app.get(view_func.__module__, None) 23 if app is not None: 24 return app 25 26 from django.conf import settings 27 for app in settings.INSTALLED_APPS: 28 if module.startswith(app): 29 _module_to_app[module] = app 30 return app 31 return '*' 32 33 app = findapp(view_func.__module__) 34 35 lang = translation.get_language_from_request(request) 36 37 translation.activate(app, lang) 38 15 def process_request(self, request): 16 language = translation.get_language_from_request(request) 17 translation.activate(language) 39 18 request.LANGUAGE_CODE = translation.get_language() 40 19 41 20 def process_response(self, request, response): 42 21 patch_vary_headers(response, ('Accept-Language',)) 22 translation.deactivate() 43 23 return response 44 24 django/branches/i18n/django/utils/translation.py
r943 r964 37 37 p = language.find('-') 38 38 if p >= 0: 39 return language[:p].lower()+'_'+language[p :].upper()39 return language[:p].lower()+'_'+language[p+1:].upper() 40 40 else: 41 41 return language.lower() … … 45 45 p = locale.find('_') 46 46 if p >= 0: 47 return locale[:p].lower()+'-'+locale[p :].lower()47 return locale[:p].lower()+'-'+locale[p+1:].lower() 48 48 else: 49 49 return locale.lower() … … 68 68 pass 69 69 self.django_output_charset = settings.DEFAULT_CHARSET 70 self.__app = '?.?.?'71 70 self.__language = '??' 72 73 def set_app_and_language(self, app, language): 74 self.__app = app 71 72 def merge(self, other): 73 self._catalog.update(other._catalog) 74 75 def set_language(self, language): 75 76 self.__language = language 76 77 … … 79 80 80 81 def __repr__(self): 81 return "<DjangoTranslation app:%s lang:%s>" % (self.__app, self.__language)82 return "<DjangoTranslation lang:%s>" % self.__language 82 83 83 84 … … 102 103 return res.encode(self.django_output_charset) 103 104 104 def translation(appname, language): 105 """ 106 This function returns a translation object. 107 app must be the fully qualified name of the 108 application. 109 110 This function will first look into the app 111 messages directory for the django message file, 112 then in the project messages directory for the 113 django message file and last in the global 114 messages directory for the django message file. 115 """ 116 117 t = _translations.get((appname, language), None) 105 def translation(language): 106 """ 107 This function returns a translation object. app must be the fully 108 qualified name of the application. 109 110 This translation object will be constructed out of multiple GNUTranslations 111 objects by merging their catalogs. It will construct a object for the requested 112 language and add a fallback to the default language, if that is different 113 from the requested language. 114 """ 115 global _translations 116 117 if language == 'en' or language.startswith('en-'): 118 return gettext_module.NullTranslations() 119 120 t = _translations.get(language, None) 118 121 if t is not None: 119 122 return t 120 123 121 124 from django.conf import settings 122 123 default_locale = to_locale(settings.LANGUAGE_CODE)124 locale = to_locale(language)125 125 126 126 # set up the right translation class … … 131 131 globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale') 132 132 133 try:134 t = gettext_module.translation('django', globalpath, [locale, default_locale], klass)135 t.set_app_and_language(appname, language)136 except IOError: t = gettext_module.NullTranslations()137 _translations[(appname, language)] = t138 139 if hasattr(settings, 'LOCALE_PATHS'):140 for localepath in settings.LOCALE_PATHS:141 try:142 t = gettext_module.translation('django', localepath, [locale, default_locale], klass)143 t.set_app_and_language(appname, language)144 except IOError: t = None145 if t is not None:146 t.add_fallback(_translations[(appname, language)])147 _translations[(appname, language)] = t148 149 133 parts = os.environ['DJANGO_SETTINGS_MODULE'].split('.') 150 134 project = __import__(parts[0], {}, {}, []) 151 152 135 projectpath = os.path.join(os.path.dirname(project.__file__), 'locale') 153 136 154 try: 155 t = gettext_module.translation('django', projectpath, [locale, default_locale], klass) 156 t.set_app_and_language(appname, language) 157 except IOError: t = None 158 if t is not None: 159 t.add_fallback(_translations[(appname, language)]) 160 _translations[(appname, language)] = t 161 162 if appname != '*': 163 app = __import__(appname, {}, {}, ['views']) 164 165 apppath = os.path.join(os.path.dirname(app.__file__), 'locale') 166 167 try: 168 t = gettext_module.translation('django', apppath, [locale, default_locale], klass) 169 t.set_app_and_language(appname, language) 170 except IOError: t = None 171 if t is not None: 172 t.add_fallback(_translations[(appname, language)]) 173 _translations[(appname, language)] = t 174 175 return _translations[(appname, language)] 176 177 def activate(appname, language): 137 def _fetch(lang, fallback=None): 138 139 global _translations 140 141 loc = to_locale(lang) 142 143 res = _translations.get(lang, None) 144 if res is not None: 145 return res 146 147 def _translation(path): 148 try: 149 t = gettext_module.translation('django', path, [loc], klass) 150 t.set_language(lang) 151 return t 152 except IOError, e: 153 return None 154 155 res = _translation(globalpath) 156 157 def _merge(path): 158 t = _translation(path) 159 if t is not None: 160 if res is None: 161 return t 162 else: 163 res.merge(t) 164 return res 165 166 if hasattr(settings, 'LOCALE_PATHS'): 167 for localepath in settings.LOCALE_PATHS: 168 if os.path.isdir(localepath): 169 res = _merge(localepath) 170 171 if os.path.isdir(projectpath): 172 res = _merge(projectpath) 173 174 for appname in settings.INSTALLED_APPS: 175 p = appname.rfind('.') 176 if p >= 0: 177 app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:]) 178 else: 179 app = __import__(appname, {}, {}, []) 180 181 apppath = os.path.join(os.path.dirname(app.__file__), 'locale') 182 183 if os.path.isdir(apppath): 184 res = _merge(apppath) 185 186 if res is None: 187 if fallback is not None: 188 res = fallback 189 else: 190 return gettext_module.NullTranslations() 191 _translations[lang] = res 192 return res 193 194 default_translation = _fetch(settings.LANGUAGE_CODE) 195 current_translation = _fetch(language, fallback=default_translation) 196 197 return current_translation 198 199 def activate(language): 178 200 """ 179 201 This function fetches the translation object for a given … … 181 203 the current translation object for the current thread. 182 204 """ 183 if language == 'en' or language.startswith('en-'): 184 t = gettext_module.NullTranslations() 185 else: 186 t = translation(appname, language) 187 _active[currentThread()] = t 205 _active[currentThread()] = translation(language) 188 206 189 207 def deactivate(): … … 193 211 default translation object, again. 194 212 """ 195 del _active[currentThread()] 213 global _active 214 215 if _active.has_key(currentThread()): 216 del _active[currentThread()] 196 217 197 218 def get_language(): … … 226 247 if _default is None: 227 248 from django.conf import settings 228 _default = translation( '*',settings.LANGUAGE_CODE)249 _default = translation(settings.LANGUAGE_CODE) 229 250 return _default.gettext(message) 230 251 … … 336 357 # did find de_DE because of language normalization 337 358 lang = langfile[len(globalpath):].split('/')[1] 338 _accepted[accept] = to_language(lang)359 _accepted[accept] = lang 339 360 return lang 340 361
