本文整理汇总了Python中modeltranslation.utils.get_language函数的典型用法代码示例。如果您正苦于以下问题:Python get_language函数的具体用法?Python get_language怎么用?Python get_language使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_language函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: rewrite_lookup_key
def rewrite_lookup_key(model, lookup_key):
translatable_fields = get_translatable_fields_for_model(model)
if translatable_fields is not None:
pieces = lookup_key.split('__')
# If we are doing a lookup on a translatable field,
# we want to rewrite it to the actual field name
# For example, we want to rewrite "name__startswith" to "name_fr__startswith"
if pieces[0] in translatable_fields:
lookup_key = build_localized_fieldname(pieces[0], get_language())
remaining_lookup = '__'.join(pieces[1:])
if remaining_lookup:
lookup_key = '%s__%s' % (lookup_key, remaining_lookup)
pieces = lookup_key.split('__')
if len(pieces) > 1:
# Check if we are doing a lookup to a related trans model
fields_to_trans_models = get_fields_to_translatable_models(model)
for field_to_trans, transmodel in fields_to_trans_models:
if pieces[0] == field_to_trans:
sub_lookup = '__'.join(pieces[1:])
if sub_lookup:
sub_lookup = rewrite_lookup_key(transmodel, sub_lookup)
lookup_key = '%s__%s' % (pieces[0], sub_lookup)
break
return lookup_key
示例2: pre_save
def pre_save(self, model_instance, add):
val = super(TranslationField, self).pre_save(model_instance, add)
if get_language() == self.language and not add:
# Rule is: 3. Assigning a value to a translation field of the
# default language also updates the original field
model_instance.__dict__[self.translated_field.name] = val
return val
示例3: __get__
def __get__(self, instance, owner):
"""
Returns value from the translation field for the current language, or
value for some another language according to fallback languages, or the
custom fallback value, or field's default value.
"""
if instance is None:
return self
default = NONE
undefined = self.fallback_undefined
if undefined is NONE:
default = self.field.get_default()
undefined = default
langs = resolution_order(get_language(), self.fallback_languages)
for lang in langs:
loc_field_name = build_localized_fieldname(self.field.name, lang)
val = getattr(instance, loc_field_name, None)
if val is not None and val != undefined:
return val
if mt_settings.ENABLE_FALLBACKS and self.fallback_value is not NONE:
return self.fallback_value
else:
if default is NONE:
default = self.field.get_default()
return default
示例4: formfield_exclude_irrelevant
def formfield_exclude_irrelevant(db_field, **kwargs):
""" only localized fields """
from modeltranslation.translator import translator
from modeltranslation.utils import get_language
trans_opts = translator.get_options_for_model(db_field.model)
if db_field.name in trans_opts.fields:
return None
if 'field' in kwargs:
field = kwargs['field']
else:
field = db_field.formfield(**kwargs)
if hasattr(db_field, 'translated_field'):
if db_field.name.endswith('_{0}'.format(get_language())):
field.required = True
field.widget.attrs['class'] = '{0} {1}'.format(
getattr(field.widget.attrs, 'class', ''),
'language-depended'
)
field.help_text = string_concat(
field.help_text,
_(' '),
_('This field dependent on current language.')
)
else:
return None
return field
示例5: cache_name
def cache_name(self):
"""
Used in django 1.x
"""
lang = get_language()
cache = build_localized_fieldname(self.accessor, lang)
return "_%s_cache" % cache
示例6: __get__
def __get__(self, instance, owner):
"""
Returns value from the translation field for the current language, or
value for some another language according to fallback languages, or the
custom fallback value, or field's default value.
"""
if instance is None:
return self
default = NONE
undefined = self.fallback_undefined
if undefined is NONE:
default = self.field.get_default()
undefined = default
langs = resolution_order(get_language(), self.fallback_languages)
for lang in langs:
loc_field_name = build_localized_fieldname(self.field.name, lang)
val = getattr(instance, loc_field_name, None)
if self.meaningful_value(val, undefined):
return val
if mt_settings.ENABLE_FALLBACKS and self.fallback_value is not NONE:
return self.fallback_value
else:
if default is NONE:
default = self.field.get_default()
# Some fields like FileField behave strange, as their get_default() doesn't return
# instance of attr_class, but rather None or ''.
# Normally this case is handled in the descriptor, but since we have overridden it, we
# must mock it up.
if (isinstance(self.field, fields.files.FileField) and
not isinstance(default, self.field.attr_class)):
return self.field.attr_class(instance, self.field, default)
return default
示例7: _validate_slugs
def _validate_slugs(page):
"""
Determine whether the given slug is available for use on a child page of
parent_page.
"""
parent_page = page.get_parent()
if parent_page is None:
# the root page's slug can be whatever it likes...
return {}
# Save the current active language
current_language = get_language()
siblings = page.get_siblings(inclusive=False).specific()
errors = {}
for language in mt_settings.AVAILABLE_LANGUAGES:
# Temporarily activate every language because even though there might
# be no repeated value for slug_pt the fallback of an empty slug could
# already be in use
trans_real.activate(language)
siblings_slugs = [sibling.slug for sibling in siblings]
if page.specific.slug in siblings_slugs:
errors[build_localized_fieldname('slug', language)] = _("This slug is already in use")
# Re-enable the original language
trans_real.activate(current_language)
return errors
示例8: __set__
def __set__(self, instance, value):
lang = get_language()
loc_field_name = build_localized_fieldname(self.field_name, lang)
# Localized field name with '_id'
loc_attname = instance._meta.get_field(loc_field_name).get_attname()
setattr(instance, loc_attname, value)
base_attname = instance._meta.get_field(self.field_name).get_attname()
instance.__dict__[base_attname] = value
示例9: keywords_list_language
def keywords_list_language(self, lang=None):
if not lang:
lang = get_language() or DEFAULT_LANGUAGE
keywords = []
list = self.keywords_list()
for k in list:
keywords.append([k[0][lang], k[1], k[2], k[0][lang].split('>')[-1].strip()])
return keywords
示例10: __set__
def __set__(self, instance, value):
lang = get_language()
loc_field_name = build_localized_fieldname(self.name, lang)
# also update the translation field of the current language
setattr(instance, loc_field_name, value)
# update the original field via the __dict__ to prevent calling the
# descriptor
instance.__dict__[self.name] = value
示例11: __set__
def __set__(self, instance, value):
if getattr(instance, '_mt_init', False):
# When assignment takes place in model instance constructor, don't set value.
# This is essential for only/defer to work, but I think it's sensible anyway.
return
lang = get_language()
loc_field_name = build_localized_fieldname(self.field.name, lang)
# also update the translation field of the current language
setattr(instance, loc_field_name, value)
示例12: keywords_list_clean
def keywords_list_clean(self, lang=None):
if not lang:
lang = get_language() or DEFAULT_LANGUAGE
keywords = []
if self.gemetkeywords is not None and self.gemetkeywords != '':
list = json.loads(self.gemetkeywords)
for k in list:
keywords.append(k[0][lang].split('>')[-1].strip())
return keywords
示例13: save_form_data
def save_form_data(self, instance, data):
# Allow 3rd-party apps forms to be saved using only translated field name.
# When translated field (e.g. 'name') is specified and translation field (e.g. 'name_en')
# not, we assume that form was saved without knowledge of modeltranslation and we make
# things right:
# Translated field is saved first, settings respective translation field value. Then
# translation field is being saved without value - and we handle this here (only for
# active language).
if self.language == get_language() and getattr(instance, self.name) and not data:
return
super(TranslationField, self).save_form_data(instance, data)
示例14: lbyl
def lbyl(terms, lang_priority = None):
terms_dict = {}
if not lang_priority:
lang_priority = [get_language(), DEFAULT_LANGUAGE]
for i in terms:
terms_dict[i.language] = i
for lang in lang_priority:
value = terms_dict.get(lang)
if value is not None:
return value
return terms.first
示例15: _patch_prepopulated_fields
def _patch_prepopulated_fields(self):
if self.prepopulated_fields:
# TODO: Perhaps allow to configure which language the slug should be based on?
lang = get_language()
prepopulated_fields_new = dict(self.prepopulated_fields)
translation_fields = []
for k, v in self.prepopulated_fields.items():
for i in v:
if i in self.trans_opts.fields.keys():
translation_fields.append(build_localized_fieldname(i, lang))
prepopulated_fields_new[k] = tuple(translation_fields)
self.prepopulated_fields = prepopulated_fields_new