Closed tadhgbowe closed 5 years ago
Update on this (spent all day debugging):
When you clear the translation cache and remove the js-translation.json file from your theme in pub/static/frontend/Magento/[your theme]/[your locale]/js-translation.json it will try to rebuild.
vendor/magento/framework/Translate.php is where all the main work occurs.
What I've found is when it starts to rebuild the js-translation.json (which starts from vendor/magento/module-translation/Model/Json/PreProcessor.php) it comes to vendor/magento/framework/Translate.php and it's not able to evaluate the theme for the config setting:
$this->_config['theme'] = $this->_viewDesign->getDesignTheme()->getId(); returns null.
So in the function _loadThemeTranslation() it fails because $this->_config['theme'] is not set (it's null). It should be returning the theme Id (in my case ID 4).
So there's something the matter with \Magento\Framework\View\DesignInterface $viewDesign in the constructor.
Hope this helps. Is there a quick workaround I wonder?
Thanks Tadhg
Duplicate of https://github.com/magento/magento2/issues/8459
Thom
Hello! Please write your steps and commands for two methods: create language pack and create theme translations.
Hello. Thanks for the feedback. I did see #8459 and #7862 but this issue I believe is an additional problem.
I have also experienced the js-translation.json not building itself properly. Deleting the file does rebuild it but still fails to pick up theme translations (not 100% sure a "bin/magento setup:static-content:deploy..." works on my custom theme).
I have no idea what is meant by "Please write your steps and commands for two methods: create language pack and create theme translations".
I have noticed some quite short and disrespectful comments on GitHub. I can appreciate people's frustration but that isn't going to solve anything. I've given up valuable company time to try highlight this particular problem.
I haven't tested in versions previous to 2.1.3. I will try do this tomorrow.
Thanks T :-)
I see the same problem, but all commands are executed too different. So I asked to write all the steps that you did when deploying the language. I try 2 methods: Method 1: Create a language package same official docs, create 4 files: composer.json, language.hml, registration.php, ru_RU.csv. After make this commands:
cd /var/www/html
rm -rf pub/static/*
rm -rf var/view_preprocessed/*
php /var/www/html/bin/magento cache:clean
bin/magento setup:static-content:deploy --theme Magento/backend en_US
bin/magento setup:static-content:deploy --theme Agestor/default ru_RU
php /var/www/html/bin/magento cache:clean
Method 2: Add file ru_RU.csv to my theme. Path: app/design/frontend/Agestor/default/i18n/. After make this commands:
cd /var/www/html
rm -rf pub/static/*
rm -rf var/view_preprocessed/*
php /var/www/html/bin/magento cache:clean
bin/magento setup:static-content:deploy --theme Magento/backend en_US
bin/magento setup:static-content:deploy --theme Agestor/default ru_RU
php /var/www/html/bin/magento cache:clean
Right?
Two methods same official documentation. Don't works... js-translation.json - empty... Thanks!
Hi Flamestyle / All.
You look to be doing everything correctly. I'm assuming you've set your Locale (In Stores > Configuration > General - Locale) to ru_RU?
However in my case the language pack is being picked up and the js-translation.json is not empty.
My Locale is set to en_GB. The code in my language.xml is set to en_GB.
It's the Theme translations that I want to pick up in my js-translation.json i.e.
app/design/frontend/
AND I THINK I'VE FOUND THE PROBLEM TO MY ISSUE:
In vendor/magento/module-translation/Model/Json/PreProcessor.php
$area = $this->areaList->getArea($areaCode);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
needs to be:
$area = $this->areaList->getArea($areaCode);
$area->load(\Magento\Framework\App\Area::PART_DESIGN);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
It needs to load the design before loading the translations. The above insertion then works for me! So hopefully Magento will pick up on this soon.
When you look at how other areas are loaded for example: vendor/magento/framework/View/DesignLoader.php
You'll see:
$area = $this->_areaList->getArea($this->appState->getAreaCode()); $area->load(\Magento\Framework\App\Area::PART_DESIGN); $area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
The design must be present before the translations are loaded.
Thanks Tadhg
@tadhgbowe, yes General - Locale include ru_RU.
I have one question: you use method 1 or 2? I see you use file en_GB.csv app/design/frontend/[THEME]/default/i18n/en_GB.csv
, but you write more about language.xml
. So you use 2 methods together?
Thank you!
Hello again,
I only created the language pack to see if my js-translation.json could pick up the translations from there. And yes that worked. But I never wanted to create a language pack. I simply wanted to be able to handle everything from my theme.
So in my example I can now remove the language pack. I only used it to prove that js translations worked when in a language pack and not from my theme.
My theme code (in database table theme) is called Training/default. If I run all the following commands it works fine:
rm -rf pub/static/ rm -rf var/view_preprocessed/ php /var/www/html/bin/magento cache:clean bin/magento setup:static-content:deploy --theme Training/default en_GB
Please Note: When you setup a theme you can't just have an i18n folder and nothing else. The static-content:deploy will error saying that the Theme doesn't exist. You have to have some wee/css/source files in there for it to deploy for you.
Thanks Tadhg
Hello, @tadhgbowe! For me prefer to be able to handle everything from my theme too.
Your solution $area->load(\Magento\Framework\App\Area::PART_DESIGN);
works!
Thank you so much!
Hi @Flamestyle and all others who read this.
Obviously this is a small Magento bug and hopefully they will pick up on this soon.
We can't change the core file vendor/magento/module-translation/Model/Json/PreProcessor.php for obvious reasons. For a workaround you can consider using a before Plugin, a Preference or as you'll see below the PreProcessor is already a part of the module-translation di.xml.
In your code/[Vendor]/[Module Name]/etc/di.xml you can use:
<preference for="Magento\Translation\Model\Json\PreProcessor" type="[Vendor]/[Module Name]\Model\Json\PreProcessor"/>
`
`
`
</virtualType>`
All the best, Tadhg
I have the same problem. But this fix doesn't work for me....
@carfantasy Which of the two? Write all your steps so that can analyze.
Hi, This is my issue and also the minicart issue: https://github.com/magento/magento2/issues/8337
I did: composer update php bin/magento setup:upgrade php bin/magento cache:clean php bin/magento setup:di:compile rm -rf pub/static/ rm -rf var/view_preprocessed/ php /var/www/html/bin/magento cache:clean bin/magento setup:static-content:deploy --theme Magento/backend en_US bin/magento setup:static-content:deploy --theme (theme)/default nl_NL php /var/www/html/bin/magento cache:clean
this was not the solution, so I did:
In vendor/magento/module-translation/Model/Json/PreProcessor.php
$area = $this->areaList->getArea($areaCode);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
needs to be:
$area = $this->areaList->getArea($areaCode);
$area->load(\Magento\Framework\App\Area::PART_DESIGN);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
rm -rf pub/static/ rm -rf var/view_preprocessed/ php /var/www/html/bin/magento cache:clean bin/magento setup:static-content:deploy --theme Magento/backend en_US bin/magento setup:static-content:deploy --theme (theme)/default nl_NL php /var/www/html/bin/magento cache:clean
Still not the solution....
Hi @carfantasy,
First you need to check that your Stores > Configuration > General > Locale Options : the Locale for you needs to be set to Dutch (Netherlands).
Secondly you need to make sure that your theme has the correct i18n/nl_NL.csv file. So it should live in a folder like app/design/frontend/[Theme/Vendor namespace]/[Theme e.g. default]/i18n
With all the caches enabled you should only need to refresh the translations cache. You can also simply delete the js-translation.json in your pub/static/frontend/[Theme/Vendor namespace]/[Theme]/nl_NL folder.
When the page loads (for example the basket page), there should be a call to build the js-translation.json.
If it's not generating a js-translation.json at all then I would stick on some debug in vendor/magento/module-translation/Model/Json/PreProcessor.php process function. It should go in here. If it isn't then that's another Magento bug. I've never had a problem it not entering this piece of code.
Please Note: If you have a language pack installed those translations will win over any theme and module translation phrases. If you have any inline translations (DB table translation) these will win over all language pack, theme and module phrases.
My fix was specifically targeted at theme i18n folder translations e.g. [Theme]/i18n/en_GB.csv.
Thanks Tadhg
Hi @tadhgbowe
I did all things you mentioned. When I do Stores > Configuration > General > Locale Options : the Locale for you needs to be set to Dutch (Netherlands). The Java script at the backend crashed. When I put it back in en_US the Java workts but there is missing tekst, My admin login is also set to en_US and still tekst is missing.
I will try do delete the theme files and run setup again. Than set to blank theme. Maybe this solves the issue...
Hi @carfantasy,
If your javascript is crashing that definitely sounds bad. It might be worth trying it out on a brand new install of Magento. I've been used CE 2.1.4. I hope you get it working.
Kind regards, Tadhg
Hi @carfantasy and anybody else tuning in.
I think it's worth noting that when you make translation changes that require a js-translation.json rebuild, to test that the changes have come through okay:
Happy coding (if there's such a thing) :-) Tadhg
I found the problem mostly in Checkout
here too in 2.1.6
Hi. I Try reproduce this bug. (Maybe i use incorrect steps, if yes please correct me)
My steps for reproduce:
AR: Displayed "You have no items in your shopping test message.
Note: It`s correct behaviour for this case.
Hi K7Triton,
Thanks for the message. That's not the correct area I was talking about. I was focusing on the min-cart overlay in the header of the page. When you click on the basket icon (beside the search bar) you get a little overlay to say "You have no items in your shopping cart.".
The area you mention is built from a .pthml template file. They work fine. It's the .html knockout generated translations that get stuck :-)
@tadhgbowe I reproduce this bug. Thanks for your report. Internal ticket MAGETWO-69601 was created for this issue.
Hi @K7Triton.
Great! The fix is below. Sorry I don't have time to follow up myself. What would be even more amazing is to be able to track MAGETWO-69601 somewhere. When the ticket has been resolved it would be great if the original source (i.e. here) could be updated with the Magento release versions that contained the fix. For people who stumble across this post in the future they can then clearly see what version it was fixed in. Right now it's quite fuzzy. Cheers. T
In vendor/magento/module-translation/Model/Json/PreProcessor.php
$area = $this->areaList->getArea($areaCode);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
needs to be:
$area = $this->areaList->getArea($areaCode);
$area->load(\Magento\Framework\App\Area::PART_DESIGN);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
It needs to load the design before loading the translations. The above insertion then works for me! So hopefully Magento will pick up on this soon.
Brilliant!!! I had issue with all translation in the mini-cart and @tadhgbowe fix is working perfectly. Now all my translation added to my theme are working!
Hello, even with your fix @tadhgbowe , my translation does not appear in js-translation.json but it works using __()
in a phtml file
@sebfie Flush your static files and javascript cache. @tadhgbowe solution worked nicely here.
I did everything, it does not work
In fact I think it can work, I did not notice that translations in js-translation.json only appear if it's used by js !
The proposed fix might not work under one specific circumstance:
You compile multiple themes at once using -j 1
on static-deploy command. This is because the area translate data is only loaded once for the first theme deployed, usually adminhtml. This means translations from the theme will be missing from the js-translation.json. Workaround is to not use -j 1
(you may need disable Redis to get it to work due to #9287)
Guys,
Can temp pass this issue by building static content by deploying static content locale by locale
Example your store has two locales: en_US, ja_JP
magento setup:static-content:deploy en_US && magento setup:static-content:deploy ja_JP
Hope this helpful in some case.
@tadhgbowe - I applied the changes you suggested in your June 1st comment and it resolved this issue for me. It looks like this change has not been made in the develop branch yet. Have you considered submitting a PR with this change?
Interestingly, the Magento\Framework\View\DesignLoader::load
method calls those methods in the exact same order:
public function load()
{
$area = $this->_areaList->getArea($this->appState->getAreaCode());
$area->load(\Magento\Framework\App\Area::PART_DESIGN);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
$area->detectDesign($this->_request);
}
I can confirm the fix proposed by @tadhgbowe worked in 2.1.4 and does not work in 2.1.8
Will try on other versions trying to track it down.
@slackerzz that fix is currently not working in 2.1.8+
Current workaround we are using is compile js-translation.json in a dev environment, deploy it to production with the rest of the code and use a RewriteRule to serve the compiled json instead of the empty one in pub.
Hi @barbazul. Thanks for the update. The workaround mentioned from June 1st works fine in 2.1.7. If I get a chance here I will try it out on a fresh 2.1.8. install. Shame. Please Note: if you have multiple locales (i.e. many store views with different languages) the deployment to production will most likely corrupt your js-translation.json file. Problem has been reported on 2.2.0. But it also exists in 2.1.x. Also: Some translations are a result of API calls e.g. basket discount/coupon area. These do not pick up Theme translations either :-( js-translation.json is a real nuisance!
Can confirm this to be broken in 2.1.9 with production mode (works in developer mode).
Hey @tadhgbowe, thanks for the reply. I am working with single language stores. The resulting json file is simply an empty array.
It is worth mentioning that as @danslo explained, this only happens in production mode. The develop mode creates a correct js-translaion.js file but the June 1st fix is still required for Magento to pick up the theme CSV file
Hi @barbazul. Oh it's all fun and games with js-translation.json :-0
I have installed a 2.1.9. CE, created a theme with a i18n/en_GB.csv. I then configured the default store view to use the new theme and also set the default locale to en_GB. And it works in production mode! But I think I got lucky. There's something not right.
When you run the php bin/magento deploy:mode:set production command can you tell me what languages it says after the following section in the console:
Starting deployment of static content Requested languages:
Does it list more than one locale for you?
In my case it was "en_GB, en_US". I can see it created an extra pub/static/frontend/[VENDOR]/default/en_US folder. But my default locale (and only one) on the frontend is "en_GB". So where did the en_US come from?
Well the Admin Panel also has an Interface Locale (click on your login user top right in the Admin Panel and then click on Account Setting). My account was set to en_US.
So that's means it creates a backend & frontend static content for all locales it collects. Oops.
Now why is it not working for you or @danslo? It all hinges on which language is listed first. In my case it was en_GB. So it will correctly generate the js-translation.json file for me. But what it does wrong is that all my locales then get the same translation file. When I checked the en_US folder it contained the same translations for en_GB. That's bad! We encountered this problem with a recent install which had 6 languages. There is a fix which I'll point you too.
But first can you confirm which "Requested languages: XXXXX" are you seeing when going into production mode? If your correct pack is not listed first you could be in trouble.
Thanks, Tadhg
Hi @tadhgbowe! Please tell me, you clear this directories before enabling production mode?
var/cache generated/metadata generated/code var/view_preprocessed pub/static
Hi @Flamestyle. Yes I removed all the folders you mention. The main one in this case is pub/static because that's where the js-translation.js file resides. If you don't delete pub/static/frontend the js-translation.json doesn't get regenerated. We will win! Thanks, Tadhg
Hi All. Just to follow up on my comment a little while ago:
The Admin Panel also has an Interface Locale (click on your login user top right in the Admin Panel and then click on Account Setting). My account was set to en_US.
I changed the above to en_GB. That then matched my frontend default locale of en_GB. I then ran the bin/magento deploy:mode:set production command again (deleted pub/static first). The results are much better. I can only see an en_GB locale across pub/static/adminhtml and pub/static/frontend. Good news.
But if you're frontend is fr_FR for example and you want your Admin Panel to remain in en_US then you will most likely have issues if the the "deployment of static content - Requested languages:" starts with en_US. It would need to list fr_FR first so that it can generate the correct js-translation.json file for you.
I don't know how Magento sorts the order of the "Requested Languages" list. And I also don't know whats causing the original bug. But we do have a workaround which we had to put in place mighty quick. You basically have to override: vendor/magento/module-deploy/Model/Filesystem.php and the function deployStaticContent. $this->getUsedLocales() returns your list of locales. Loop though each locale for the static deploy instead. So you'll end up running something like this for each $locale: $this->functionCallPath . 'setup:static-content:deploy ' . $locale; This will ensure that all js-translation.json files are generated correctly. It's not pretty but it works. We hit the problem at the 11th hour.
I've not had time to find the root cause of the Magento bug.
Thanks, Tadhg
@tadhgbowe Your comment sparked some ideas that I will explore further before sharing to avoid adding to the mess.
In the meantime, here is the command I am running (while on production mode):
[Mwltr\Robo\Magento2\Task\SetupStaticContentDeployTask] Deploy Static Content : /usr/bin/php7.0 bin/magento setup:static-content:deploy --theme=SemExpert/CustomTheme --language es_CL --jobs 16
[Mwltr\Robo\Magento2\Task\SetupStaticContentDeployTask] Running /usr/bin/php7.0 bin/magento setup:static-content:deploy --theme=SemExpert/CustomTheme --language es_CL --jobs 16 in shop
Requested languages: es_CL
Requested areas: frontend
Requested themes: SemExpert/CustomTheme
=== frontend -> SemExpert/CustomTheme -> es_CL ===
When enabling production mode I am not actually running any compilation:
[Mwltr\Robo\Magento2\Task\DeploySetModeTask] activate production mode : /usr/bin/php7.0 bin/magento deploy:mode:set production --skip-compilation
[Mwltr\Robo\Magento2\Task\DeploySetModeTask] Running /usr/bin/php7.0 bin/magento deploy:mode:set production --skip-compilation in shop
Enabled production mode.
[Mwltr\Robo\Magento2\Task\DeploySetModeTask] Done in 0.594s
Hi @barbazul - Okay if you have only one requested language in the static deploy then it should work. I'm not an expert in the deployment process. I traced ours to here:
/vendor/magento/module-deploy/Model/Filesystem.php function deployStaticContent
$output->writeln('Starting deployment of static content');
$cmd = $this->functionCallPath . 'setup:static-content:deploy '
. implode(' ', $this->getUsedLocales());
So as you can see above if you have for example three frontend locales it will run something like
setup:static-content:deploy en_US en_GB fr_FR
In the example above the js_translation.json file will be generated for the en_US locale and the exact same file is deployed to the other locales. en_GB and fr_FR never get a look in. Not good. So we simply tried this:
$locales = $this->getUsedLocales();
foreach ($locales as $locale) {
$cmd = $this->functionCallPath . 'setup:static-content:deploy ' . $locale;
...
We probably need to re-examine our deployment process. But that's where we are at the moment.
/vendor/magento/module-deploy/Model/Filesystem.php function deployStaticContent should ideally separate the frontend and backend deploy process. It needs to avoid deploying unnecessary static content to areas it will never use.
All the best. Tadhg
Hi, For me it almost works fine but i found some problem. When i have 2 Themes : ThemeA -> set on Global and Main Website(default)/ Store / StoreView ThemeB -> set on new Website/ Store / StoreView
Generated js-translation.json is same for BOTH and i can find translations from ThemeA in js-translation genereted for ThemeB. So i tried to switch them and then js-translation.json was same for BOTH but translations from ThemeB were then in ThemeA.
In other words, js-translation.json have translations from CSV file but from theme that is connected with MainWebsite.
Anyone notice that too ? Any solution ?
HI, I use Magento 2.1.9 with method _Create a language package same official docs, create 4 files: composer.json, language.hml, registration.php, ruRU.csv.
my generated js-translation.json has only half translations in dictionary (ru_RU.csv) I have about 220 translations in js-translation.json only 53.
Ticket seems to be already solved on develop in https://github.com/magento/magento2/issues/10673 as MAGETOTWO-72301. Meanwhile following @tadhgbowe response I've got stuck on same static-content:deploy issue in Magneto 2.2.0 making similar workaround:
Magento\Translation\Model\Json\PreProcessor
@@ -81,6 +81,7 @@
}
$area = $this->areaList->getArea($areaCode);
+ $area->load(\Magento\Framework\App\Area::PART_DESIGN);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
Magento\Deploy\Service\DeployTranslationsDictionary;
@@ -35,6 +35,13 @@
private $logger;
/**
+ * Translator
+ *
+ * @var \Magento\Framework\TranslateInterface
+ */
+ protected $_translator;
+
+ /**
* @param JsTranslationConfig $jsTranslationConfig
* @param DeployStaticFile $deployStaticFile
* @param State $state
@@ -44,12 +51,14 @@
JsTranslationConfig $jsTranslationConfig,
DeployStaticFile $deployStaticFile,
State $state,
- LoggerInterface $logger
+ LoggerInterface $logger,
+ \Magento\Framework\TranslateInterface $translator
) {
$this->jsTranslationConfig = $jsTranslationConfig;
$this->deployStaticFile = $deployStaticFile;
$this->state = $state;
$this->logger = $logger;
+ $this->_translator = $translator;
}
/**
@@ -62,6 +71,8 @@
{
try {
$this->state->emulateAreaCode($area, function () use ($area, $theme, $locale) {
+ $this->_translator->loadData($area, true, $theme);
$this->deployStaticFile->deployFile(
$this->jsTranslationConfig->getDictionaryFileName(),
[
Magento\Framework\Translate
@@ -167,12 +167,17 @@
* @param bool $forceReload
* @return $this
*/
- public function loadData($area = null, $forceReload = false)
+ public function loadData($area = null, $forceReload = false, $theme = null)
{
$this->setConfig(
['area' => isset($area) ? $area : $this->_appState->getAreaCode()]
);
+ if($theme){
+ $this->_viewDesign->setDesignTheme($theme);
+ $this->_config['theme'] = $this->_viewDesign->getDesignTheme()->getId();
+ }
+
if (!$forceReload) {
$this->_data = $this->_loadCache();
if ($this->_data !== false) {
Isn't this fixed in 2.2.2?
Hi @jissereitsma!
I've just tested this on Magento Commerce v2.2.2 and it's working fine. The fix was simply to put the following line of code in module-translation Model/Json/PreProcessor.php function process(...):
$this->viewDesign->setDesignTheme($themePath, $areaCode);
Previously the fix was to do this via a before Plugin.
I hope you're seeing the correct results too. All the best, Tadhg
@tadhgbowe Are you saying this is fixed out of the box in Magento 2.2.2 ?? Or are you saying that the proposed fix works in 2.2.2 ?
Hi @barbazul!
v2.2.2 - out of the box fixed :-)
The line of code I showed was what they put to fix it.
Cheers T
For me this issue still exists on Magento 2.2.2 production mode, I am using Valet+ on MacOSX. I changed production mode to developer mode and the issue was fixed. Issue happens on checkout and admin pages. On admin pages, if I open a page which has grid in it, the error is there and due to which grid data is not loaded, but for other pages error happens but page loads normally.
Tried to fix it as suggested by @Tomasz-Silpion but issue cannot be resolved.
Hello. Apologies is reported before. I couldn't see it.
My theme translation file is not appearing in certain locations. For example the mini cart overlay and the cart is empty it says "You have no items in your shopping cart."
In my theme I have the translation "You have no items in your shopping basket.". When I go to the cart page I can see underneath the main content title "You have no items in your shopping basket.". So I know it's working. However when I hover over the mini cart in the header the overlay continues to say "You have no items in your shopping cart."
Tried on 2.1.3 and 2.1.4 community edition.
I have tried a static deploy, deleted the js-translation.json file itself. No joy.
Question: Does the js-translation.json not pick up theme translations?
Preconditions
Locale set to en_GB - English (United Kingdom) Create a new theme. Enable this in the backend via Content > Design - Configuration. Create a i18n/en_GB.csv file with the above translation: "You have no items in your shopping cart.","You have no items in your shopping basket." Refresh block, page and translation caches.
Steps to reproduce
Go to cart page /onepage/cart. In the main content area underneath Shopping Cart I can see my translation "You have no items in your shopping basket.". However the mini-cart overlay doesn't display the new translation.
Expected result
The empty mini-cart overlay should translate to "You have no items in your shopping basket."
Actual result
The empty mini-cart overlay continues to display the core translation "You have no items in your shopping cart."
OTHER NOTES/OBSERVATIONS/TESTS:
Thanks T