我们从Python开源项目中,提取了以下47个代码示例,用于说明如何使用django.utils.translation.get_language_bidi()。
def kolibri_language_globals(context): lang_dir = "rtl" if get_language_bidi() else "ltr" js = """ <script> var languageCode = '{lang_code}'; var languageDir = '{lang_dir}'; var languages = JSON.parse('{languages}'); </script> """.format( lang_code=get_language(), lang_dir=lang_dir, languages=json.dumps({code: { # Format to match the schema of the content Language model 'id': code, 'lang_name': name, 'lang_direction': get_language_info(code)['bidi'] } for code, name in settings.LANGUAGES}), ) return mark_safe(js)
def field_choices(self, field, request, model_admin): mptt_level_indent = getattr(model_admin, 'mptt_level_indent', self.mptt_level_indent) language_bidi = get_language_bidi() initial_choices = field.get_choices(include_blank=False) pks = [pk for pk, val in initial_choices] models = field.related_model._default_manager.filter(pk__in=pks) levels_dict = {model.pk: getattr(model, model._mptt_meta.level_attr) for model in models} choices = [] for pk, val in initial_choices: padding_style = ' style="padding-%s:%spx"' % ( 'right' if language_bidi else 'left', mptt_level_indent * levels_dict[pk]) choices.append((pk, val, mark_safe(padding_style))) return choices # Ripped from contrib.admin.filters,RelatedFieldListFilter Django 1.8 to # yield padding_style
def render(self, name, value, attrs=None): # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if isinstance(value, six.string_types): value = self.deserialize(value) if value: # Check that srid of value and map match if value.srid != self.map_srid: try: ogr = value.ogr ogr.transform(self.map_srid) value = ogr except gdal.GDALException as err: logger.error( "Error transforming geometry from srid '%s' to srid '%s' (%s)" % ( value.srid, self.map_srid, err) ) context = self.build_attrs( attrs, name=name, module='geodjango_%s' % name.replace('-', '_'), # JS-safe serialized=self.serialize(value), geom_type=gdal.OGRGeomType(self.attrs['geom_type']), STATIC_URL=settings.STATIC_URL, LANGUAGE_BIDI=translation.get_language_bidi(), ) return loader.render_to_string(self.template_name, context)
def i18n(request): from django.utils import translation context_extras = {} context_extras['LANGUAGES'] = settings.LANGUAGES context_extras['LANGUAGE_CODE'] = translation.get_language() context_extras['LANGUAGE_BIDI'] = translation.get_language_bidi() return context_extras
def render(self, context): context[self.variable] = translation.get_language_bidi() return ''
def render(self, name, value, attrs=None): # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if isinstance(value, six.string_types): value = self.deserialize(value) if value: # Check that srid of value and map match if value.srid != self.map_srid: try: ogr = value.ogr ogr.transform(self.map_srid) value = ogr except gdal.GDALException as err: logger.error( "Error transforming geometry from srid '%s' to srid '%s' (%s)", value.srid, self.map_srid, err ) context = self.build_attrs( attrs, name=name, module='geodjango_%s' % name.replace('-', '_'), # JS-safe serialized=self.serialize(value), geom_type=gdal.OGRGeomType(self.attrs['geom_type']), STATIC_URL=settings.STATIC_URL, LANGUAGE_BIDI=translation.get_language_bidi(), ) return loader.render_to_string(self.template_name, context)
def get_language_bidi(): """Override for Django's get_language_bidi that's aware of more RTL languages. """ return gettext.language_dir(translation.get_language()) == 'rtl'
def hijack_translation(): """Sabotage Django's fascist linguistical regime.""" # Override functions that check if language is known to Django translation.check_for_language = lambda lang_code: True trans_real.check_for_language = lambda lang_code: True translation.get_language_from_request = get_language_from_request # Override django's inadequate bidi detection translation.get_language_bidi = get_language_bidi # We hijack gettext functions to install the safe variable formatting # override override_gettext(gettext)
def get_context(self, name, value, attrs): context = super(BaseGeometryWidget, self).get_context(name, value, attrs) # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if value and isinstance(value, six.string_types): value = self.deserialize(value) if value: # Check that srid of value and map match if value.srid != self.map_srid: try: ogr = value.ogr ogr.transform(self.map_srid) value = ogr except gdal.GDALException as err: logger.error( "Error transforming geometry from srid '%s' to srid '%s' (%s)", value.srid, self.map_srid, err ) if attrs is None: attrs = {} build_attrs_kwargs = { 'name': name, 'module': 'geodjango_%s' % name.replace('-', '_'), # JS-safe 'serialized': self.serialize(value), 'geom_type': gdal.OGRGeomType(self.attrs['geom_type']), 'STATIC_URL': settings.STATIC_URL, 'LANGUAGE_BIDI': translation.get_language_bidi(), } build_attrs_kwargs.update(attrs) context.update(self.build_attrs(self.attrs, build_attrs_kwargs)) return context
def get_context_data(self, *args, **kwargs): context = super().get_context_data(*args, **kwargs) context['LANGUAGE_BIDI'] = translation.get_language_bidi() language_form_kwargs = self.get_language_form_kwargs() if self.translations_enabled: context['language_switcher'] = { 'show': True, 'form': self.language_form_class(**language_form_kwargs), } return context
def get_template_names(self): if translation.get_language_bidi(): return [self.template_name_bidi] return super().get_template_names()
def render(self, name, value, attrs=None): # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if value and isinstance(value, six.string_types): value = self.deserialize(value) if value: # Check that srid of value and map match if value.srid != self.map_srid: try: ogr = value.ogr ogr.transform(self.map_srid) value = ogr except gdal.GDALException as err: logger.error( "Error transforming geometry from srid '%s' to srid '%s' (%s)", value.srid, self.map_srid, err ) context = self.build_attrs( attrs, name=name, module='geodjango_%s' % name.replace('-', '_'), # JS-safe serialized=self.serialize(value), geom_type=gdal.OGRGeomType(self.attrs['geom_type']), STATIC_URL=settings.STATIC_URL, LANGUAGE_BIDI=translation.get_language_bidi(), ) return loader.render_to_string(self.template_name, context)
def i18n(request): from django.utils import translation return { 'LANGUAGES': settings.LANGUAGES, 'LANGUAGE_CODE': translation.get_language(), 'LANGUAGE_BIDI': translation.get_language_bidi(), }
def i18n(request): return { 'LANGUAGES': django_settings.LANGUAGES, 'LANG': translation.get_language(), 'DIR': 'rtl' if translation.get_language_bidi() else 'ltr', }
def render(self, name, value, attrs=None): # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if isinstance(value, six.string_types): value = self.deserialize(value) if value: # Check that srid of value and map match if value.srid != self.map_srid: try: ogr = value.ogr ogr.transform(self.map_srid) value = ogr except gdal.OGRException as err: logger.error( "Error transforming geometry from srid '%s' to srid '%s' (%s)" % ( value.srid, self.map_srid, err) ) context = self.build_attrs(attrs, name=name, module='geodjango_%s' % name.replace('-','_'), # JS-safe serialized=self.serialize(value), geom_type=gdal.OGRGeomType(self.attrs['geom_type']), STATIC_URL=settings.STATIC_URL, LANGUAGE_BIDI=translation.get_language_bidi(), ) return loader.render_to_string(self.template_name, context)