Bug inhérent à Magento 2. Les chaînes non-traduites sont dasn des fichiers javaScript. Un fichier js-translation.json
contenant les traductions nécessaires est généré en front au chargement de la page, mais il est incomplet!
The cause of issue is: Sometime file js-translation.json is loaded slower or later than other javascript file (I assume file a.js contain some texts need to translate) which contains some texts need to translate so at this time translated texts still not push to storage. Source: Magento 2 javascript translation sometime translate sometime not work – Why and how to fix?.
Voir ici:
- [Ouvert][Remonté le 10 oct 2018] magento generate a broken js-translation.json file while deploying locale
- [Ouvert][Remonté le 5 juil 2018] Problem with the translations from javascript in Magento 2.2.2
- [Fermé][Remonté le 12 déc 2018] Strings not translated in all email templates, keep curly braces around string
- [Fermé][Remonté le 9 juil 2018] 2.2.4 to 2.2.5 js-translation.json gets messed up
- [Fermé (non reproductible)][Remonté le 31 mar 2017] js-translation.json generation incomplete.
Attention: les solutions 1 à 4 ont été testées alors qu’aucun language pack pour le core de Magento 2 n’avait été installé. Il se trouve que les clés de traduction n’étaient présentes qu’en anglais dans le projet. Le front ne pouvait de ce fait pas afficher de libellés en français. Il est impératif d’avoir installé un language pack au préalable, avant de procéder à des tests sur les problèmes de build du fichier js-translation.json
.
Solution #1 (testée Magento 2.3.4, non fonctionnelle)
Dans la DB, virer les rangées de inline translation qui sont importées dans la table core_config_data
:
1 2 |
delete from core_config_data where path like 'dev/translate_inline/active'; delete from core_config_data where path like 'dev/translate_inline/active_admin'; |
Puis:
- disable the inline translation
- rm your pub/static/frontend directory
- redeploy the frontend
Solution #2 (non testée)
Il faut impérativement qu’on ait un environnement où les traductions fonctionnent pour mettre en place cette solution: Magento 2 – Translations : how does js-translation.json get populated? Mine is empty!
Solution #3 (testée Magento 2.3.4, non fonctionnelle)
Magento 2 Knockout Translation Bug
Solution #4 (non testée)
js-translation.json fails to generated
Solution #5 (testée non fonctionnelle Magento 2.3.3, 2.3.4)
Problème de traductions dans Magento 2: les traductions ne fonctionnent pas.
Packs de langues (traductions)
Installer mageplazza
Créer un token depuis Github
- Vous logguer sur votre compte GitHub
- Settings > Developer settings > Personal access tokens
- Button « Generate new token »
- Cocher la case read:packages
- Copier-coller le token généré. Il vous sera demandé au
composer require
1 2 3 4 |
$ composer require mageplaza/magento-2-french-language-pack $ php -d memory_limit=-1 bin/magento setup:static-content:deploy -f fr_FR $ n98-magerun2 indexer:reindex $ n98-magerun2 cache:flush |
Outils
- AOEpeople/grunt-magento-translation-to-json – Parses magento translation csv files and builds json for required keys (including fallback if no translation file available).
ATTENTION: Magento 2 est très buggué au niveau des localisations traductions, notamment lorsque celles-ci reposent sur JavaScript. La solution ci-dessous permet de contourner le problème mais n’est pas propre. Dans le fichier app/design/frontend/MyVendor/myTheme/Magento_Theme/templates/messages.phtml
, rajouter à la fin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<script type="text/javascript"> require([ 'jquery' ], function(jQuery){ (function($) { (function(win) { 'use strict'; var listeners = [], doc = win.document, MutationObserver = win.MutationObserver || win.WebKitMutationObserver, observer; function ready(selector, fn) { // Store the selector and callback to be monitored listeners.push({ selector: selector, fn: fn }); if (!observer) { // Watch for changes in the document observer = new MutationObserver(check); observer.observe(doc.documentElement, { childList: true, subtree: true }); } // Check if the element is currently in the DOM check(); } function check() { // Check the DOM for elements matching a stored selector for (var i = 0, len = listeners.length, listener, elements; i < len; i++) { listener = listeners[i]; // Query for elements matching the specified selector elements = doc.querySelectorAll(listener.selector); for (var j = 0, jLen = elements.length, element; j < jLen; j++) { element = elements[j]; // Make sure the callback isn't invoked with the // same element more than once if (!element.ready) { element.ready = true; // Invoke the callback with the element listener.fn.call(element, element); } } } } // Expose `ready` win.ready = ready; })(this); // Select the node that will be observed for mutations and translate english text to page lang const htmlLang = $('html').attr('lang'), pageMainID = $('.page-main').attr('id'), targetNode = document.getElementById(pageMainID); ready('.text[data-bind="html: message.text"]', function(targetNode) { var thisObj = $(this), alertText = thisObj.text(); if (alertText === 'The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later.') { if (htmlLang === 'fr') { thisObj.text('La connexion au compte était incorrecte ou votre compte est temporairement désactivé. Veuillez patienter et réessayer plus tard.'); } } }); })(jQuery); }); </script> |
Axes d’amélioration: améliorer le code en le séparant en 2 plugins. Les mutation observers et le traducteur.