3

How can I get the current selected language using the Function JFactory::getLanguage(); from another directory?

I know how to do it when I am there:

  • /web/include
  • /web <---- [can call it from here] (solved here)

but I need to call it from...

  • /web/scripts/myScripts/myScript.php

Tried to import Joomla Framework:

// Set flag that this is a parent file.
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/../../defines.php')) {
    include_once dirname(__FILE__) . '/../../defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/../../includes/defines.php';
}
require_once JPATH_BASE.'/../../includes/framework.php';

// Instantiate the application.
$app = JFactory::getApplication('site');

// Initialise the application.
$app->initialise();

$lang =& JFactory::getLanguage()->getTag();
8
  • So are you trying to get the currently language from an external PHP file that isn't associated with Joomla?
    – Lodder
    Nov 14, 2014 at 9:23
  • Yes, seems like that :) Is it even possible? Nov 14, 2014 at 9:24
  • You will have to import the Joomla framework to do this ;)
    – Lodder
    Nov 14, 2014 at 9:26
  • I tried but I couldn't from there. Help me with that and than.. help me to get this login-function from your website too :D Nov 14, 2014 at 9:42
  • Please add the code you tried to import the Joomla framework and get the language. Then mention any errors that may have occured
    – Lodder
    Nov 14, 2014 at 9:44

1 Answer 1

0

The definition for JPATH_BASE is still missing from your code. To import the Joomla API, you can simply use the following:

define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/..' ));

require_once ( JPATH_BASE . '/includes/defines.php' );
require_once ( JPATH_BASE . '/includes/framework.php' );

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

Then to get the language tag:

$lang = JFactory::getLanguage();
$tag  = $lang->getTag();

You need to make sure JPATH_BASE has been defined correctly. I'm not sure where your external PHP file is located in relation to your Joomla installation, but you will need to change line 2 of the API importing code.

Hope this helps

5
  • Now another question, based on the $tag... Can I compare this like $tag=="en-GB" Nov 14, 2014 at 10:42
  • 1
    You're welcome. As for the login on my website, the styling is based on UIKit and the implementation is custom
    – Lodder
    Nov 14, 2014 at 10:45
  • Well, I thought it is an easy module from joomla ^^ looks like it is more complex and it needs time :) Nov 14, 2014 at 10:52
  • Can I if ($tag == "en-GB") ?? Nov 14, 2014 at 11:02
  • Yes you can do this. For example: if($tag == 'en-GB') { echo "English"; }
    – Lodder
    Nov 14, 2014 at 11:06

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.