示例#1
0
 def test_no_translations_deactivate_translations(self):
     """
     When the Command handle method is decorated with @no_translations,
     translations are deactivated inside the command.
     """
     current_locale = translation.get_language()
     with translation.override('pl'):
         result = management.call_command('no_translations', stdout=StringIO())
         self.assertIsNone(result)
     self.assertEqual(translation.get_language(), current_locale)
示例#2
0
def _i18n_cache_key_suffix(request, cache_key):
    """If necessary, add the current locale or time zone to the cache key."""
    if settings.USE_I18N or settings.USE_L10N:
        # first check if LocaleMiddleware or another middleware added
        # LANGUAGE_CODE to request, then fall back to the active language
        # which in turn can also fall back to settings.LANGUAGE_CODE
        cache_key += '.%s' % getattr(request, 'LANGUAGE_CODE', get_language())
    if settings.USE_TZ:
        cache_key += '.%s' % get_current_timezone_name()
    return cache_key
示例#3
0
 def wrapped(*args, **kwargs):
     from djmodels.utils import translation
     saved_locale = translation.get_language()
     translation.deactivate_all()
     try:
         res = handle_func(*args, **kwargs)
     finally:
         if saved_locale is not None:
             translation.activate(saved_locale)
     return res
示例#4
0
def get_format_modules(lang=None, reverse=False):
    """Return a list of the format modules found."""
    if lang is None:
        lang = get_language()
    if lang not in _format_modules_cache:
        _format_modules_cache[lang] = list(iter_format_modules(lang, settings.FORMAT_MODULE_PATH))
    modules = _format_modules_cache[lang]
    if reverse:
        return list(reversed(modules))
    return modules
示例#5
0
def number_format(value, decimal_pos=None, use_l10n=None, force_grouping=False):
    """
    Format a numeric value using localization settings.

    If use_l10n is provided and is not None, it forces the value to
    be localized (or not), overriding the value of settings.USE_L10N.
    """
    if use_l10n or (use_l10n is None and settings.USE_L10N):
        lang = get_language()
    else:
        lang = None
    return numberformat.format(
        value,
        get_format('DECIMAL_SEPARATOR', lang, use_l10n=use_l10n),
        decimal_pos,
        get_format('NUMBER_GROUPING', lang, use_l10n=use_l10n),
        get_format('THOUSAND_SEPARATOR', lang, use_l10n=use_l10n),
        force_grouping=force_grouping,
        use_l10n=use_l10n,
    )
示例#6
0
def get_format(format_type, lang=None, use_l10n=None):
    """
    For a specific format type, return the format for the current
    language (locale). Default to the format in the settings.
    format_type is the name of the format, e.g. 'DATE_FORMAT'.

    If use_l10n is provided and is not None, it forces the value to
    be localized (or not), overriding the value of settings.USE_L10N.
    """
    use_l10n = use_l10n or (use_l10n is None and settings.USE_L10N)
    if use_l10n and lang is None:
        lang = get_language()
    cache_key = (format_type, lang)
    try:
        return _format_cache[cache_key]
    except KeyError:
        pass

    # The requested format_type has not been cached yet. Try to find it in any
    # of the format_modules for the given lang if l10n is enabled. If it's not
    # there or if l10n is disabled, fall back to the project settings.
    val = None
    if use_l10n:
        for module in get_format_modules(lang):
            val = getattr(module, format_type, None)
            if val is not None:
                break
    if val is None:
        if format_type not in FORMAT_SETTINGS:
            return format_type
        val = getattr(settings, format_type)
    elif format_type in ISO_INPUT_FORMATS:
        # If a list of input formats from one of the format_modules was
        # retrieved, make sure the ISO_INPUT_FORMATS are in this list.
        val = list(val)
        for iso_input in ISO_INPUT_FORMATS.get(format_type, ()):
            if iso_input not in val:
                val.append(iso_input)
    _format_cache[cache_key] = val
    return val
示例#7
0
 def setUp(self):
     self._orig_lang = translation.get_language()
     translation.activate('en-us')
示例#8
0
 def handle(self, *args, **options):
     return translation.get_language()
示例#9
0
 def get_extra_descriptor_filter(self, instance):
     return models.Q(lang=get_language())
示例#10
0
 def get_extra_descriptor_filter(self, instance):
     return {'lang': get_language()}
示例#11
0
 def get_extra_restriction(self, where_class, alias, related_alias):
     return ColConstraint(alias, 'lang', get_language())
示例#12
0
 def test_language_preserved(self):
     out = StringIO()
     with translation.override('fr'):
         management.call_command('dance', stdout=out)
         self.assertEqual(translation.get_language(), 'fr')
示例#13
0
 def setUp(self):
     self._language = get_language()
     self._translations = trans_real._translations
     activate('fr')