<s> import os <EOL> import gettext as gettext_module <EOL> from django import http <EOL> from django . conf import settings <EOL> from django . utils import importlib <EOL> from django . utils . translation import check_for_language , activate , to_locale , get_language <EOL> from django . utils . text import javascript_quote <EOL> from django . utils . encoding import smart_text <EOL> from django . utils . formats import get_format_modules , get_format <EOL> from django . utils . _os import upath <EOL> from django . utils . http import is_safe_url <EOL> from django . utils import six <EOL> def set_language ( request ) : <EOL> """<STR_LIT>""" <EOL> next = request . REQUEST . get ( '<STR_LIT>' ) <EOL> if not is_safe_url ( url = next , host = request . get_host ( ) ) : <EOL> next = request . META . get ( '<STR_LIT>' ) <EOL> if not is_safe_url ( url = next , host = request . get_host ( ) ) : <EOL> next = '<STR_LIT:/>' <EOL> response = http . HttpResponseRedirect ( next ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> lang_code = request . POST . get ( '<STR_LIT>' , None ) <EOL> if lang_code and check_for_language ( lang_code ) : <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> request . session [ '<STR_LIT>' ] = lang_code <EOL> else : <EOL> response . set_cookie ( settings . LANGUAGE_COOKIE_NAME , lang_code ) <EOL> return response <EOL> def get_formats ( ) : <EOL> """<STR_LIT>""" <EOL> FORMAT_SETTINGS = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> result = { } <EOL> for module in [ settings ] + get_format_modules ( reverse = True ) : <EOL> for attr in FORMAT_SETTINGS : <EOL> result [ attr ] = get_format ( attr ) <EOL> src = [ ] <EOL> for k , v in result . items ( ) : <EOL> if isinstance ( v , ( six . string_types , int ) ) : <EOL> src . append ( "<STR_LIT>" % ( javascript_quote ( k ) , javascript_quote ( smart_text ( v ) ) ) ) <EOL> elif isinstance ( v , ( tuple , list ) ) : <EOL> v = [ javascript_quote ( smart_text ( value ) ) for value in v ] <EOL> src . append ( "<STR_LIT>" % ( javascript_quote ( k ) , "<STR_LIT>" . join ( v ) ) ) <EOL> return '<STR_LIT>' . join ( src ) <EOL> NullSource = """<STR_LIT>""" <EOL> LibHead = """<STR_LIT>""" <EOL> LibFoot = """<STR_LIT>""" <EOL> LibFormatHead = """<STR_LIT>""" <EOL> LibFormatFoot = """<STR_LIT>""" <EOL> SimplePlural = """<STR_LIT>""" <EOL> InterPolate = r"""<STR_LIT>""" <EOL> PluralIdx = r"""<STR_LIT>""" <EOL> def null_javascript_catalog ( request , domain = None , packages = None ) : <EOL> """<STR_LIT>""" <EOL> src = [ NullSource , InterPolate , LibFormatHead , get_formats ( ) , LibFormatFoot ] <EOL> return http . HttpResponse ( '<STR_LIT>' . join ( src ) , '<STR_LIT>' ) <EOL> def javascript_catalog ( request , domain = '<STR_LIT>' , packages = None ) : <EOL> """<STR_LIT>""" <EOL> if request . GET : <EOL> if '<STR_LIT>' in request . GET : <EOL> if check_for_language ( request . GET [ '<STR_LIT>' ] ) : <EOL> activate ( request . GET [ '<STR_LIT>' ] ) <EOL> if packages is None : <EOL> packages = [ '<STR_LIT>' ] <EOL> if isinstance ( packages , six . string_types ) : <EOL> packages = packages . split ( '<STR_LIT:+>' ) <EOL> packages = [ p for p in packages if p == '<STR_LIT>' or p in settings . INSTALLED_APPS ] <EOL> default_locale = to_locale ( settings . LANGUAGE_CODE ) <EOL> locale = to_locale ( get_language ( ) ) <EOL> t = { } <EOL> paths = [ ] <EOL> en_selected = locale . startswith ( '<STR_LIT>' ) <EOL> en_catalog_missing = True <EOL> for package in packages : <EOL> p = importlib . import_module ( package ) <EOL> path = os . path . join ( os . path . dirname ( upath ( p . __file__ ) ) , '<STR_LIT>' ) <EOL> paths . append ( path ) <EOL> paths . extend ( list ( reversed ( settings . LOCALE_PATHS ) ) ) <EOL> for path in paths : <EOL> try : <EOL> catalog = gettext_module . translation ( domain , path , [ '<STR_LIT>' ] ) <EOL> t . update ( catalog . _catalog ) <EOL> except IOError : <EOL> pass <EOL> else : <EOL> if en_selected : <EOL> en_catalog_missing = False <EOL> if default_locale != '<STR_LIT>' : <EOL> for path in paths : <EOL> try : <EOL> catalog = gettext_module . translation ( domain , path , [ default_locale ] ) <EOL> except IOError : <EOL> catalog = None <EOL> if catalog is not None : <EOL> t . update ( catalog . _catalog ) <EOL> if locale != default_locale : <EOL> if en_selected and en_catalog_missing : <EOL> t = { } <EOL> else : <EOL> locale_t = { } <EOL> for path in paths : <EOL> try : <EOL> catalog = gettext_module . translation ( domain , path , [ locale ] ) <EOL> except IOError : <EOL> catalog = None <EOL> if catalog is not None : <EOL> locale_t . update ( catalog . _catalog ) <EOL> if locale_t : <EOL> t = locale_t <EOL> src = [ LibHead ] <EOL> plural = None <EOL> if '<STR_LIT>' in t : <EOL> for l in t [ '<STR_LIT>' ] . split ( '<STR_LIT:\n>' ) : <EOL> if l . startswith ( '<STR_LIT>' ) : <EOL> plural = l . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . strip ( ) <EOL> if plural is not None : <EOL> plural = [ el . strip ( ) for el in plural . split ( '<STR_LIT:;>' ) if el . strip ( ) . startswith ( '<STR_LIT>' ) ] [ <NUM_LIT:0> ] . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> src . append ( PluralIdx % plural ) <EOL> else : <EOL> src . append ( SimplePlural ) <EOL> csrc = [ ] <EOL> pdict = { } <EOL> for k , v in t . items ( ) : <EOL> if k == '<STR_LIT>' : <EOL> continue <EOL> if isinstance ( k , six . string_types ) : <EOL> csrc . append ( "<STR_LIT>" % ( javascript_quote ( k ) , javascript_quote ( v ) ) ) <EOL> elif isinstance ( k , tuple ) : <EOL> if k [ <NUM_LIT:0> ] not in pdict : <EOL> pdict [ k [ <NUM_LIT:0> ] ] = k [ <NUM_LIT:1> ] <EOL> else : <EOL> pdict [ k [ <NUM_LIT:0> ] ] = max ( k [ <NUM_LIT:1> ] , pdict [ k [ <NUM_LIT:0> ] ] ) <EOL> csrc . append ( "<STR_LIT>" % ( javascript_quote ( k [ <NUM_LIT:0> ] ) , k [ <NUM_LIT:1> ] , javascript_quote ( v ) ) ) <EOL> else : <EOL> raise TypeError ( k ) <EOL> csrc . sort ( ) <EOL> for k , v in pdict . items ( ) : <EOL> src . append ( "<STR_LIT>" % ( javascript_quote ( k ) , '<STR_LIT:U+002C>' . join ( [ "<STR_LIT>" ] * ( v + <NUM_LIT:1> ) ) ) ) <EOL> src . extend ( csrc ) <EOL> src . append ( LibFoot ) <EOL> src . append ( InterPolate ) <EOL> src . append ( LibFormatHead ) <EOL> src . append ( get_formats ( ) ) <EOL> src . append ( LibFormatFoot ) <EOL> src = '<STR_LIT>' . join ( src ) <EOL> return http . HttpResponse ( src , '<STR_LIT>' ) </s>
