def _populate_template_fields(self): template_use = getattr(self.script_item, "template_use", TemplateUse.NONE) if template_use == TemplateUse.MULTILINGUAL: self.template_languages = [] # TODO: Should we get this list from somewhere else? for language_code, language_name in settings.LANGUAGES: self.template_languages.append( (language_code, get_language_name(language_code))) elif template_use == TemplateUse.UNILINGUAL: self.template_languages = [ UNILINGUAL_TEMPLATE_LANGUAGE, _(u"Template") ] else: # Nothing to do return ordered_fields = sorted(self.script_item.template_fields.items(), key=lambda p: p[1].creation_counter) for lang_code, lang_name in self.template_languages: for t_field_name, base_field in ordered_fields: field = copy.deepcopy(base_field) field.label = "%s (%s)" % (field.label, lang_name) field.required = False field_name = "t_%s_%s" % (lang_code, t_field_name) self.fields[field_name] = field self.template_field_info[lang_code][t_field_name] = field_name
def test_get_language_name(): with override("fi"): assert get_language_name("fi") == "suomi" assert get_language_name("zh") == "kiina" assert get_language_name("zh_Hans") == get_language_name("zh-Hans") == "yksinkertaistettu kiina" assert "yksinkertaistettu kiina" with override("sv"): assert get_language_name("fi") == "finska" assert get_language_name("zh") == "kinesiska" assert get_language_name("zh_Hans") == get_language_name("zh-Hans") == "förenklad kinesiska"
def get_editor_args(self): return { "languages": [ { "code": code, "name": get_language_name(code) } for code in to_language_codes( settings.LANGUAGES, getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE", "en")) ], "variables": self.get_variable_data() }
def clean(self): """ If title or content has been given on any language we must enforce that the other fields are also required in that language. This is done the way it is because url is not required by default in model level. """ data = super(PageForm, self).clean() something_filled = False urls = [] for language in self.languages: field_names = self.trans_name_map[language] if not any( data.get(field_name) for field_name in field_names.values()): # Let's not complain about this language continue something_filled = True for field_name in field_names.values(): value = data.get(field_name) if value: # No need to bother complaining about this field if field_name.startswith( "url__"): # url needs a second look though if not self.is_url_valid(language, field_name, value): self.add_error( field_name, ValidationError(_("URL already exists."), code="invalid_url")) if value in urls: self.add_error( field_name, ValidationError(_("URL must be unique"), code="invalid_unique_url")) urls.append(value) continue self.add_error( field_name, _("%(label)s is required when any %(language)s field is filled." ) % { "label": self.fields[field_name].label, "language": get_language_name(language) }) if not something_filled: titlefield = "title__%s" % self.default_language self.add_error(titlefield, _("Please fill at least one language fully.")) return data
def create_multilanguage_page(languages=("fi", "en", "ja", "de"), **kwargs): """ :rtype: wshop.simple_cms.models.Page """ page = initialize_page(**kwargs) base_url = page.url base_title = page.title for lang in languages: page.set_current_language(lang) page.title = "%s, %s" % (base_title, get_language_name(lang)) page.url = "%s-%s" % (base_url, lang) page.content = "Super interesting content in %s" % get_language_name( lang) page.full_clean() page.save() assert page._parler_meta.root_model.objects.filter( master_id=page.pk, language_code=lang, url="%s-%s" % (base_url, lang)).exists() assert page.has_translation(lang) return page
def _process_multilang_attr(self, attribute, pa): languages = tuple(self.languages) if pa: # Ensure the fields for languages in the database but not currently otherwise available are visible extant_languages = pa.get_available_languages() languages += tuple(lang for lang in extant_languages if lang not in languages) else: extant_languages = set() for lang in languages: field_name = "%s__%s" % (attribute.identifier, lang) self.fields[field_name] = field = attribute.formfield() field.label = "%s [%s]" % (field.label, get_language_name(lang)) self.trans_name_map[lang][field_name] = field_name self.translated_field_names.append(field_name) if pa and lang in extant_languages: self.initial[field_name] = getattr(pa.get_translation(lang), "translated_string_value", None) self._field_languages[attribute.identifier][lang] = field_name
def __init__(self, **kwargs): self.default_language = kwargs.pop( "default_language", getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE")) self.languages = to_language_codes(kwargs.pop("languages", ()), self.default_language) self.language_names = dict( (lang, get_language_name(lang)) for lang in self.languages) self.product = kwargs.pop("product") self.attributes = self.product.get_available_attribute_queryset() self.trans_name_map = defaultdict(dict) self.translated_field_names = [] super(ProductAttributesForm, self).__init__(**kwargs) if self.product.pk: self.applied_attrs = dict( (pa.attribute_id, pa) for pa in self.product.attributes.all()) else: self.applied_attrs = {} self._field_languages = {} self._build_fields()
def __init__(self, **kwargs): self.default_language = kwargs.pop( "default_language", getattr(self, 'language', getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE"))) self.languages = to_language_codes(kwargs.pop("languages", ()), self.default_language) self.required_languages = kwargs.pop("required_languages", [self.default_language]) opts = self._meta translations_model = self._get_translation_model() object_data = {} # We're not mutating the existing fields, so the shallow copy should be okay self.base_fields = self.base_fields.copy() self.translation_fields = [ f for (f, _) in translations_model._meta.get_fields_with_model() if f.name not in ('language_code', 'master', 'id') and f.name in self.base_fields ] self.trans_field_map = defaultdict(dict) self.trans_name_map = defaultdict(dict) self.translated_field_names = [] self.required_translated_field_names = [] self.non_default_languages = sorted( set(self.languages) - set([self.default_language])) self.language_names = dict( (lang, get_language_name(lang)) for lang in self.languages) for f in self.translation_fields: base = self.base_fields.pop(f.name, None) if not base: continue for lang in self.languages: language_field = copy.deepcopy(base) language_field_name = "%s__%s" % (f.name, lang) if language_field.required: self.required_translated_field_names.append( language_field_name) language_field.required = language_field.required and ( lang in self.required_languages) language_field.label = self._get_label(f.name, language_field, lang) self.base_fields[language_field_name] = language_field self.trans_field_map[lang][language_field_name] = f self.trans_name_map[lang][f.name] = language_field_name self.translated_field_names.append(language_field_name) instance = kwargs.get("instance") initial = kwargs.get("initial") if instance is not None: assert isinstance(instance, self._meta.model) current_translations = dict( (trans.language_code, trans) for trans in translations_model.objects.filter( master=instance)) object_data = {} for lang, trans in six.iteritems(current_translations): model_dict = model_to_dict(trans, opts.fields, opts.exclude) object_data.update(("%s__%s" % (fn, lang), f) for (fn, f) in six.iteritems(model_dict)) if initial is not None: object_data.update(initial) kwargs["initial"] = object_data super(MultiLanguageModelForm, self).__init__(**kwargs)