Admin Translation uses page language instead of backend locale

When I use

echo $this->translate('Textabschnitt', [], 'admin');

shouldn’t pimcore use the current backend language to translate the text “Textabschnitt”?

Currently, pimcore always uses the current page language (“Navigation & Properties” / “Language”). However, I like to use the admin translation, because I want to the user to see the text in the language he choose in his profile settings.

Is there a way to do this?

why are you using Admin Translations for frontend pages anyway?

I use them to translate options of a select editable, as it is encouraged by the documentation: https://pimcore.com/docs/6.x/Development_Documentation/Multi_Language_i18n/Admin_Translations.html

However, in that example pimcore does not use the backend language but the page language instead.

That does make sense.

Sorry, I don’t think there is a solution, how do you wanna get a backend user language when you’re not in the context of the backend…

If you are doing some sort of Iframe thing in the backend, you have access to the pimcore user, if not, you don’t

Thank you for your reply.

One solution could be to pass the backend language as the fourth parameter:

echo $this->translate('Textabschnitt', [], 'admin', $currentBackendLanguage);

However, I did not find a way to get the current backend language.

there is a fourth parameter for the language, so that works.

Did you find a way to get to the current backend language?

@tgross-m-s I found a solution inside Pimcores Translation/Admin class.

I now use this approach to retrieve the current User (if any):

public function getCurrentUser() {
    if (!$this->currentUser) {

        // determine currently logged in user
        if ($user = \Pimcore\Tool\Admin::getCurrentUser()) {
            $this->currentUser = $user;
        } elseif ($user = \Pimcore\Tool\Authentication::authenticateSession()) {
            $this->currentUser = $user;
        } else {
            $this->currentUser = false;
        }
    }

    return $this->currentUser;
}

Cheers.

Thank you for your solution, I am going to try this.

I still think that

echo $this->translate('Textabschnitt', [], 'admin');

always using the document-language is a bug, because it is explicitly requesting an admin-translation.

I thought so myself, but I don’t know if its really a bug or if its intended to work with namespaced shared translations.

For example, maybe this:

echo $this->translate('Textabschnitt', [], 'admin');

resolves to the key admin.Textabschnitt inside the shared translations? I didn’t test it, but yeah…:man_shrugging: