Jfactory::getLanguage() do not return good language

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
mphm
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon May 05, 2014 9:19 am

Jfactory::getLanguage() do not return good language

Post by mphm » Mon May 05, 2014 9:24 am

Hi all,

I have a multi-language web site and it seems that Joomla 3.2 don't return the good language in external script with this code :


<?php
// Protect from unauthorized access
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', '../');
require_once ( JPATH_BASE.DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE.DS.'includes'.DS.'framework.php' );

// load language string definitions
$lang = JFactory::getLanguage();
$lang->load('ed3c_contrats');

echo($lang->getName()); return always GB ????

goedda
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 130
Joined: Sat Aug 06, 2011 3:40 am

Re: Jfactory::getLanguage() do not return good language

Post by goedda » Mon May 05, 2014 11:46 am

It is not clear what you are trying to achieve... Are you trying to load the language file 'ed3c_contrats' and it is not working properly? Or do you only want to get the language code and this is not working?

If you want the language code, try this, where every aspect of the language is elaborated
http://stackoverflow.com/questions/3352 ... la-website

In short, for the language code, you can use this

Code: Select all

$lang = JFactory::getLanguage();
$lang->getTag(); // returns 'en-GB' or 'en-US' for example
We come in peace, always!
http://www.quadrifolia.ch

mphm
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon May 05, 2014 9:19 am

Re: Jfactory::getLanguage() do not return good language

Post by mphm » Mon May 05, 2014 12:46 pm

Hi goedda,

In my script I would like to load he correct language file ed3c_contrats. I have one file en-GB.ed3c_contrats.ini in /languages/en-GB and this other fr-FR.ed3c_contrats.ini in /languages/fr-FR.
However it's always the en-GB file which is loaded. And the line echo(lang->getName()); return English (United Kingdom) even if I switch the site in French. By the way I noted that when the site is in English the url is http://..../en/index.php while in french I got http://..../index.php and not http://..../fr/index.php

Do you think it's normal ?


Philippe

goedda
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 130
Joined: Sat Aug 06, 2011 3:40 am

Re: Jfactory::getLanguage() do not return good language

Post by goedda » Wed May 07, 2014 10:59 am

It is normal for SEF-URLs since Joomla! 1.7.x or so.
http://multilingual.demojoomla.com/mult ... ml?start=8
You may try and disable SEF temporarily, check if it works then. This will cost you only a few minutes...

If that does not work, I would check, if you get a different result while being logged in frontend as administrator. This would be a hint that the language does not have a 'content'. You should be able to fix that by going backend to the 'Language Manager > Content' and create a content for the French language.
This should help doing that (just use the French code etc. instead)

[url deleted by Moderator as one has to pay to access this tutorial)
We come in peace, always!
http://www.quadrifolia.ch

mphm
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon May 05, 2014 9:19 am

Re: Jfactory::getLanguage() do not return good language

Post by mphm » Sun May 18, 2014 9:49 pm

goedda wrote:It is normal for SEF-URLs since Joomla! 1.7.x or so.
http://multilingual.demojoomla.com/mult ... ml?start=8
You may try and disable SEF temporarily, check if it works then. This will cost you only a few minutes...

Disable SEF didn't work.

If that does not work, I would check, if you get a different result while being logged in frontend as administrator.

In administrator frontend same thing it's always the English page.

This would be a hint that the language does not have a 'content'. You should be able to fix that by going backend to the 'Language Manager > Content' and create a content for the French language.
This should help doing that (just use the French code etc. instead)

I have a content for both French and English languages.

[url deleted by Moderator as one has to pay to access this tutorial)

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

Re: Jfactory::getLanguage() do not return good language

Post by infograf768 » Tue May 20, 2014 5:43 am

Which type of extension is "ed3c_contrats"? How is it it has no extension type (com, plg, mod, tpl, lib..)?
Do you need to load that specific language file when the extension is used, or in another context?

Apart from this extension, is the multilanguage working as should, i.e. loading French or English when desired?

Please attach a screenshot of the multilanguage status module
( see http://community.joomla.org/blogs/commu ... odule.html )
Jean-Marie Simonet / infograf
---------------------------------
ex-Joomla Translation Coordination Team • ex-Joomla! Production Working Group

mphm
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon May 05, 2014 9:19 am

Re: Jfactory::getLanguage() do not return good language

Post by mphm » Sat May 24, 2014 3:36 pm

infograf768 wrote:Which type of extension is "ed3c_contrats"? How is it it has no extension type (com, plg, mod, tpl, lib..)?
Do you need to load that specific language file when the extension is used, or in another context?
ed3c_contrats.ini is not an extension it's the language file that the script load when it start.
Apart from this extension, is the multilanguage working as should, i.e. loading French or English when desired?
The site works correctly with the two languages except when I run my own scripts. The problem is not the language files, the scripts do not recognize the selected language when the user click in the flag although all this other menus switch correctly to the language.
As I said the lines :
Jfactory::getLanguage();
echo $lang->getName();
return always English
Please attach a screenshot of the multilanguage status module
( see http://community.joomla.org/blogs/commu ... odule.html )
You can have a look at this address : http://ed3c-essai.snv.jussieu.fr
On script is in the menu Contrats doctoraux -> recherche projets de thèse

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

Re: Jfactory::getLanguage() do not return good language

Post by infograf768 » Sun May 25, 2014 7:42 am

As we can't access to your custom script, I do not see how we can help.
http://ed3c-essai.snv.jussieu.fr/index. ... t-these-fr
(French version)
is loaded in a fr-fr document where JFactory::getLanguage would echo the correct language.

Code: Select all

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr-fr" lang="fr-fr" dir="ltr" >
Therefore I guess your script has an error.
I suggest you enable language debug and see which precise language files are loaded as, even in English, the drop down options (Indifferente) is in French (Without the accent).

I also suggest you look better at the method in library/joomla/language/language.php

Code: Select all

public function load($extension = 'joomla', $basePath = JPATH_BASE, $lang = null, $reload = false, $default = true)
or at the method loadLanguage
To understand the way a language file is loaded.

You may have to use rather something like:

Code: Select all

$lang = JFactory::getLanguage();
$lang->load('ed3c_contrats' , dirname(__FILE__), $lang->getTag(), false, true);
Where "dirname(__FILE__)" would be "JPATH_SITE" if your ini files are in language/en-GB/ and language/fr-FR/
Jean-Marie Simonet / infograf
---------------------------------
ex-Joomla Translation Coordination Team • ex-Joomla! Production Working Group

mphm
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon May 05, 2014 9:19 am

Re: Jfactory::getLanguage() do not return good language

Post by mphm » Mon May 26, 2014 10:27 am

infograf768 wrote:As we can't access to your custom script, I do not see how we can help.
http://ed3c-essai.snv.jussieu.fr/index. ... t-these-fr
(French version)
is loaded in a fr-fr document where JFactory::getLanguage would echo the correct language.

Code: Select all

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr-fr" lang="fr-fr" dir="ltr" >
Therefore I guess your script has an error.
I suggest you enable language debug and see which precise language files are loaded as, even in English, the drop down options (Indifferente) is in French (Without the accent).

I also suggest you look better at the method in library/joomla/language/language.php

Code: Select all

public function load($extension = 'joomla', $basePath = JPATH_BASE, $lang = null, $reload = false, $default = true)
or at the method loadLanguage
To understand the way a language file is loaded.

You may have to use rather something like:

Code: Select all

$lang = JFactory::getLanguage();
$lang->load('ed3c_contrats' , dirname(__FILE__), $lang->getTag(), false, true);
Where "dirname(__FILE__)" would be "JPATH_SITE" if your ini files are in language/en-GB/ and language/fr-FR/
Thanks for the information. Unfortunately the suggestion in code don't work. I have activated the langues debug and strangely the ed3c_contrats file are never loaded in the debug console.

**Fichiers de langue chargés**
**Chargé** : JROOT/language/fr-FR/fr-FR.ini
**Chargé** : JROOT/administrator/language/fr-FR/fr-FR.plg_system_logout.ini
**Chargé** : JROOT/language/fr-FR/fr-FR.lib_joomla.ini
**Chargé** : JROOT/language/fr-FR/fr-FR.tpl_ed3c.ini
**Chargé** : JROOT/language/fr-FR/fr-FR.com_wrapper.ini
**Chargé** : JROOT/administrator/language/en-GB/en-GB.com_rsform.ini
**Chargé** : JROOT/administrator/language/fr-FR/fr-FR.com_rsform.ini
**Chargé** : JROOT/administrator/language/fr-FR/fr-FR.plg_system_jcemediabox.ini
**Chargé** : JROOT/language/fr-FR/fr-FR.mod_languages.ini
**Chargé** : JROOT/language/fr-FR/fr-FR.mod_menu.ini
**Chargé** : JROOT/language/fr-FR/fr-FR.mod_articles_news.ini
**Chargé** : JROOT/language/fr-FR/fr-FR.mod_breadcrumbs.ini
**Chargé** : JROOT/plugins/attachments/attachments_plugin_framework/language/fr-FR/fr-FR.plg_attachments_attachments_plugin_framework.ini
**Chargé** : JROOT/administrator/language/fr-FR/fr-FR.plg_system_debug.ini

here the code of the script :
//

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<title>Demande allocation</title>
<link rel="stylesheet" href="/templates/ed3c/css/ed3c.css" type="text/css">

<script type="text/javascript">
function showUnity(str)
{
if (str=="")
{
document.getElementById('txtHint').innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('txtHint').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/Contrats/Fiches/getunity.php?q="+str,true);
xmlhttp.send();
}
</script>
<script language="JavaScript" type="text/javascript" src="/includes/gen_validatorv4.js"></script>
</head>
<body style='background:#ffffff'>

<?php
// Protect from unauthorized access
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', '../../');
require_once ( JPATH_BASE.DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE.DS.'includes'.DS.'framework.php' );

// load language string definitions
$lang = JFactory::getLanguage();
$lang->load('ed3c_contrats' , JPATH_SITE, $lang->getTag(), false, true);

//$lang->load('ed3c_contrats',JPATH_BASE);
.....

//

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

Re: Jfactory::getLanguage() do not return good language

Post by infograf768 » Tue May 27, 2014 8:54 am

What does the debug lang show when you are in en-GB?
Jean-Marie Simonet / infograf
---------------------------------
ex-Joomla Translation Coordination Team • ex-Joomla! Production Working Group

mphm
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon May 05, 2014 9:19 am

Re: Jfactory::getLanguage() do not return good language

Post by mphm » Mon Jun 02, 2014 9:03 pm

infograf768 wrote:What does the debug lang show when you are in en-GB?
Strangely the en-GB.ed3c_contrats.ini are not loaded ???

**Parsing errors in language files**
**None**

**Language Files Loaded**
**Loaded** : JROOT/language/en-GB/en-GB.ini
**Loaded** : JROOT/administrator/language/en-GB/en-GB.plg_system_logout.ini
**Loaded** : JROOT/language/en-GB/en-GB.lib_joomla.ini
**Loaded** : JROOT/language/en-GB/en-GB.tpl_ed3c.ini
**Loaded** : JROOT/language/en-GB/en-GB.com_wrapper.ini
**Loaded** : JROOT/administrator/language/en-GB/en-GB.plg_system_jcemediabox.ini
**Loaded** : JROOT/language/en-GB/en-GB.mod_languages.ini
**Loaded** : JROOT/language/en-GB/en-GB.mod_menu.ini
**Loaded** : JROOT/language/en-GB/en-GB.mod_breadcrumbs.ini
**Loaded** : JROOT/plugins/attachments/attachments_plugin_framework/language/en-GB/en-GB.plg_attachments_attachments_plugin_framework.ini
**Loaded** : JROOT/administrator/language/en-GB/en-GB.plg_system_debug.ini
**Untranslated Strings**
**None**

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

Re: Jfactory::getLanguage() do not return good language

Post by infograf768 » Tue Jun 03, 2014 7:07 am

Your script is wrong somewhere...
Jean-Marie Simonet / infograf
---------------------------------
ex-Joomla Translation Coordination Team • ex-Joomla! Production Working Group

mphm
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon May 05, 2014 9:19 am

Re: Jfactory::getLanguage() do not return good language

Post by mphm » Thu Jun 05, 2014 3:42 am

infograf768 wrote:Your script is wrong somewhere...
Yes but where ? ;D

That I don't understood is that the translation works in English even the en-GB.ed3c_contrats.ini file is not loaded in the debug panel.
And why it didn't find the french file ?

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

Re: Jfactory::getLanguage() do not return good language

Post by infograf768 » Thu Jun 05, 2014 9:07 am

I would say first: why does it not display the English file in the debug?
Jean-Marie Simonet / infograf
---------------------------------
ex-Joomla Translation Coordination Team • ex-Joomla! Production Working Group

mphm
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon May 05, 2014 9:19 am

Re: Jfactory::getLanguage() do not return good language

Post by mphm » Fri Jun 13, 2014 11:04 am

I'm trying to bypass the Joomla language choice by forcing the language in my script reading the paramaters in the url, but it doesn't work :

// load language string definitions
$tag = $_GET['lang'];
echo ("Language: ".$_GET['lang']);
$lang = JFactory::getLanguage();
if ($tag == 'fr')
$lang->load('ed3c_contrats',JPATH_BASE,'fr-FR');
else
$lang->load('ed3c_contrats',JPATH_BASE,'en-GB');

$_GET['lang'] return an empty string. Note that I have disable the SEF in the server.

mphm
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Mon May 05, 2014 9:19 am

Re: Jfactory::getLanguage() do not return good language

Post by mphm » Thu Sep 03, 2015 10:38 am

Hi,
Finally I found a other way (not so nice but) to get the current language. It seems that in Joomla 3 getLanguage() doesn't work from outside php scripts.
So I get the current language from the current url as :

Code: Select all

// load language string definitions in url
$app = JFactory::getApplication('site');
$app->initialise();
$lang = JFactory::getLanguage();
$url = getenv("HTTP_REFERER");
$choice_lg = strpos($url_lg, "/en/");
if ($choice_lg === false) $lg_tag = "fr-FR";
else $lg_tag = "en-GB";
$extension = 'ed3c_soutenance';
$base_dir = JPATH_SITE;
$lang->load($extension, $base_dir, $lg_tag, true);


Locked

Return to “Language - Joomla! 3.x”