Admin switch language

Open to all! Ask other shopowners for help.
User avatar
bonbec
Contributor
Posts: 210
Joined: Mon Oct 26, 2020 12:23 pm
Phoenix Version: V1.1.0.8
Has thanked: 57 times
Been thanked: 44 times

Admin switch language

Post by bonbec »

Hello everyone,

I need to switch between French and English—but only within the admin area.
I’ve implemented a solution that works well :
EN_FR.jpg
But there is one thing that really bothers me: I had to add code to the admin's `application_top.php` file to ensure the selected language is preserved when changing pages.

Here is my code:
1) catalog/includes/hooks/admin/siteWide/admin_language_switcher.php

Code: Select all

<?php
class hook_admin_siteWide_admin_language_switcher {

    public function listen_injectBodyEnd() {
        global $language;

        // 1. Détecter proprement la langue en cours
        $is_fr = (strtolower($language) == 'french');
        
        $current_page = basename($_SERVER['PHP_SELF']);
        $query_params = $_GET;
        
        // Liens propres renvoyant vers le sélecteur d'application_top
        $query_params['set_adm_lang'] = 'fr';
        $link_fr = $current_page . '?' . http_build_query($query_params);
        
        $query_params['set_adm_lang'] = 'en';
        $link_en = $current_page . '?' . http_build_query($query_params);

        // Styles de surbrillance Bootstrap 5
        $active_fr = $is_fr ? 'fw-bold text-decoration-underline text-white' : 'text-white-50';
        $active_en = !$is_fr ? 'fw-bold text-decoration-underline text-white' : 'text-white-50';

        // 2. Injection graphique en fin de page dans la navbar native
        $html = '
        <script>
        (function() {
            var targetNav = document.querySelector(".navbar-nav.ms-auto");
            if (targetNav) {
                var langLi = document.createElement("li");
                langLi.className = "nav-item d-flex align-items-center px-3 small";
                langLi.innerHTML = \'<a href="' . $link_fr . '" class="nav-link p-0 ' . $active_fr . '">FR</a><span class="text-white-50 mx-1">|</span><a href="' . $link_en . '" class="nav-link p-0 ' . $active_en . '">EN</a>\';
                
                targetNav.insertBefore(langLi, targetNav.firstChild);
            }
        })();
        </script>
        ';

        return $html;
    }
}

2) in admin/includes/application_top.php

Find:

Code: Select all

if (!isset($_SESSION['language']) || isset($_GET['language'])) {
$lng = language::build(); 
}
Add right below:

Code: Select all

// --- LANGUAGE SELECTOR CONFIGURATION (via HOOK) ---
if (isset($_GET['set_adm_lang']) && in_array($_GET['set_adm_lang'], ['fr', 'en'])) {
$_SESSION['language'] = ($_GET['set_adm_lang'] == 'fr') ? 'french' : 'english'; 
$_SESSION['languages_id'] = ($_GET['set_adm_lang'] == 'fr') ? 2 : 1; 

if (isset($lng) && is_object($lng)) {
$lng->language = $_SESSION['language']; 
$lng->languages_id = $_SESSION['languages_id']; 
}

unset($_GET['set_adm_lang']); 
header('Location: ' . basename($_SERVER['PHP_SELF']) . (!empty($_GET) ? '?' . http_build_query($_GET) : '')); 
exit;
}
// --- END CONFIGURATION ---
Is there a way to do what I want without modifying admin/application_top.php?
Thank you in advance for your help.
You do not have the required permissions to view the files attached to this post.



Join The Code Co-op to get access to your library in the Code Co-op Forum
raiwa
Certified Developer
Posts: 1666
Joined: Sat Dec 21, 2019 8:08 am
Phoenix Version: 1.1.0.6
Has thanked: 72 times
Been thanked: 156 times

Re: Admin switch language

Post by raiwa »

Hi Fred,
Not sure if you need this for to send order update mails to customers in their language.
If its the case there is the localize customers addon:
app.php/addons/paid_addon/localize_customer

Which will do it without switching the admin language.
Public Phoenix Change Log Cheat Set on Google Sheets
https://docs.google.com/spreadsheets/d/ ... sp=sharing

User avatar
burt
Core Team
Posts: 4739
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 279 times
Been thanked: 467 times

Re: Admin switch language

Post by burt »

Is there not already a langue switcher in the admin area?
I thought there was...but may very well be misremembering..

If there is no langue switcher, that needs solving as a priority.
Something like your screenshot showing FR | EN | DE links etc
I am not here to build for you.
I am here to build with you. Let's help each other.

User avatar
bonbec
Contributor
Posts: 210
Joined: Mon Oct 26, 2020 12:23 pm
Phoenix Version: V1.1.0.8
Has thanked: 57 times
Been thanked: 44 times

Re: Admin switch language

Post by bonbec »

Hi Rainer,

Thanks for pointing out this add-on; I wasn't aware of it. It will likely come in handy.

Actually, I set up this system because I’m more comfortable managing things in my native language. However, when I need to install add-ons, the instructions are always in English, so it’s easier to switch the admin interface to English to ensure I don't make any mistakes.

User avatar
bonbec
Contributor
Posts: 210
Joined: Mon Oct 26, 2020 12:23 pm
Phoenix Version: V1.1.0.8
Has thanked: 57 times
Been thanked: 44 times

Re: Admin switch language

Post by bonbec »

burt wrote: ↑Tue Sep 29, 2026 10:48 am Is there not already a langue switcher in the admin area?
I thought there was...but may very well be misremembering..
Hi burt,
There is an option for a language switcher for the storefront, but not specifically for the administration language. Or perhaps I just didn't look in the right place...
My system changes the language only in the administration area without affecting the storefront language.

User avatar
burt
Core Team
Posts: 4739
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 279 times
Been thanked: 467 times

Re: Admin switch language

Post by burt »

bonbec wrote: ↑Tue Sep 29, 2026 10:54 am
burt wrote: ↑Tue Sep 29, 2026 10:48 am Is there not already a langue switcher in the admin area?
I thought there was...but may very well be misremembering..
Hi burt,
There is an option for a language switcher for the storefront, but not specifically for the administration language. Or perhaps I just didn't look in the right place...
I'll look into that today and post back here if I find it (or not)..
I don't work in other languages very much so I definitely may be forgetting
I am not here to build for you.
I am here to build with you. Let's help each other.

User avatar
bonbec
Contributor
Posts: 210
Joined: Mon Oct 26, 2020 12:23 pm
Phoenix Version: V1.1.0.8
Has thanked: 57 times
Been thanked: 44 times

Re: Admin switch language

Post by bonbec »

Thanks. Yes I know.
It is primarily the modification to admin/application_top that I would like to avoid. That modification would inevitably be overwritten when updating to a new version of PhoenixCart.
The native code block

Code: Select all

if (!isset($_SESSION['language']) || isset($_GET['language'])) {
$lng = language::build(); 
}
consistently prevents any work involving languages ​​within the hook. That is why I was forced to add code afterwards to bypass that restriction.

User avatar
burt
Core Team
Posts: 4739
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 279 times
Been thanked: 467 times

Re: Admin switch language

Post by burt »

admin/login.php has this;

langue.png

admin/index.php (obviously version dependant, so it might be different - the shots are from a v1.1.0.7);

langue2.png

Clicking this button make a language dropdown appear, allowing you to choose.

--

So, it's sort of half there, as I think (maybe) what you want is something "more";

a/ a chooser EN | FR | DE etc in the horizontal header
b/ click it to change language and be redirected back to the page you were on?

EG, you on admin/specials.php in french.
Click on EN in the top, you are redirected to admin/specials.php in english.
Then Click on FR in the top, and you are back to admin/specials in french.

--

The existing way, would mean (again assuming you are on admin/specials in french);

Click to "index" (still in french)
Click on langue button, dropdown shows, click english
Index page reloads (now in english)
Click back to admin/specials.php (now in english)

I can see how that is a Pain In The A---.
You do not have the required permissions to view the files attached to this post.

User avatar
bonbec
Contributor
Posts: 210
Joined: Mon Oct 26, 2020 12:23 pm
Phoenix Version: V1.1.0.8
Has thanked: 57 times
Been thanked: 44 times

Re: Admin switch language

Post by bonbec »

OK.
When I click on this language dropdown, it does indeed change the language.
Image
But as soon as I click on a link in the admin area, the language selector disappears.
dropdown.jpg
The other solution is to log out and log back in using a different language, which is neither practical nor quick when frequently switching between languages.
My solution (imperfect due to the modification of admin/application_top.php) avoids all that. I simply click on FR or EN, and I remain in the selected language regardless of which link I click next.
You do not have the required permissions to view the files attached to this post.

User avatar
burt
Core Team
Posts: 4739
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 279 times
Been thanked: 467 times

Re: Admin switch language

Post by burt »

I understand. Just For Testing to see if it works;

/includes/hooks/admin/siteWide/hMenu.php

Find:

Code: Select all

$output .= '<div class="col bg-light mb-1 border-bottom d-print-none">';
  $output .= '<ul class="nav justify-content-end">';
Add after:

Code: Select all

// new added
$langue = $GLOBALS['Admin']->link()->retain_query_except(['action', 'language']);
          
foreach (language::load_all() as $l) {
  $output .= '<li class="nav-item">';
    $output .= '<a class="nav-link" href="' . $langue->set_parameter('language', $l['code']) . '">' . strtoupper($l['code']) . '</a>';
  $output .= '</li>';
}
// eof
If you have time, please test?

This should give (eg) "EN" "FR " links which change language and redirect back to same page.
I am not here to build for you.
I am here to build with you. Let's help each other.



Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply