Jump to content

Translating trivial module strings – possible causes for PrestaShop to ignore it?


Narancs

Recommended Posts

I have a rather simple module which consists only of its main class. Within that class, I use strings translated with the following, conventional method:

$this->l('Derp');

However, if I want to translate the module in the Back-Office (translation of installed modules, theme independent) the untranslated string, not even the module itself, shows up. Everything else does (I am on a local installation in debug mode). I also tried all other translation views in the Back-Office to eliminate possible problems originating in front of the computer, without success.

 

I even tried to get things working by hand, creating the translations directory with de.php file just like in other modules, conforming to the array key pattern described in official documentation.

 

Still, PrestaShop ignores it, not even recognizing there is something to translate. What could the possible reason be?

Link to comment
Share on other sites

Sure, this is my complete module class, except for some security checks which I removed for the forum post not being so long: 

<?php

  define( 'MODULE_AUTOMATICEXPORT_DELIMITER', ';' );
  define( 'MODULE_AUTOMATICEXPORT_ENCLOSURE', '"' );

  class AutomaticExport extends Module
  {
    public function __construct( )
    {
      $this->name = 'automaticexport';
      $this->tab = 'export';
      $this->version = '1.0.0';
      $this->author = 'Narancs';
      $this->need_instance = 0;

      parent::__construct();

      $this->displayName = $this->l( 'Automatic Export' );
      $this->description = $this->l( 'Extends the SQL manager functionality for automatic exports.' );
    }

    public function install( )
    {
      return parent::install();
    }

    public function uninstall( )
    {
      return parent::uninstall();
    }

    public function export( )
    {
      $id_request_sql = Tools::getValue( 'id_request_sql', null );

      if ( $this->active == false )
      {
        Logger::addLog( sprintf( $this->l( 'Automatic export invoked even though module is not active (%s).' ), $id_request_sql ), 2, null, get_class( $this ), null, true );
        exit;
      }

      if ( $id_request_sql == null )
      {
        Logger::addLog( $this->l( 'Automatic export invoked with invalid parameter (null).' ), 2, null, get_class( $this ), null, true );
        exit;
      }

      $request_sqls = Db::getInstance()->ExecuteS( 'SELECT id_request_sql FROM `'._DB_PREFIX_.'request_sql`' );
      $request_sql_ids = array();

      foreach ( $request_sqls as $request_sql )
      {
        $request_sql_ids[] = ( int ) $request_sql[ 'id_request_sql' ];
      }

      if ( in_array( $id_request_sql, $request_sql_ids ) == false )
      {
        Logger::addLog( sprintf( $this->l( 'Automatic export invoked with invalid parameter (%d).' ), $id_request_sql ), 2, null, get_class( $this ), null, true );
        exit;
      }

      $requestSql = new RequestSql();

      if ( $requestSql == false )
      {
        Logger::addLog( $this->l( 'Failed to instanciate RequestSql in automatic export module.' ), 4, null, get_class( $this ), null, true );
        exit;
      }

      $sql = $requestSql->getRequestSqlById( $id_request_sql );
      $sql = $sql[ 0 ][ 'sql' ];

      $result = DB::getInstance()->executeS( $sql );

      if ( $result == 0 )
      {
        Logger::addLog( $this->l( 'The result set for the automatic export was empty.' ), 2, null, get_class( $this ), null, true );
        exit;
      }

      $filename = _PS_ROOT_DIR_.DS.'fe7e4328-b02d-455b-9fb8-8eabda09f0a2'.DS.'2cf9d794-4c0b-405b-bf51-80fec2263fb8.csv';

      $file = fopen( $filename, 'w' );

      if ( $file )
      {
        $headers = array_keys( $result[ 0 ] );
        fputcsv( $file, $headers, MODULE_AUTOMATICEXPORT_DELIMITER, MODULE_AUTOMATICEXPORT_ENCLOSURE );

        foreach ( $result as $row )
        {
          fputcsv( $file, $row, MODULE_AUTOMATICEXPORT_DELIMITER, MODULE_AUTOMATICEXPORT_ENCLOSURE );
        }

        fclose( $file );
      }
      else
      {
        Logger::addLog( sprintf( $this->l( 'Automatic export failed to write to file: "%s"' ), $filename ), 4, null, get_class( $this ), null, true );
        exit;
      }

      Logger::addLog( sprintf( $this->l( 'Successfully exported %d results of SQL request (%d).' ), count( $result ), $id_request_sql ), 1, null, get_class( $this ), null, true );
      die( '1' );
    }
  } 

In addition, there is this script within the module directory which exposes functionality as an URL callable by cron (like in gsitemap, but stripped of all clutter unsuitable for forum post):

<?php

include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');

$valid_token = Tools::encrypt( 'automaticexport/cron' );

if ( $valid_token != Tools::getValue( 'token' ) )
{
  exit;
}

if ( Module::isInstalled( 'automaticexport' ) == false )
{
  exit;
}

include( dirname( __FILE__ ).'/automaticexport.php' );

$automaticExport = new AutomaticExport();
$automaticExport->export();


I edited this post because I also stripped the actual problem parts (untranslated strings). In particular: I am not just talking about the module name and description set in the constructor but also about strings used for writing into the PrestaShop Log.

Edited by Narancs (see edit history)
Link to comment
Share on other sites

I am sorry, I should have explained it in more detail: The one line code snippet written in my first post was just a dummy, to avoid unnecessary and long code posting in the first place. It just should show what I have in the whole class document with every invocation of that method. At the beginning of the export method, where I also write to PrestaShop log, there is one such example. The module's "L" method simply always returns the string it is supposed to translate (of course, because I do not find any possibility to translate it, even if I do, the translations are ignored).

 

I will edit my post above with the full source code.

Link to comment
Share on other sites

Yepp, that's the issue.

 

It is not translated, the translation method returns the string as it was provided as input parameter, just like if there is no translation existent (even if I created the translation files like they should be and are described in PrestaShop documentation). And the back-office does not appear to parse my module for translatable strings.

 

I was looking into other modules because I was suspicious of the missing module name parameter (like it is necessary in the smarty version of the translate method, {l s='a' modn='b'}). But other modules did not use that, too (even for quite module specific strings).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...