Ejemplo n.º 1
0
    def __init__(self, *args, **kwargs):
        # Still allow to pass the translated fields (e.g. title=...) to this function.
        translated_kwargs = {}
        current_language = None
        if kwargs:
            current_language = kwargs.pop('_current_language', None)
            for field in self._translations_model.get_translated_fields():
                try:
                    translated_kwargs[field] = kwargs.pop(field)
                except KeyError:
                    pass

        # Run original Django model __init__
        super(TranslatableModel, self).__init__(*args, **kwargs)

        self._translations_cache = {}
        self._current_language = normalize_language_code(
            current_language or get_language()
        )  # What you used to fetch the object is what you get.

        # Assign translated args manually.
        if translated_kwargs:
            translation = self._get_translated_model(auto_create=True)
            for field, value in six.iteritems(translated_kwargs):
                setattr(translation, field, value)
Ejemplo n.º 2
0
    def __init__(self, *args, **kwargs):
        # Still allow to pass the translated fields (e.g. title=...) to this function.
        translated_kwargs = {}
        current_language = None
        if kwargs:
            current_language = kwargs.pop('_current_language', None)
            for field in self._parler_meta.get_all_fields():
                try:
                    translated_kwargs[field] = kwargs.pop(field)
                except KeyError:
                    pass

        # Have the attributes available, but they can't be ready yet;
        # self._state.adding is always True at this point,
        # the QuerySet.iterator() code changes it after construction.
        self._translations_cache = None
        self._current_language = None

        # Run original Django model __init__
        super(TranslatableModelMixin, self).__init__(*args, **kwargs)

        # Assign translated args manually.
        self._translations_cache = defaultdict(dict)
        self._current_language = normalize_language_code(current_language or get_language())  # What you used to fetch the object is what you get.

        if translated_kwargs:
            self._set_translated_fields(self._current_language, **translated_kwargs)
Ejemplo n.º 3
0
    def __init__(self, *args, **kwargs):
        # Still allow to pass the translated fields (e.g. title=...) to this function.
        translated_kwargs = {}
        current_language = None
        if kwargs:
            current_language = kwargs.pop('_current_language', None)
            for field in self._parler_meta.get_all_fields():
                try:
                    translated_kwargs[field] = kwargs.pop(field)
                except KeyError:
                    pass

        # Have the attributes available, but they can't be ready yet;
        # self._state.adding is always True at this point,
        # the QuerySet.iterator() code changes it after construction.
        self._translations_cache = None
        self._current_language = None

        # Run original Django model __init__
        super(TranslatableModel, self).__init__(*args, **kwargs)

        # Assign translated args manually.
        self._translations_cache = defaultdict(dict)
        self._current_language = normalize_language_code(
            current_language or get_language()
        )  # What you used to fetch the object is what you get.

        if translated_kwargs:
            self._set_translated_fields(self._current_language,
                                        **translated_kwargs)
Ejemplo n.º 4
0
    def __init__(self, *args, **kwargs):
        # Still allow to pass the translated fields (e.g. title=...) to this function.
        translated_kwargs = {}
        current_language = None
        if kwargs:
            current_language = kwargs.pop("_current_language", None)
            for field in self._translations_model.get_translated_fields():
                try:
                    translated_kwargs[field] = kwargs.pop(field)
                except KeyError:
                    pass

        # Run original Django model __init__
        super(TranslatableModel, self).__init__(*args, **kwargs)

        self._translations_cache = {}
        self._current_language = normalize_language_code(
            current_language or get_language()
        )  # What you used to fetch the object is what you get.

        # Assign translated args manually.
        if translated_kwargs:
            translation = self._get_translated_model(auto_create=True)
            for field, value in six.iteritems(translated_kwargs):
                setattr(translation, field, value)
Ejemplo n.º 5
0
    def _language(self, request, obj=None):
        """
        Get the language parameter from the current request.
        """
        if not is_multilingual_project() or not self._has_translatable_model():
            # By default, the objects are stored in a single static language.
            # This makes the transition to multilingual easier as well.
            # The default language can operate as fallback language too.
            return appsettings.PARLER_DEFAULT_LANGUAGE_CODE
        else:
            # In multilingual mode, take the provided language of the request.
            code = request.GET.get(self.query_language_key)

            if not code:
                # Show first tab by default
                try:
                    lang_choices = appsettings.PARLER_LANGUAGES[
                        settings.SITE_ID]
                    code = lang_choices[0]['code']
                except (KeyError, IndexError):
                    # No configuration, always fallback to default language.
                    # This is essentially a non-multilingual configuration.
                    code = appsettings.PARLER_DEFAULT_LANGUAGE_CODE

            return normalize_language_code(code)
Ejemplo n.º 6
0
    def set_current_language(self, language_code, initialize=False):
        """
        Switch the currently activate language of the object.
        """
        self._current_language = normalize_language_code(language_code or get_language())

        # Ensure the translation is present for __get__ queries.
        if initialize:
            self._get_translated_model(use_fallback=False, auto_create=True)
Ejemplo n.º 7
0
    def set_current_language(self, language_code, initialize=False):
        """
        Switch the currently activate language of the object.
        """
        self._current_language = normalize_language_code(language_code or get_language())

        # Ensure the translation is present for __get__ queries.
        if initialize:
            self._get_translated_model(use_fallback=False, auto_create=True)
Ejemplo n.º 8
0
    def _language(self, request, obj=None):
        """
        Get the language parameter from the current request.
        """
        if not is_multilingual_project() or not self._has_translatable_model():
            # By default, the objects are stored in a single static language.
            # This makes the transition to multilingual easier as well.
            # The default language can operate as fallback language too.
            return appsettings.PARLER_DEFAULT_LANGUAGE_CODE
        else:
            # In multilingual mode, take the provided language of the request.
            code = request.GET.get(self.query_language_key)

            if not code:
                # Show first tab by default
                try:
                    lang_choices = appsettings.PARLER_LANGUAGES[settings.SITE_ID]
                    code = lang_choices[0]['code']
                except (KeyError, IndexError):
                    # No configuration, always fallback to default language.
                    # This is essentially a non-multilingual configuration.
                    code = appsettings.PARLER_DEFAULT_LANGUAGE_CODE

            return normalize_language_code(code)