News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

[solved] Change current language in custom Plugin

Started by taenny, September 07, 2014, 23:40:03 PM

Previous topic - Next topic

taenny

Hello,

i have been trying to solve this one on my own for hours now (forum and google search didn't help much), so maybe someone can give me a hint.

I am developing a plugin extending vmCustomPlugin that uses the hook plgVmOnUserInvoice to send an e-mail to a supplier (not vendor), who can create products in the frontend.
As this "supplier" does not necessarily speak the same language as the customer, who triggers plgVmOnUserInvoice with "his" language, the current language has to be changed accordingly.
The problem is, that I do not manage to change the current language within my plugin, so that JTEXT translates to the changed language.

In general strings are translated correctly with JTEXT::_(); - when triggered "in german" it translates to german etc.
The corresponding translations are included in the plugin language folder and work (for instance language/de-DE/de.DE.plg_vmcustom_notifysupplier.ini ...)

In my code snippet below vmdebug first evaluates to "German (DE-CH-AT)" and then to "English (United Kingdom)" but JTEXT still translates into german.
Various posts across the internet imply that in order to change the current language it has to be loaded by JLanguage::load() but I'm not sure whether this also applies to plugins - I have tried (see below) without success.
In the constructor the translation files are already included by $this->loadLanguage(); so I wonder if that is truly necessary.

Am I missing something obvious? Any help is greatly appreciated!

<?php
// no direct access
defined'_JEXEC' ) or die( 'Restricted access' );

if (!
class_exists('vmCustomPlugin')) require(JPATH_VM_PLUGINS DS 'vmcustomplugin.php');
if (!
class_exists('VmModel')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'config.php');

class 
plgVmCustomNotifySupplier extends vmCustomPlugin
{
  public function 
__construct(&$subject$config)
  {
          
parent::__construct($subject$config);
          
$this->loadLanguage();
  }

 function 
plgVmOnUserInvoice($orderDetails, &$data)
 {
    
$language =& JFactory::getLanguage();

    
vmdebug($language->getName());
    
$language->setLanguage("en-GB");
    
$language->load('plg_vm_notifysupplier'JPATH_PLUGINS."/vmcustom/notifysupplier"'en-GB'truetrue);       
    
vmdebug($language->getName());

    
$subject JText::_('PLG_VMCUSTOM_NOTIFYSUPPLIER_MAIL_SUBJECT');
  
    return 
null;
 }
}
Vm Version: 2.6.12.2
Joomla: 2.5.27
Php: 5.5
MySQL: 5.5

taenny

OK I figured it out.
3 lines of code -  :-[ ... did no longer see the wood for the trees...


$language = JFactory::getLanguage();
$language->setLanguage("en-GB");
$this->loadLanguage();
Vm Version: 2.6.12.2
Joomla: 2.5.27
Php: 5.5
MySQL: 5.5