Using Joomla JText variables on Ajax external file

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

Moderator: 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.
Locked
gerry_mandm
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Oct 14, 2021 8:00 am

Using Joomla JText variables on Ajax external file

Post by gerry_mandm » Thu Oct 14, 2021 8:08 am

Joomla version: 3.10.2

Basically I'm working on a custom code inside a Joomla module and I'm using jQuery Ajax.
Everything's is working great and I also need to use Joomla language variables because it's a multilanguage website but seems like the Ajax files doesn't recognize the Joomla language tag.

I add to my Ajax files this code in the start:

Code: Select all

define( '_JEXEC', 1 );
define( 'JPATH_BASE', '/var/www/');

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

$lang = JFactory::getLanguage();
$lang->load('custom',JPATH_ROOT)
But there's no value on $lang->getTag() once the Ajax file process the request, and the Joomla JTEXT variable are untranslated.

Am I missing something?
How to use JText variables and native Joomla language features in external ajax script file?

Thanks so much and best regards!

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

Re: Using Joomla JText variables on Ajax external file

Post by SharkyKZ » Thu Oct 14, 2021 8:31 am

Why don't you use Ajax component (com_ajax)?

gerry_mandm
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Oct 14, 2021 8:00 am

Re: Using Joomla JText variables on Ajax external file

Post by gerry_mandm » Thu Oct 14, 2021 8:33 am

Hi SharkyKZ, good idea. Didn't thought about this... gonna search for how to use com_ajax and give it a try. Thanks!


Locked

Return to “Language - Joomla! 3.x”