What objects comes with "JFactory::getLanguage()"?

General questions regarding the use of languages in Joomla! 3.x.

Moderators: infograf768, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
sokrates_mx5
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 148
Joined: Tue Mar 24, 2009 12:30 pm

What objects comes with "JFactory::getLanguage()"?

Post by sokrates_mx5 » Fri May 15, 2020 2:37 pm

I'm trying to adopt a plugin for a multilingual web site and I need to get the two character language code of the current page, e.g. "en". I believe this can be done by JFactory::getLanguage(). Does this JFactory function contain an array and where do one find information of what objects are included?

I belive the following code will give the language code in this format of en-GB:

Code: Select all

$lang = JFactory::getLanguage();
$langCode = $lang->getName;
Does JFactory::getLanguage() also include the two character language code e.g. "en" or do I have to write a code to get "en" out of "en-GB"?

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1810
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: What objects comes with "JFactory::getLanguage()"?

Post by SharkyKZ » Fri May 15, 2020 6:44 pm

JFactory::getLanguage() returns an instance of Joomla\CMS\Language\Language. You can find API documentation here https://api.joomla.org/cms-3/classes/Jo ... etLanguage. You can also inspect the source. And most IDEs show information about returned object.

The class in question has no getName property. It does have getName() method but that returns the name of the language pack.

If by two character code you mean the one used in SEF URLs (note: it doesn't have to be 2 characters), you can get it this way:

Code: Select all

use Joomla\CMS\Factory;
use Joomla\CMS\Language\LanguageHelper;

// Get the current language tag.
$currentLanguage = Factory::getLanguage()->getTag();

// Get all languages.
$languages = LanguageHelper::getLanguages('lang_code');

// Get the SEF tag of current language.
$sefTag = $languages[$currentLanguage]->sef;

sokrates_mx5
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 148
Joined: Tue Mar 24, 2009 12:30 pm

Re: What objects comes with "JFactory::getLanguage()"?

Post by sokrates_mx5 » Sat May 16, 2020 8:05 am

SharkyKZ wrote:
Fri May 15, 2020 6:44 pm
JFactory::getLanguage() returns an instance of Joomla\CMS\Language\Language. You can find API documentation here https://api.joomla.org/cms-3/classes/Jo ... etLanguage. You can also inspect the source. And most IDEs show information about returned object.

The class in question has no getName property. It does have getName() method but that returns the name of the language pack.

If by two character code you mean the one used in SEF URLs (note: it doesn't have to be 2 characters), you can get it this way:

Code: Select all

use Joomla\CMS\Factory;
use Joomla\CMS\Language\LanguageHelper;

// Get the current language tag.
$currentLanguage = Factory::getLanguage()->getTag();

// Get all languages.
$languages = LanguageHelper::getLanguages('lang_code');

// Get the SEF tag of current language.
$sefTag = $languages[$currentLanguage]->sef;
Thanks for your help.

I had to change your code a bit since it kicked out an error. The following it does the job:

Code: Select all

			// Get the current language tag.
			$currentLanguage = JFactory::getLanguage()->getTag();

			// Get all languages.
			$languages = JLanguageHelper::getLanguages('lang_code');

			// Get the SEF tag of current language.
			$sefTag = $languages[$currentLanguage]->sef;

User avatar
infograf768
Joomla! Master
Joomla! Master
Posts: 19019
Joined: Fri Aug 12, 2005 3:47 pm
Location: **Translation Matters**

Re: What objects comes with "JFactory::getLanguage()"?

Post by infograf768 » Sun May 17, 2020 9:19 am

Error came from the fact that the code was for J4. No worry.
Jean-Marie Simonet / infograf · http://www.info-graf.fr
---------------------------------
ex-Joomla Translation Coordination Team • ex-Joomla! Production Working Group

sokrates_mx5
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 148
Joined: Tue Mar 24, 2009 12:30 pm

Re: What objects comes with "JFactory::getLanguage()"?

Post by sokrates_mx5 » Sun May 17, 2020 11:18 am

infograf768 wrote:
Sun May 17, 2020 9:19 am
Error came from the fact that the code was for J4. No worry.
Thanks, good to know.

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1810
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: What objects comes with "JFactory::getLanguage()"?

Post by SharkyKZ » Sun May 17, 2020 11:43 am

No, the code should work on 3.8 and above.

User avatar
infograf768
Joomla! Master
Joomla! Master
Posts: 19019
Joined: Fri Aug 12, 2005 3:47 pm
Location: **Translation Matters**

Re: What objects comes with "JFactory::getLanguage()"?

Post by infograf768 » Mon May 18, 2020 5:46 am

@SharkyKZ
Does it not need to add on top of the php file?

Code: Select all

use Joomla\CMS\Factory;
use Joomla\CMS\Language\LanguageHelper;
Jean-Marie Simonet / infograf · http://www.info-graf.fr
---------------------------------
ex-Joomla Translation Coordination Team • ex-Joomla! Production Working Group


Post Reply

Return to “Language - Joomla! 3.x”