0
Votes
Undo
  1. luk
  2. Sherlock Holmes
  3. Commercial Templates
  4. Friday, 29 September 2023
  5.  Subscribe via email
Hello forum,

I need some help to modify my custom error.php to show a 404 depending on the language in my bi-lingual development site (NL and FR).

In a monolingual site (f.i. NL) I always use this error.php together with a hidden menu with a menu-item "niet-gevonden" set to NL, linked to the 404-article set to NL, and this works like expected.


<?php
defined('_JEXEC') or die;
if (($this->error->getCode()) == '404') {
header("HTTP/1.0 404 Not Found");
echo file_get_contents(JURI::root().'/niet-gevonden');
exit;}
?>


Now with 2 languages (NL and FR) I have created two menu-items linked to each appropriate article in the hidden menu and this error.php:


<?php
defined('_JEXEC') or die;
if (($this->error->getCode()) == '404') {
header("HTTP/1.0 404 Not Found");
$lang = JFactory::getLanguage();
$result = $lang->getTag();
if ($result=="nl-NL") {
echo file_get_contents(JURI::root().'/niet-gevonden');
exit;}
if ($result=="fr-FR") {
echo file_get_contents(JURI::root().'/non-trouve');
exit;}
}
?>


But now only the NL-404 is shown and the expected FR-404 shows an empty page.

My PHP-knowledge is very poor, in fact zero, so I was inspired to this older topic: https://joomla.stackexchange.com/questions/15141/how-to-create-a-custom-404-error-page-for-every-language but obviously I missed something.

Any suggestions ?
Joomla 4.3.4 – PHP 8.1.21

Thanks
References
  1. https://eltee.eu/devlcold/nl
Accepted Answer Pending Moderation
0
Votes
Undo
Solved, with this error.php, which I want to share.


<?php
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;

$errorPages = ['nl-NL' => 'nl/niet-gevonden', 'en-GB' => 'en/not-found', 'fr-FR' => 'fr/pas-trouve'];
$languageTag = Factory::getLanguage()->get('tag');

if ($this->error->getCode() == '404')
{
header("HTTP/1.0 404 Not Found");
echo file_get_contents(URI::root() . '/'
. $errorPages[$languageTag]);
exit;
}

?>


Where "niet-gevonden", "not-found" and "pas-trouve" are the menu-items with the appropriate articles, in a hidden menu.
Note that the country code must be added before in the URL, f.i. en/not-found aso
  1. more than a month ago
  2. Commercial Templates
  3. # 1
Accepted Answer Pending Moderation
0
Votes
Undo
Hi Luk

Thank you for sharing your solution. I dont believe I realised this was possible... every day is a school day :)

Ciaran
  1. more than a month ago
  2. Commercial Templates
  3. # 2
  • Page :
  • 1


There are no replies made for this post yet.
Be one of the first to reply to this post!

Join Our Newsletter

* indicates required
We respect your privacy and do not tolerate spam and will never sell, rent, lease or give away your information (name, email, number, etc.) to any third party. Nor will we send you unsolicited email.
Joomla51 - Mullaghmore, Co. Sligo, Ireland
Joomla51.com is not affiliated with or endorsed by the Joomla! Project or Open Source Matters.
The Joomla! name and logo is used under a limited license granted by
Open Source Matters
the trademark holder in the United States and other countries.