4

I´m aware that a similar qustion is asked here: Get current language in external script (Joomla 3.3)

but it seems that noone has understood the problem correctly. I´m having the exact same problem, so here is my example, maybe it is easier to understand:

On previous Joomla sites I´ve used this script to get the current language in external script, and it works perfectly in Joomla 2.5 framework, but for some reason it doesn´t in Joomla 3.xx. In Joomla 3 $CurrentlangString always resolves to "en-GB" even though another language is active:

define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', $_SERVER["DOCUMENT_ROOT"]);
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );

$mainframe = JFactory::getApplication('site');
$mainframe->initialise();

$Currentlang = JFactory::getLanguage();
$CurrentlangString = $Currentlang->getTag();

Please advice what is wrong here..

Here is a little bonus info that may help a little. When loading the code above in external file under Joomla 3, it seems that it does indeed load the framework, because I have no problem accessing the user object and get the current user´s infos, but for some strange reason I cannot get the current language..

3

4 Answers 4

4

I am making a guess here, as I can't really test this right now and I am not entirely sure I understood what you want correctly, but this should be in the correct general direction.

define( '_JEXEC', 1 );
define( 'JPATH_BASE', $_SERVER["DOCUMENT_ROOT"]);

require_once ( JPATH_BASE '/includes/defines.php' );
require_once ( JPATH_BASE '/includes/framework.php' );
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();

$user = JFactory::getUser();
$language = $user->getParam('language', 'the default');

echo "<p>Your language is set to {$language}.</p>";

Fundamentally, hkais was correct. In Joomla! 3 the site has a default language, but not really a current language, as there might be several current users. And if you want the current language of the current user, you need ask it from the current user. Or the user you happen to be interested in. Like I said, I have no confidence I understood what you want.

6
  • Thanks for the answer. I think the you misunderstand the application of use. By "external script" I mean PHP code executed outside normal Joomla environment, like in an iframe or similar. By "current language" I mean the the language chosen or decided by the user currently executing the code / visiting the site. This is proven to be possible in Joomla 2.5 using the exact same code, so why not in Joomla 3? Commented Jan 24, 2015 at 18:14
  • Maybe it´s easier to understand if I give the actual circumstance. On my Joomla 3 site I´m using a stand alone PHP-based chat application, which runs outside Joomla environment. I need to load the current language chosen by user in order to make the chat application support multi-language strings. I have made this work 100% flawlessly loading the Joomla framework in Joomla 2.5 using the same chat system, so this problem is only with Joomla 3. Commented Jan 24, 2015 at 18:21
  • @GeniusDesign Even more puzzled, if it is embedded on your Joomla! site with an iframe or similar why can't you just pass it as a query parameter (chat.php?lang=en-EN)? // I think the basic issue is that the way multi-language works is entirely different in J!3. // Being as you specifically mentioned you could get current users infos, I take you were able read the users language from their parameters? (Just adding to where you were already reading user data.) Commented Jan 24, 2015 at 20:22
  • Thanks for the answer. Passing the language as URL parameter is of course also an option. Then I just need to be able to set the language based on language tag, e.g. "da-DK". I have tried using this code: $lang = JFactory::getLanguage(); $lang->setLanguage( 'da-DK' ); $lang->load(); But that does not work in Joomla 3, so I´m still stuck with nothing.. Commented Jan 25, 2015 at 12:16
  • @GeniusDesign But you did get the language to your code? Now it just fails to load or use the language files? Joomla documentation is a mess, but shouldn't load have more parameters? While it has default parameters, if you want to load a language other than default you probably should fill all the parameters. Commented Jan 25, 2015 at 12:47
2

I use the default Joomla multilingual management, no falang, no third-party plugin.

Same problem, and I spent 1 hour to find the solution.

The key is, don't try to use JFactory::getLanguage(). Instead of that, use JFactory::getSession() and retrieve the result like this:

$session = JFactory::getSession();
$lang_obj = $session->get('plg_system_languagefilter');
$lang_code = $lang_obj->{'language'} //and yes, here use curly brackets
echo $lang_code; //return e.g. en-GB, de-DE, etc.
1

Unfortunately Joomla 3 does not seem to support language initialization in external scripts.

I therefore had to construct an alternative solution, which might be of help to others wanting to code external scripts such as APPs and API´s for Joomla 3 with multilanguage support using Joomla´s override language files.

Here is what I did.

  1. I transferred the current users language tag to external script as URL parameter (in my case the external script is loaded via Ajax). e.g. "/scriptfolder/externalfile.php?lang=da-DK".

  2. In external script I included the following class needed to parse the override lang ini file found in /language/overrides/ (of course you can use any ini language file you like, maybe your own custom lang file from script folder):

    class ArrayINI implements ArrayAccess, IteratorAggregate {
        private $lang;
    
        public function __construct($ini) {
            $this->lang = parse_ini_file($ini);
        }
    
        function __invoke($offset) {
            return $this->offsetGet($offset);
        }
    
        public function getIterator() {
            return new ArrayIterator($this->lang);
        }
    
        public function offsetSet($offset, $value) {
            if (is_null($offset)) {
                $this->lang[] = $value;
            } else {
                $this->lang[$offset] = $value;
            }
        }
    
        public function offsetExists($offset) {
            return isset($this->lang[$offset]);
        }
    
        public function offsetUnset($offset) {
            unset($this->lang[$offset]);
        }
    
        public function offsetGet($offset) {
            return isset($this->lang[$offset]) ? $this->lang[$offset] : null;
        }
    }
    
  3. Then I executed the class to parse the file like this:

    $finallangparams = $_GET['userlang'];
    $iniLang = new ArrayINI("../../language/overrides/".$finallangparams.".override.ini");
    
  4. And now I can echo all the language strings I want like this example:

    echo $iniLang("PHP_CHAT_CUSTOM_NEED_HELP_FRONT");
    

And that´s it!

1
  • Probably not relevant since you already have working code, but I finally did what we should have done first and actually read the language.php file. It was illuminating, I recommend you do the same, if you are still curious. Basically, all the documentation we could not find is written as comments in the source file. Commented Jan 25, 2015 at 14:48
0

Please check this forum post, seems to be the identical issue to your issue.

http://forum.joomla.org/viewtopic.php?f=711&t=844514

afaik, the https://docs.joomla.org/JFactory/getLanguage getLanguage() needs a existing global language object, else a new one will be created. And I assume en-GB is your default language. So either integrate your script in to the existing session, or ensure your session gets initialized properly.

3
  • As I explained there are no problems when using the shown script in Joomla 2.5 environment, which means when I execute that code in external file on Joomla 2.5 site then obviously the session is initialized properly. Commented Jan 23, 2015 at 16:50
  • So, the question is why does it not work in Joomla 3? And the Joomla 3 site I´m working on has Danish language set as default language - both front-end and back-end. Commented Jan 23, 2015 at 16:51
  • for your info, I have looked everywhere, in all Joomla related forums I have found, and to my knowledge there has not been a single example of someone managing to get the current language in external script on Joomla 3 site. It´s pretty strange to me that Joomla 2.5 and Joomla 3 apparently doesn´t share the same method for fetching the framework needed to get the users current/active language. Commented Jan 23, 2015 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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