Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classes common::Message and common::EmptyMessage #2032

Merged
merged 3 commits into from
Feb 5, 2025

Conversation

jrohel
Copy link
Contributor

@jrohel jrohel commented Feb 4, 2025

common::Message: A base abstract class for passing a message whose formatting, including localization (translation, argument format) is done at the destination.

common::EmptyMessage: class for passing an empty message

Closes: #2034
Requires: #2029
Requires: #2031

@jan-kolarik
Copy link
Member

Hmm, sorry, but I am bit confused about the usage of the Message object. Could you provide some simple example of how would one use it e.g. for implementing some new callback method with passing this object? I am specifically interested how dnf5 app would prints the message.

@jrohel
Copy link
Contributor Author

jrohel commented Feb 4, 2025

In plugin:

class InfoMessage : public libdnf5::Message {
public:
    InfoMessage(arg_type arg) : arg(arg) {}

    std::string format(bool translate, const libdnf5::utils::Locale * locale) const override {
        return libdnf5::utils::format(locale, translate, M_("Some text with args {} "), arg);
    }

private:
  arg_type arg;
};

And pass the object to callback.

In app:

bool repokey_remove(const libdnf5::rpm::KeyInfo & key_info,  const libdnf5::Message & info) override {
    std::string info_message = info.format(true, nullptr);
}

@jan-kolarik
Copy link
Member

In plugin:

class InfoMessage : public libdnf5::Message {
public:
    InfoMessage(arg_type arg) : arg(arg) {}

    std::string format(bool translate, const libdnf5::utils::Locale * locale) const override {
        return libdnf5::utils::format(locale, translate, M_("Some text with args {} "), arg);
    }

private:
  arg_type arg;
};

And pass the object to callback.

In app:

bool repokey_remove(const libdnf5::rpm::KeyInfo & key_info,  const libdnf5::Message & info) override {
    std::string info_message = info.format(true, nullptr);
}

All right, thanks for the example. Why is the translate argument, which determines whether to translate, handled on the app side? It seems to require the app to understand the message content and decide whether to translate it. I understand that translation itself happens on the app side, not the client, but shouldn’t the decision to translate be made by the message author, who knows its semantics? For instance, error codes or machine-generated messages might not need translation.

To put it differently, if the message author already determines which parts are translatable, what scenario would require the app to override that decision and not translate the message?

@jrohel
Copy link
Contributor Author

jrohel commented Feb 5, 2025

If it makes sense to translate the passed text, the message author implements Message::format to return the translated text if translate == true.

The application decides whether the translation is used and in what language. Why? Well, because the application doesn't have to be just dnf5, but any application using libdnf5. This arbitrary application might want to output the received information translated in the user's preferred language to the terminal. But it may want to write it untranslated to the log. In general, we don't know where and in what language the unknown application will want to write the received message.

…nation

A base abstract class for passing a message whose formatting, including
localization (translation, argument format) is done at the destination.

Usage: The user creates a child of this class and implements
the `format` method.
@jan-kolarik jan-kolarik added this pull request to the merge queue Feb 5, 2025
Merged via the queue into rpm-software-management:main with commit ce3c006 Feb 5, 2025
3 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Passing messages formatted at the destination
2 participants