Source code for django.utils.translation

"""Internationalization support."""from__future__importunicode_literalsimportrefromdjango.utils.decoratorsimportContextDecoratorfromdjango.utils.encodingimportforce_textfromdjango.utils.functionalimportlazyfromdjango.utilsimportsix__all__=['activate','deactivate','override','deactivate_all','get_language','get_language_from_request','get_language_info','get_language_bidi','check_for_language','to_locale','templatize','string_concat','gettext','gettext_lazy','gettext_noop','ugettext','ugettext_lazy','ugettext_noop','ngettext','ngettext_lazy','ungettext','ungettext_lazy','pgettext','pgettext_lazy','npgettext','npgettext_lazy','LANGUAGE_SESSION_KEY',]LANGUAGE_SESSION_KEY='_language'classTranslatorCommentWarning(SyntaxWarning):pass# Here be dragons, so a short explanation of the logic won't hurt:# We are trying to solve two problems: (1) access settings, in particular# settings.USE_I18N, as late as possible, so that modules can be imported# without having to first configure Django, and (2) if some other code creates# a reference to one of these functions, don't break that reference when we# replace the functions with their real counterparts (once we do access the# settings).classTrans(object):""" The purpose of this class is to store the actual translation function upon receiving the first call to that function. After this is done, changes to USE_I18N will have no effect to which function is served upon request. If your tests rely on changing USE_I18N, you can delete all the functions from _trans.__dict__. Note that storing the function with setattr will have a noticeable performance effect, as access to the function goes the normal path, instead of using __getattr__. """def__getattr__(self,real_name):fromdjango.confimportsettingsifsettings.USE_I18N:fromdjango.utils.translationimporttrans_realastranselse:fromdjango.utils.translationimporttrans_nullastranssetattr(self,real_name,getattr(trans,real_name))returngetattr(trans,real_name)_trans=Trans()# The Trans class is no more needed, so remove it from the namespace.delTrans

gettext_lazy=lazy(gettext,str)ugettext_lazy=lazy(ugettext,six.text_type)pgettext_lazy=lazy(pgettext,six.text_type)deflazy_number(func,resultclass,number=None,**kwargs):ifisinstance(number,six.integer_types):kwargs['number']=numberproxy=lazy(func,resultclass)(**kwargs)else:classNumberAwareString(resultclass):def__mod__(self,rhs):ifisinstance(rhs,dict)andnumber:try:number_value=rhs[number]exceptKeyError:raiseKeyError('Your dictionary lacks key \'%s\'. ''Please provide it, because it is required to ''determine whether string is singular or plural.'%number)else:number_value=rhskwargs['number']=number_valuetranslated=func(**kwargs)try:translated=translated%rhsexceptTypeError:# String doesn't contain a placeholder for the numberpassreturntranslatedproxy=lazy(lambda**kwargs:NumberAwareString(),NumberAwareString)(**kwargs)returnproxy

This document is for Django's development version, which can be significantly different from previous releases. For older releases, use the version selector floating in the bottom right corner of this page.