我们从Python开源项目中,提取了以下39个代码示例,用于说明如何使用django.utils.translation.get_language_info()。
def do_get_language_info(parser, token): """ This will store the language information dictionary for the given language code in a context variable. Usage:: {% get_language_info for LANGUAGE_CODE as l %} {{ l.code }} {{ l.name }} {{ l.name_translated }} {{ l.name_local }} {{ l.bidi|yesno:"bi-directional,uni-directional" }} """ args = token.split_contents() if len(args) != 5 or args[1] != 'for' or args[3] != 'as': raise TemplateSyntaxError("'%s' requires 'for string as variable' (got %r)" % (args[0], args[1:])) return GetLanguageInfoNode(parser.compile_filter(args[2]), args[4])
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 do_get_language_info(parser, token): """ This will store the language information dictionary for the given language code in a context variable. Usage:: {% get_language_info for LANGUAGE_CODE as l %} {{ l.code }} {{ l.name }} {{ l.name_local }} {{ l.bidi|yesno:"bi-directional,uni-directional" }} """ args = token.split_contents() if len(args) != 5 or args[1] != 'for' or args[3] != 'as': raise TemplateSyntaxError("'%s' requires 'for string as variable' (got %r)" % (args[0], args[1:])) return GetLanguageInfoNode(parser.compile_filter(args[2]), args[4])
def render(self, context): lang_code = self.lang_code.resolve(context) context[self.variable] = translation.get_language_info(lang_code) return ''
def get_language_info(self, language): # ``language`` is either a language code string or a sequence # with the language code as its first item if len(language[0]) > 1: return translation.get_language_info(language[0]) else: return translation.get_language_info(str(language))
def render(self, context): langs = self.languages.resolve(context) context[self.variable] = [self.get_language_info(lang) for lang in langs] return ''
def language_name(lang_code): return translation.get_language_info(lang_code)['name']
def language_name_local(lang_code): return translation.get_language_info(lang_code)['name_local']
def language_bidi(lang_code): return translation.get_language_info(lang_code)['bidi']
def sorted_chunks(self): bidi = get_language_info(get_language())['bidi'] return sorted(filter(partial(filter_by_bidi, bidi), self.bundle), key=lambda x: x['name'].split('.')[-1])
def details_display(self): result = super().details_display() for lang, title in sorted(self.titles.items(), key=lambda item: item[0] != get_language()): language = _('Title ({lang})').format(lang=get_language_info(lang)['name_translated']) result['display'].append((language, title)) return result
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) new_fields = OrderedDict() self.i18n_fields = [] for name, form_field in self.fields.items(): model_field = self.instance._meta.get_field(name) if not isinstance(model_field, I18nField): new_fields[name] = form_field continue values = OrderedDict((lang_code, '') for lang_code, language in settings.LANGUAGES) if self.instance is not None and self.instance.pk: values.update(getattr(self.instance, model_field.attname)) has_values = False for language in values.keys(): sub_field_name = '%s__%s' % (name, language) new_value = self.data.get(sub_field_name) if new_value is not None: has_values = True values[language] = new_value language_info = get_language_info(language) field_title = format_lazy(_('{field_name} ({lang})'), field_name=capfirst(model_field.verbose_name), lang=language_info['name_translated']) new_fields[sub_field_name] = CharField(label=field_title, required=False, initial=values[language].strip(), max_length=model_field.i18n_max_length) if has_values: self.i18n_fields.append((model_field, values)) self.fields = new_fields
def __init__(self, *args, **kwargs): super(UserSettingsForm, self).__init__(*args, **kwargs) # Languages def get_language_display_name(code, desc): try: desc = translation.get_language_info(code)['name_local'] desc = string.capwords(desc) except KeyError: # If a language is not defined in django.conf.locale.LANG_INFO # get_language_info raises KeyError pass return "%s (%s)" % (desc, code) languages = [(k, get_language_display_name(k, v)) for k, v in settings.LANGUAGES] self.fields['language'].choices = languages # Timezones timezones = [] language = translation.get_language() current_locale = translation.to_locale(language) babel_locale = babel.Locale.parse(current_locale) for tz, offset in self._sorted_zones(): try: utc_offset = _("UTC %(hour)s:%(min)s") % {"hour": offset[:3], "min": offset[3:]} except Exception: utc_offset = "" if tz == "UTC": tz_name = _("UTC") elif tz == "GMT": tz_name = _("GMT") else: tz_label = babel.dates.get_timezone_location( tz, locale=babel_locale) # Translators: UTC offset and timezone label tz_name = _("%(offset)s: %(label)s") % {"offset": utc_offset, "label": tz_label} timezones.append((tz, tz_name)) self.fields['timezone'].choices = timezones # When we define a help_text using any variable together with # form field, traslation does not work well. # To avoid this, we define here. (#1563021) self.fields['pagesize'].help_text = ( _("Number of items to show per page (applies to the pages " "that have API supported pagination, Max Value: %s)") % self.max_value)