S.Visser
S.Visser

Reputation: 4725

Get English page title Joomla

I'm working with an custom back-end template in Joomla and now I'm come to a point that I need to support multiple languages.

In the begin of the development I did not think about multiple languages and that gives me some trouble now. That it is best to rewrite the code and make it "smarter" I know, but at this point there is not much time to rewrite this code.

In the backend I'm working with icons that are based on the page title. For example the page Article Manager requests the icon images/icon/article-manager.png. So you see what happens if the page title is for example German and is called Inhalt.

At this moment I use this code to generate the iconpath:

#Loading Joomla Document Module
$doc             = JFactory::getDocument();

#Get the page title 
$title =  explode("-", $doc->title);
$title = trim(end($title));

#Generate icon path

$lastTitle = explode("-", $title);

if (strpos(end($lastTitle), ':')) {
    $lastTitle = explode(":", end($lastTitle));
    $lastTitle = $lastTitle[0];
    $iconPath = trim($lastTitle);
    $iconPath =  'templates/' . $this->template . '/images/icons/' . strtolower(str_replace(" ", "-", $iconPath)) . '.png';
} else { 
    $iconPath = trim(end($lastTitle));
    $iconPath =  'templates/' . $this->template . '/images/icons/' . strtolower(str_replace(" ", "-", $iconPath)) . '.png';
}

Now I'm thinking of searching the database for the page/componetent/modul/plugin ID but is there a faster/easier way to edit it?

Work around

As earlier stated, icons generated by a page title is indeed a bad idea. But Lodder came with a pretty nice and easy work around. So I decided to follow his idea. I added an extra variable that checks if it is a 'subpage', when yes then it extends the icon file name with this subpage.

#Loading Joomla Application Module
$app            = JFactory::getApplication();

#Genrate page icon
$comp = $app->input->get('option');
$view = $app->input->get('view');

if(!empty($view)) {
    $iconFileName = $comp . '-' . $view;
} else {
     $iconFileName = $comp;
}

$iconPath =  'templates/' . $this->template . '/images/icons/' . $iconFileName . '.png';

Upvotes: 2

Views: 72

Answers (1)

Lodder
Lodder

Reputation: 19733

Basing the icons on the Page Title is a bad idea. Purely because as you have already mentioned, your site is multi-lingual, therefore different languages will result in different results.

I would personally base the icons on the component name. For example:

$jinput = JFactory::getApplication()->input;
$view   = $jinput->get('option');

The above code will output com_content and will always be the same for all languages.

You can then simply name your icon com_content.png and call the icons like so:

$iconPath =  'templates/' . $this->template . '/images/icons/' . $view . '.png';

Hope this helps

Upvotes: 1

Related Questions