I try create system plugin for auto change language by IP. I don't have idea how to correctly write code so as not to receive an infinite loop of redirection requests.

My code

class PlgSystemAutochangelang extends JPlugin
    public function onBeforeRender(){
        $app = JFactory::getApplication();

        $session = JFactory::getSession();

        // Use this plugin only in site application.
        if ($app->isClient('site')) {
            $get_country = file_get_contents("http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/country");

            $lang = $app->getLanguage();
            $locale = $lang->getTag();
            //echo $get_country." ";
            //echo $locale." ";

            if ($get_country !== 'UA' && $locale !== 'uk-UA'){



Who can help me, guys?

  • So, shouldn't you be saving the found value in the session, then in subsequent page loads checking if the session contains a found value? If this is too difficult, you may wish to hire a developer to code this up for you. – mickmackusa Jun 20 '19 at 11:29
  • You may already know that, but unless there is a really good reason to do it that way, forcing a language to the user based on his IP isn't such a good idea, as it may sound. – FFrewin Jun 20 '19 at 14:19
  • @FFrewin I understand but client wants it – Pavel8289 Jun 21 '19 at 9:25

If the client forces it, I would go with the @mickmackusa approach and check if the language has been set via a value in the session and only if it hasn't then execute your code to get the IP and resolve the correct language to use. You would also need to be triggering this plugin very early on in processing to ensure the right language files are used. Even though the call to the session details is a lot more economical than the http call to get the IP info, it seems like a waste for it to happen every time someone loads/relaods a page.

I also agree with @FFreewin and that user should be deciding what language they want to use so maybe a check of the session value and if not set, then check the IP and prompt with a pop up 'Would you like to change your Language to xx?'

Another option that might be useful is the System - Language Filter plugin that comes with Joomla that appears to allow you to switch the Language automatically based upon the visitors Browser Language settings which maybe more reliable than checking the IP. If you don't want to check the Browser, looking at the code in the plugin might give you ideas as it appears to store the value in a cookie for the session.


solution to the problem! It's works for me SxGeo https://sypexgeo.net/ru/download/

class PlgSystemAutochangelang extends JPlugin
    public function onAfterInitialise(){
        $app = JFactory::getApplication();
            $uri = $_SERVER['REQUEST_URI'];              

            include_once __DIR__.'/SxGeo.php';

            $SxGeo = new SxGeo( __DIR__ .'/SxGeo.dat', SXGEO_BATCH | SXGEO_MEMORY);
            $get_country = $SxGeo->getCountry($_SERVER['REMOTE_ADDR']);

            if($get_country != 'UA' && strpos($uri, '/uk/') !== false) $uri = str_replace('/uk/', '/ru/', $uri);
            elseif($get_country == 'UA' && strpos($uri, '/ru/') !== false) $uri = str_replace('/ru/', '/uk/', $uri);

            if($uri != $_SERVER['REQUEST_URI']){
                header('HTTP/1.1 303 See Other');
                header('Location: '.$uri);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.