Switching Joomla's language on-the-fly

76 views
Skip to first unread message

Rimas Kudelis

unread,
Sep 28, 2009, 1:40:21 PM9/28/09
to Joomla! Developer List
Hello,

I want to write a small plugin for Joomla, which would do the following:
1. check if language is set for the current menu item,
1.1. if not, check if language is set for the currently displayed item,
2. if we have the language after step 1, switch Joomla to use that
language (for dates, standard links etc.).

I would also like to avoid any HTTP redirects or cookie dependencies.

My questions are:
Is there a way to switch Joomla's language from a plugin?
If yes, then how?
What event should I listen to in the plugin?
What type of the plugin should it be? I tried both Content and System
plugins, but still couldn't make the language to switch without an
additional redirect...

Thanks in advance for any clues.
Rimas Kudelis

Ed Stafford

unread,
Sep 28, 2009, 2:11:34 PM9/28/09
to joomla-de...@googlegroups.com
Just as an aside, could you also look at any browser information and retrieve the language set from that?

2009/9/28 Rimas Kudelis <r...@akl.lt>

Rimas Kudelis

unread,
Sep 28, 2009, 2:31:25 PM9/28/09
to joomla-de...@googlegroups.com
Yes, I can, but in my case, this only makes sense when the user is visiting the homepage, and not any of the inner pages.

Rimas

Ian MacLennan

unread,
Sep 28, 2009, 2:43:26 PM9/28/09
to joomla-de...@googlegroups.com
I would try:

$systemLanguage = &JFactory::getLanguage();
$myLanguage = &JLanguage::getInstance('en-GB'); //substitute your language code
$systemLanguage = $myLanguage;

Ian

Rimas Kudelis

unread,
Sep 28, 2009, 4:02:16 PM9/28/09
to joomla-de...@googlegroups.com
Thanks Ian!

I've added your lines to a system plugin's onAfterRoute function, and they seem to work well. However, if I add the lines to a content plugin (or to system plugin's onAfterDispatch function), they no longer have enough effect.
Hence, I have another problem: how can I access current menu item and current article from onAfterRoute?

TIA,
Rimas

Ian MacLennan

unread,
Sep 28, 2009, 4:10:20 PM9/28/09
to joomla-de...@googlegroups.com
$app = &JFactory::getApplication();
$menu = &$app->getMenu();
$menuItem = &$menu->getActive();

$articleId = JRequest::getInt('id'); // provided it is an article we are viewing

Ian

akede

unread,
Sep 28, 2009, 4:37:42 PM9/28/09
to Joomla! General Development
Hi Rimas,

If you look at the Joom!Fish extension ( http://www.joomfish.net ) and
within our JFRouter plugin you will find most of the code you are
looking for. Joom!Fish does this check in a system plugin and sets the
browser or other language directly for your system

Cheers

Alex
> >>     2009/9/28 Rimas Kudelis <r...@akl.lt <mailto:r...@akl.lt>>

Rimas Kudelis

unread,
Sep 29, 2009, 3:47:24 AM9/29/09
to joomla-de...@googlegroups.com
Thanks again Ian!

2009.09.28 23:10, Ian MacLennan rašė:
> $app = &JFactory::getApplication();
> $menu = &$app->getMenu();
> $menuItem = &$menu->getActive();

Apparently, I could also just add the following line to make my previous
code work:
jimport( 'joomla.application.menu' );

> $articleId = JRequest::getInt('id'); // provided it is an article we
> are viewing

Provided that I have the id, is there an easy way to access the article
from onAfterRoute?

Rimas


Rimas Kudelis

unread,
Oct 3, 2009, 5:01:46 AM10/3/09
to joomla-de...@googlegroups.com
Hi all,

I still haven't got any answer to my question below. I hope you don't
mind me re-asking:

Louis Landry

unread,
Oct 8, 2009, 6:51:57 PM10/8/09
to joomla-de...@googlegroups.com
Well if you have the id you can use JTableContent to load the article data

$article = JTable::getInstance('content');
$article->load($articleId);

then $article is populated with the data for the article.

- Louis
--
Development Coordinator
Joomla! ... because open source matters.
http://www.joomla.org

Rimas Kudelis

unread,
Oct 9, 2009, 10:12:28 AM10/9/09
to joomla-de...@googlegroups.com
Thank you Louis!

This is exactly what I needed!

Rimas

Debreczeni András

unread,
Nov 10, 2009, 4:17:31 AM11/10/09
to Joomla! General Development
We may also need to check for debug language configuration as
follows:


$systemLanguage = &JFactory::getLanguage();
$myLanguage = &JLanguage::getInstance( 'en-GB' );

$conf =& JFactory::getConfig();
$debug = $conf->getValue('config.debug_lang');
$myLanguage->setDebug($debug);

$systemLanguage = $myLanguage;

András
Reply all
Reply to author
Forward
Message has been deleted
0 new messages