Prestashop: Can't use translations on module overrides

Created on 26 Feb 2020  路  18Comments  路  Source: PrestaShop/PrestaShop

Describe the bug
We can't use translations on override files of modules.
According to this documentation, we tried with Module::getInstanceByName('mymodulename')->l().

To Reproduce
Steps to reproduce the behavior:

  1. Create an override file in a module (inside override folder)
  2. Add a translation with Module::getInstanceByName('mymodulename')->l()
  3. The translation doesn't appear in BO

Edited by Khouloud

  1. Override /ps_facetedsearch/src/Product/SearchProvider.php
  2. Create an override of the module ps_facetesearch in this path /Project_folder/override/modules/ps_facetedsearch/src/Product/SearchProvider.php
<?php

if (!defined('_PS_VERSION_'))
    exit;
class SearchProviderOverride extends SearchProvider
{

    private function hideUselessFacets(array $facets)
    {
        foreach ($facets as $facet) {
            if ($facet->getWidgetType() === 'slider') {
                $facet->setDisplayed(
                    $facet->getProperty('min') != $facet->getProperty('max')
                );
                continue;
            }

            $usefulFiltersCount = 0;
            foreach ($facet->getFilters() as $filter) {
                if ($filter->getMagnitude() > 0) {
                    ++$usefulFiltersCount;
                }
            }

            $facet->setDisplayed(
                $usefulFiltersCount > 2
            );
        }
        Module::getInstanceByName('Facetedsearch')->l('My text to translate');
    }
}
  1. Delete the cache manually
  2. In the BO => Translation pf the module => I couldn't find this translation

Additional information
PrestaShop version: 1.7.x

1.7.5.2 1.7.6.3 Bug CO Minor Modules Translations Won't Fix

All 18 comments

Hello @micka-fdz ,

We manage to reproduce the issue, it is added to our debug roadmap.
Your ticket is a duplicate of #13392
So, I close it.

Duplicate of #13392

@florine2623 it's not the same, we're talking about class override, not about tpl.

@florine2623 my issue is about php files (in override folder), not template files override in theme... Can you reopen it please?

Hello @jf-viguier & @micka-fdz ,

I just reopened the issue.

A file in override folder is definitely a class override, not a tpl extend. Thanks for reopening.

Hi @jf-viguier ,

It is similar to this issue : https://github.com/PrestaShop/PrestaShop/issues/12300 ?

Thanks to check & feedback!

@florine2623 not at all, : #12300 uses the new translation system $this->trans()
We're talking about the classic one $this->l()

Hello @jf-viguier ,

Could you send me the demo module you have created, I will test this on my shop ?

Thanks!

@florine2623 I don't have any demo module for that, but you can use our module generator in fop_console.

Or just create a simple override file in any native module.

Hi @micka-fdz, @jf-viguier,

I am trying to override /ps_facetedsearch/src/Product/SearchProvider.php
I created an override of the module ps_facetesearch in this path /Project_folder/override/modules/ps_facetedsearch/src/Product/SearchProvider.php

<?php

if (!defined('_PS_VERSION_'))
    exit;
class SearchProviderOverride extends SearchProvider
{

    private function hideUselessFacets(array $facets)
    {
        foreach ($facets as $facet) {
            if ($facet->getWidgetType() === 'slider') {
                $facet->setDisplayed(
                    $facet->getProperty('min') != $facet->getProperty('max')
                );
                continue;
            }

            $usefulFiltersCount = 0;
            foreach ($facet->getFilters() as $filter) {
                if ($filter->getMagnitude() > 0) {
                    ++$usefulFiltersCount;
                }
            }

            $facet->setDisplayed(
                $usefulFiltersCount > 2
            );
        }
        Module::getInstanceByName('Facetedsearch')->l('My text to translate');
    }
}

I deleted the cache manually
In the BO => Translation pf the module => I couldn't find this translation

Can you confirm this?

PS: If I edit the /ps_facetedsearch/src/Product/SearchProvider.php directly (without override)
The translation is well displayed
image

Thanks to check & feedback.

Yes @khouloudbelguith, the issue is about override in module folder.

@micka-fdz, thanks for your feedback.
I manage to reproduce the issue with PS1.7.6.3 and PS1.7.5.2
I鈥檒l add this to the debug roadmap so that it鈥檚 fixed. If you have already fixed it on your end or if you think you can do it, please do send us a pull request!
Thanks!

How is this not fixed?

New wordings declared in overrides don't show up in the translation interface because only modules and themes can declare new wordings. Wordings don't appear magically in the translation interface, they require static analysis of the source code (aka reading and interpreting code via code), which is terribly slow.

Overrides are meant to progressively disappear as core legacy classes are replaced by services that modules can decorate or replace. As such, I don't think it makes sense to add a "wording discovery" feature there.

If you need to create new wordings and use them in your overrides, just create a simple module containing those wordings. Follow the documentation on how to make your wordings appear in the translation interface. Once translated, you'll be able to use them anywhere in PrestaShop (including in overrides), just by getting the translator instance.

Alternatively, you can just create them in the ps_translations table. Once the cache is rebuilt they should be available for use.

I'm assuming this bug is about using new translations in overrides and closing it as won't fix. Feel free to ping me otherwise.

It's sad. Actually, there is no other way than override for customizing front module behaviors.

Was this page helpful?
0 / 5 - 0 ratings