def select_best_available_language(target_code, available_codes=None):
 target_code = lcode_to_django_lang(target_code)
 store_cache = False
 if (available_codes is None):
  if (target_code in __select_best_available_language):
   return __select_best_available_language[target_code]
  else:
   store_cache = True
   available_codes = get_installed_language_packs().keys()
 available_codes = [lcode_to_django_lang(lc) for lc in available_codes if lc]
 if (target_code in available_codes):
  actual_code = target_code
 elif (target_code.split('-', 1)[0] in available_codes):
  actual_code = target_code.split('-', 1)[0]
 elif (settings.LANGUAGE_CODE in available_codes):
  actual_code = settings.LANGUAGE_CODE
 elif ('en' in available_codes):
  actual_code = 'en'
 elif available_codes:
  actual_code = available_codes[0]
 else:
  raise RuntimeError('No languages found')
 if store_cache:
  __select_best_available_language[target_code] = actual_code
 return actual_code

def select_best_available_language ( target_code , available_codes = None ) : target_code = lcode_to_django_lang ( target_code ) store_cache = False if ( available_codes is None ) : if ( target_code in __select_best_available_language ) : return __select_best_available_language [ target_code ] else : store_cache = True available_codes = get_installed_language_packs ( ) . keys ( ) available_codes = [ lcode_to_django_lang ( lc ) for lc in available_codes if lc ] if ( target_code in available_codes ) : actual_code = target_code elif ( target_code . split ( '-' , 1 ) [ 0 ] in available_codes ) : actual_code = target_code . split ( '-' , 1 ) [ 0 ] elif ( settings . LANGUAGE_CODE in available_codes ) : actual_code = settings . LANGUAGE_CODE elif ( 'en' in available_codes ) : actual_code = 'en' elif available_codes : actual_code = available_codes [ 0 ] else : raise RuntimeError ( 'No languages found' ) if store_cache : __select_best_available_language [ target_code ] = actual_code return actual_code
critical function for choosing the best available language for a resource .