Ejemplo n.º 1
0
def test_underscore_localization():
    i18n.localize("de")
    assert i18n.get_current_language() == "de"
    assert i18n._("Age") == "Alter"
    i18n.unlocalize()
    assert i18n._("Age") == "Age"
    assert i18n.get_current_language() is None
Ejemplo n.º 2
0
    def _do_build_index(
        self,
        match_item_generators: Iterable[ABCMatchItemGenerator],
    ) -> None:
        current_language = get_current_language()

        with self._redis_client.pipeline() as pipeline:
            self._add_language_independent_item_generators_to_redis(
                iter(  # to make pylint happy
                    filter(
                        lambda match_item_gen: not match_item_gen.
                        is_localization_dependent,
                        match_item_generators,
                    )),
                pipeline,
            )
            self._add_language_dependent_item_generators_to_redis(
                list(
                    filter(
                        lambda match_item_gen: match_item_gen.
                        is_localization_dependent,
                        match_item_generators,
                    )),
                pipeline,
            )
            pipeline.execute()

        localize(current_language)
Ejemplo n.º 3
0
    def _action(self) -> None:
        assert user.id is not None

        users = userdb.load_users(lock=True)
        user_spec = users[user.id]

        language = request.get_ascii_input_mandatory("language", "")
        # Set the users language if requested to set it explicitly
        if language != "_default_":
            user_spec["language"] = language
            user.language = language
            set_language_cookie(request, response, language)

        else:
            if "language" in user_spec:
                del user_spec["language"]
            user.reset_language()

        # load the new language
        localize(user.language)

        if user.may("general.edit_notifications") and user_spec.get(
                "notifications_enabled"):
            value = forms.get_input(get_vs_flexible_notifications(),
                                    "notification_method")
            user_spec["notification_method"] = value

        # Custom attributes
        if user.may("general.edit_user_attributes"):
            for name, attr in userdb.get_user_attributes():
                if not attr.user_editable():
                    continue

                perm_name = attr.permission()
                if perm_name and not user.may(perm_name):
                    continue

                vs = attr.valuespec()
                value = vs.from_html_vars("ua_" + name)
                vs.validate_value(value, "ua_" + name)
                # TODO: Dynamically fiddling around with a TypedDict is a bit questionable
                user_spec[name] = value  # type: ignore[literal-required]

        userdb.save_users(users)

        flash(_("Successfully updated user profile."))

        # In distributed setups with remote sites where the user can login, start the
        # user profile replication now which will redirect the user to the destination
        # page after completion. Otherwise directly open up the destination page.
        if user.authorized_login_sites():
            back_url = "user_profile_replicate.py?back=user_profile.py"
        else:
            back_url = "user_profile.py"

        # Ensure theme changes are applied without additional user interaction
        html.reload_whole_page(back_url)
        html.footer()

        raise FinalizeRequest(code=200)
Ejemplo n.º 4
0
def test_lazy_localization():
    lazy_str = i18n._l("Age")

    assert lazy_str == "Age"

    i18n.localize("de")
    assert lazy_str == "Alter"

    i18n.unlocalize()
    assert lazy_str == "Age"
Ejemplo n.º 5
0
    def _build_index(
        self,
        names_and_generators: Iterable[Tuple[str, ABCMatchItemGenerator]],
    ) -> Index:
        index = Index()
        localization_dependent_generators = []

        for name, match_item_generator in names_and_generators:
            if match_item_generator.is_localization_dependent:
                localization_dependent_generators.append(
                    (name, match_item_generator))
                continue
            index.localization_independent[
                name] = self._evaluate_match_item_generator(
                    match_item_generator)

        index.localization_dependent = {
            language_name: {
                name: self._evaluate_match_item_generator(match_item_generator)
                for name, match_item_generator in
                localization_dependent_generators
            }
            for language, language_name in self._all_languages.items()
            for _ in [localize(language)]  # type: ignore[func-returns-value]
        }

        return index
Ejemplo n.º 6
0
 def _add_language_dependent_item_generators_to_redis(
     self,
     match_item_generators: Collection[ABCMatchItemGenerator],
     redis_pipeline: redis.client.Pipeline,
 ) -> None:
     key_categories_ld = self.key_categories(self.PREFIX_LOCALIZATION_DEPENDENT)
     for language, language_name in self._all_languages.items():
         localize(language)
         for match_item_generator in match_item_generators:
             self._add_match_item_generator_to_redis(
                 match_item_generator,
                 redis_pipeline,
                 key_categories_ld,
                 self.add_to_prefix(
                     self.PREFIX_LOCALIZATION_DEPENDENT,
                     language_name,
                 ),
             )
Ejemplo n.º 7
0
    def _build_index(
        self,
        names_and_generators: Iterable[ABCMatchItemGenerator],
    ) -> None:
        localization_dependent_generators = []

        with self._redis_client.pipeline() as pipeline:
            key_categories_li = self.key_categories(
                self.PREFIX_LOCALIZATION_INDEPENDENT)
            for match_item_generator in names_and_generators:
                if match_item_generator.is_localization_dependent:
                    localization_dependent_generators.append(
                        match_item_generator)
                    continue
                pipeline.sadd(
                    key_categories_li,
                    match_item_generator.name,
                )
                self._add_match_items_to_redis(
                    match_item_generator,
                    pipeline,
                    self.PREFIX_LOCALIZATION_INDEPENDENT,
                )

            key_categories_ld = self.key_categories(
                self.PREFIX_LOCALIZATION_DEPENDENT)
            for language, language_name in self._all_languages.items():
                localize(language)
                prefix_ld = self.add_to_prefix(
                    self.PREFIX_LOCALIZATION_DEPENDENT,
                    language_name,
                )
                for match_item_generator in localization_dependent_generators:
                    pipeline.sadd(
                        key_categories_ld,
                        match_item_generator.name,
                    )
                    self._add_match_items_to_redis(
                        match_item_generator,
                        pipeline,
                        prefix_ld,
                    )

            pipeline.execute()