我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用django.utils.translation.deactivate_all()。
def move_all_color_into_groups(apps, schema_editor): LocationGroupCategory = apps.get_model('mapdata', 'LocationGroupCategory') category = LocationGroupCategory.objects.get(name='groups') colors = {} for model_name in ('Level', 'Space', 'Area', 'POI'): model = apps.get_model('mapdata', model_name) for obj in model.objects.filter(color__isnull=False): colors.setdefault(obj.color, []).append(obj) from c3nav.mapdata.models import Location for color, objects in colors.items(): titles = {lang: [] for lang in set(chain(*(obj.titles.keys() for obj in objects)))} for obj in objects: for lang in titles.keys(): translation.activate(lang) titles[lang].append(Location(titles=obj.titles).title) translation.deactivate_all() titles = {lang: ', '.join(values) for lang, values in titles.items()} group = category.groups.create(can_search=False, can_describe=False, color=color, titles=titles) for obj in objects: obj.groups.add(group)
def test_when_all_language_is_deactivated(settings): settings.USE_I18N = True settings.USE_L10N = True with translation_being('en'): translation.deactivate_all() ck = CacheKey(result=Group(pk=1)) prefix = ck.i18n_l10n_prefix assert prefix == '.'
def get(self, request, *args, **kwargs): # Set language as is tenant language rec = Client.objects.get(pk=request.tenant.pk) translation.deactivate_all() translation.activate(rec.lang) # if the form updated we need to clear message query self.clearMsg(request) return super().get(self, request, *args, **kwargs)
def get(self, request, *args, **kwargs): # Set language as is tenant language rec = Client.objects.get(pk=request.tenant.pk) translation.deactivate_all() translation.activate(rec.lang) # if the form updated we need to clear message query self.clearMsg(request) resp = auth_views.login(request, template_name='accounts/login.html', authentication_form=MyAuthenticationForm) return resp
def execute(self, *args, **options): """ Try to execute this command, performing system checks if needed (as controlled by the ``requires_system_checks`` attribute, except if force-skipped). """ if options.get('no_color'): self.style = no_style() self.stderr.style_func = None if options.get('stdout'): self.stdout = OutputWrapper(options['stdout']) if options.get('stderr'): self.stderr = OutputWrapper(options.get('stderr'), self.stderr.style_func) saved_locale = None if not self.leave_locale_alone: # Only mess with locales if we can assume we have a working # settings file, because django.utils.translation requires settings # (The final saying about whether the i18n machinery is active will be # found in the value of the USE_I18N setting) if not self.can_import_settings: raise CommandError("Incompatible values of 'leave_locale_alone' " "(%s) and 'can_import_settings' (%s) command " "options." % (self.leave_locale_alone, self.can_import_settings)) # Deactivate translations, because django-admin creates database # content like permissions, and those shouldn't contain any # translations. from django.utils import translation saved_locale = translation.get_language() translation.deactivate_all() try: if (self.requires_system_checks and not options.get('skip_validation') and # Remove at the end of deprecation for `skip_validation`. not options.get('skip_checks')): self.check() output = self.handle(*args, **options) if output: if self.output_transaction: # This needs to be imported here, because it relies on # settings. from django.db import connections, DEFAULT_DB_ALIAS connection = connections[options.get('database', DEFAULT_DB_ALIAS)] if connection.ops.start_transaction_sql(): self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql())) self.stdout.write(output) if self.output_transaction: self.stdout.write('\n' + self.style.SQL_KEYWORD(connection.ops.end_transaction_sql())) finally: if saved_locale is not None: translation.activate(saved_locale)
def execute(self, *args, **options): """ Try to execute this command, performing system checks if needed (as controlled by the ``requires_system_checks`` attribute, except if force-skipped). """ if options['no_color']: self.style = no_style() self.stderr.style_func = None if options.get('stdout'): self.stdout = OutputWrapper(options['stdout']) if options.get('stderr'): self.stderr = OutputWrapper(options['stderr'], self.stderr.style_func) saved_locale = None if not self.leave_locale_alone: # Only mess with locales if we can assume we have a working # settings file, because django.utils.translation requires settings # (The final saying about whether the i18n machinery is active will be # found in the value of the USE_I18N setting) if not self.can_import_settings: raise CommandError("Incompatible values of 'leave_locale_alone' " "(%s) and 'can_import_settings' (%s) command " "options." % (self.leave_locale_alone, self.can_import_settings)) # Deactivate translations, because django-admin creates database # content like permissions, and those shouldn't contain any # translations. from django.utils import translation saved_locale = translation.get_language() translation.deactivate_all() try: if self.requires_system_checks and not options.get('skip_checks'): self.check() if self.requires_migrations_checks: self.check_migrations() output = self.handle(*args, **options) if output: if self.output_transaction: connection = connections[options.get('database', DEFAULT_DB_ALIAS)] output = '%s\n%s\n%s' % ( self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()), output, self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()), ) self.stdout.write(output) finally: if saved_locale is not None: translation.activate(saved_locale) return output
def execute(self, *args, **options): """ Try to execute this command, performing system checks if needed (as controlled by the ``requires_system_checks`` attribute, except if force-skipped). """ if options['no_color']: self.style = no_style() self.stderr.style_func = None if options.get('stdout'): self.stdout = OutputWrapper(options['stdout']) if options.get('stderr'): self.stderr = OutputWrapper(options['stderr'], self.stderr.style_func) saved_locale = None if not self.leave_locale_alone: # Deactivate translations, because django-admin creates database # content like permissions, and those shouldn't contain any # translations. from django.utils import translation saved_locale = translation.get_language() translation.deactivate_all() try: if self.requires_system_checks and not options.get('skip_checks'): self.check() if self.requires_migrations_checks: self.check_migrations() output = self.handle(*args, **options) if output: if self.output_transaction: connection = connections[options.get('database', DEFAULT_DB_ALIAS)] output = '%s\n%s\n%s' % ( self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()), output, self.style.SQL_KEYWORD(connection.ops.end_transaction_sql()), ) self.stdout.write(output) finally: if saved_locale is not None: translation.activate(saved_locale) return output
def execute(self, *args, **options): """ Try to execute this command, performing system checks if needed (as controlled by attributes ``self.requires_system_checks`` and ``self.requires_model_validation``, except if force-skipped). """ if options.get('no_color'): self.style = no_style() self.stderr.style_func = None if options.get('stdout'): self.stdout = OutputWrapper(options['stdout']) if options.get('stderr'): self.stderr = OutputWrapper(options.get('stderr'), self.stderr.style_func) saved_locale = None if not self.leave_locale_alone: # Only mess with locales if we can assume we have a working # settings file, because django.utils.translation requires settings # (The final saying about whether the i18n machinery is active will be # found in the value of the USE_I18N setting) if not self.can_import_settings: raise CommandError("Incompatible values of 'leave_locale_alone' " "(%s) and 'can_import_settings' (%s) command " "options." % (self.leave_locale_alone, self.can_import_settings)) # Deactivate translations, because django-admin creates database # content like permissions, and those shouldn't contain any # translations. from django.utils import translation saved_locale = translation.get_language() translation.deactivate_all() try: if (self.requires_system_checks and not options.get('skip_validation') and # Remove at the end of deprecation for `skip_validation`. not options.get('skip_checks')): self.check() output = self.handle(*args, **options) if output: if self.output_transaction: # This needs to be imported here, because it relies on # settings. from django.db import connections, DEFAULT_DB_ALIAS connection = connections[options.get('database', DEFAULT_DB_ALIAS)] if connection.ops.start_transaction_sql(): self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql())) self.stdout.write(output) if self.output_transaction: self.stdout.write('\n' + self.style.SQL_KEYWORD(connection.ops.end_transaction_sql())) finally: if saved_locale is not None: translation.activate(saved_locale)
def test_localized_ref(cls): """Tests whether the :see:LocalizedRef expression properly works.""" obj = cls.TestModel1.objects.create(name='bla bla') for i in range(0, 10): cls.TestModel2.objects.create( text=LocalizedValue(dict(en='text_%d_en' % i, ro='text_%d_ro' % i, nl='text_%d_nl' % i)), other=obj ) def create_queryset(ref): return ( cls.TestModel1.objects .annotate(mytexts=ref) .values_list('mytexts', flat=True) ) # assert that it properly selects the currently active language for lang_code, _ in settings.LANGUAGES: translation.activate(lang_code) queryset = create_queryset(LocalizedRef('features__text')) for index, value in enumerate(queryset): assert translation.get_language() in value assert str(index) in value # ensure that the default language is used in case no # language is active at all translation.deactivate_all() queryset = create_queryset(LocalizedRef('features__text')) for index, value in enumerate(queryset): assert settings.LANGUAGE_CODE in value assert str(index) in value # ensures that overriding the language works properly queryset = create_queryset(LocalizedRef('features__text', 'ro')) for index, value in enumerate(queryset): assert 'ro' in value assert str(index) in value # ensures that using this in combination with ArrayAgg works properly queryset = create_queryset(ArrayAgg(LocalizedRef('features__text', 'ro'))).first() assert isinstance(queryset, list) for value in queryset: assert 'ro' in value