Translation (.tpl) does not work when printing a pdf order in the administrative part of prestashop 8
Come back
41

Translation (.tpl) does not work when printing a pdf order in the administrative part of prestashop 8

I'll leave it here so I don't lose it

There is a similar problem in the post: Translation does not work when printing a pdf order in Prestashop 1.7 and higher

In the examples I will use the standard theme "classic"

I'll describe a little how translations work

When you go to the translations section in the admin area, you see a list of translations that are available from the store templates. Since all templates in the store are scanned through the code if the file line contains an entry {l s='Translate text' d='Shop.Theme'}. Scanning occurs through specific directories of the site. For example, let's take a store theme, the directory is written in the code "themes", and if there are format template files in it ".tpl", then they will all be scanned, even if they are in nested directories. After scanning, temporary saving occurs, like a cache (so as not to overload the store too much). If you disable caching in the settings, the file will be scanned again.

Thus, in your Administrative part a field for translation may appear. Which may be empty or filled in.

Translations are stored in files ".xlf" and in the database in the table "translate", will list them:

1) File formats ".xlf" are stored in the directory of the latest version, for example, "prestashop 1.7 versions" this directory you can find, for example, by the path "\app\Resources\translations\en-US\....en-US.xlf". Translations are loaded from there, and the priority of this file will be 2, since the translation is initially checked in the database, and then the translation is searched for in the catalog, that is, in the file ".xlf".
In the new version of prestashop 8, the catalog is now located at the path "\translations\en-US\....en-US.xlf"

2) In the database in the table "translate" translations of the selected topic are stored, or base translations are stored. Therefore, if you need, for example, to receive translations of pdf templates in the format ".tpl", you should choose the name of the topic you are translating, or not choose a topic and get translations of basic templates.

That is, as I wrote earlier, first a search for a translation is performed in the database in the table "translate", if translation is not found, searches in file ".xlf". But if the translation is not found in ".xlf", then the text that you entered in the template is displayed.

My problem is in translating pdf invoice templates, where pdf is compiled from "tpl" templates

After the user places an order on the website, then in the administrative part in the section "Orders" I see the orders that the user has completed. I go to the order and want to create an invoice in the format pdf, I do everything in the administrative part. After the download is complete pdf, I open it and not all the text is translated.

Because the templates pdf, I created (or moved/modified) in the topic along the way "/themes/classic/pdf/...". So that each topic has its own template. Or if the store is updated, the theme templates will not be affected, and my changes will remain in the files. Since the basic templates for "pdf" stored along the way "/pdf/..." and they are updated if the store updates to a new version.

Since the translation was performed in the selected topic, the translation should have been loaded from it. But it will not load the translation from it because in the administrative part, you are in the order section and you do not know whether the topic is selected or not, so the theme is not available to you and the translation will not be received. And in pdf you won't have everything translated. This is the error of prestashop. If your store works as a multi-store, then perhaps the translations will be loaded, as I have not checked with a multi-store.

Receiving translation in code.
If you are not interested, you can skip this point.

I'll let you know right away that a call to class is in progress "\vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php", which in the "loadCatalogue" function loads the translation, the translation cache is also checked there, if there is no cache, it will re-download the translation.

File: \vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php

To fix errors or perform validation, I have written an example of additional code below that does not perform caching.

All actions are performed in the paragraph: Attention: this can only be done by the developer to check the code execution!!!

File: \vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php

Next in the function "doLoadCatalogue", we receive translations either from the catalog ".xlf" or from the database:

In the line of code: $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));

File: \vendor\symfony\symfony\src\Symfony\Component\Translation\Translator.php

Function "load" performs 2 classes which:

  • \src\PrestaShopBundle\Translation\Loader\SqlTranslationLoader.php - A function call is being made "load". Here a query is executed on a database table "translate". This is a file of prestashop itself written by developers and it overrides the symfony file depending on how it is written in the configuration yml.
  • \vendor\symfony\symfony\src\Symfony\Component\Translation\Loader\XliffFileLoader.php - A function call is being made "load". Here a request is made to the format files ".xlf". They can lie along the path that I wrote above, this is "\app\Resources\translations\en-US\....en-US.xlf" in the old version 1.7 or in the new version 8 on "\translations\en-US\....en-US.xlf"

Now you know a little about the files that work

Solution

The solution lies in one file that is located on the path "\src\PrestaShopBundle\Translation\Loader\SqlTranslationLoader.php" in function "load"

You can also check the code with prestashop 1.7 and prestashop 8. The code will be different because in the new version the developers have changed the way translations are received from templates.

The problem is that it gets its translation from the base translations in the table "translate", but our templates ".tpl", are in the theme "classic", in the directory "/themes/classic/pdf/...", and the translation is loaded only from the basic ones. But we performed the translation on the selected theme.

At the moment you are in the administrative part, in the user order itself, and you want to download the invoice in pdf. But you don't get the current topic in which you performed the translation. Since in fact in the admin panel you are not in any of these themes. But although the theme chosen for the current store "classic". Therefore, it should at least receive the current topic and load these translations. But as you can see, it does not do this.

Below I show the code in the function "load", where you should add the piece of code in the file

File: \src\PrestaShopBundle\Translation\Loader\SqlTranslationLoader.php

Once you change this file, you should now be able to load translations.

created:
21.01.2025
updated:
23.01.2025
author:
admin
Tags
yandex metrika