-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New feature: Improved jstranslator.xml capabilities #56
Conversation
Hey @justinbeaty, the backend/frontend separation is very nice and needed, but I'm not super about the advanced features because if users use a full page cache, it will probably break everything right? |
While I don't have much experience with FPC and Magento, I'm not sure I see how it could break. FPC modules must cache the head block per page, right? Here's the head block's phtml on the frontend: maho/app/design/frontend/base/default/template/page/html/head.phtml Lines 23 to 26 in fddaaf9
So if The only potential case I could see is if the FPC module ignores the loaded layout handles. I.e. maho/app/design/frontend/base/default/layout/weee.xml Lines 33 to 37 in fddaaf9
Is there a specific scenario you can think of that would break things? |
Put another way, for each page on the frontend:
Thus for each page, the same strings are included whether FPC is enabled or not. |
if I remember correctly some blocks are cache site wise, but probably you're right and the head block is page-based anyway (eg: because javascripts are different between category and product page). I was checking Lesti but it does seem to only work on a page-basis. Maybe I was remembering stuff from Turpentine or the Magento EE full page cache module that's not relevant anymore. |
So then I think we can agree the strings must be correct for the It's still possible you could break things with layout handles. As a completely dumb example you add a randomly generated handle to each page load dynamically from your controller file, and somehow expect to add strings based on that. But I think the solution is, don't do that if you're using FPC. Which isn't unreasonable since there are other similar things you can't do when using FPC. |
Also I would assume the head block is page-based since the |
Reverted the debugging commit, however you can apply it with: diff --git a/app/code/core/Mage/Core/Helper/Js.php b/app/code/core/Mage/Core/Helper/Js.php
index 0c23ef6e64..0e50d47ae4 100644
--- a/app/code/core/Mage/Core/Helper/Js.php
+++ b/app/code/core/Mage/Core/Helper/Js.php
@@ -51,7 +51,8 @@ class Mage_Core_Helper_Js extends Mage_Core_Helper_Abstract
*/
public function getTranslateJson()
{
- return Mage::helper('core')->jsonEncode($this->_getTranslateData());
+ // return Mage::helper('core')->jsonEncode($this->_getTranslateData());
+ return json_encode($this->_getTranslateData(), JSON_PRETTY_PRINT);
}
/**
@@ -132,8 +133,8 @@ class Mage_Core_Helper_Js extends Mage_Core_Helper_Abstract
$messageText = is_array($messageText) ? $messageText : [$messageText];
foreach ($messageText as $text) {
$translated = Mage::helper($module)->__($text);
- if ($text && $text !== $translated) {
- $this->_translateData[$text] = $translated;
- }
+ // if ($text && $text !== $translated) {
+ $this->_translateData[$text] = $translated;
+ // }
}
} |
i've added the debug code locally to test and everything works beatifully, I though i've seen something wrong about the configurableswatches string but I was checking the category page instead of the product page, so, again, everything is perfect ❤️ |
I'll put your documentation in the release notes but I'll also have to create a documentation page on the website for this |
Sweet! At some point I'll probably help with the docu too. |
Description
Improved the jstranslator.xml syntax to allow including strings based on the scope, layout handle, and/or script included in
<head>
. Also added support very basicsprintf
like syntax in the JS Translate class.Rationale
I wanted to utilize the jstranslator.xml feature, but it has one very glaring problem: every string is included on every page load on both adminhtml and frontend. So if I add 10 new strings meant only for the admin panel, they'll also be included on the frontend. This could even be a security issue if you include strings that leak information about the server setup or installed modules.
Of course, this only happens if you're on a language pack other than English, so to illustrate the problem comment out the
unset()
line below. This simulates a language pack with all strings defined.maho/app/code/core/Mage/Core/Helper/Js.php
Lines 143 to 146 in fddaaf9
Then view the source of the homepage, and you'll see messages that are only used in the admin panel. I highlighted an example where it could be a security issue.
With this PR the string count goes from 70 -> 41, and will prevent future strings from being included unnecessarily.
Solution
I added some new syntax options to jstranslator.xml, here is what's possible:
Additionally, you can add messages manually with PHP code. You could do this either in a controller or block class, as long as it's before the head block is rendered (i.e. don't do it in a blocks
_toHtml()
method).Of course, you can also add messages in phtml files as before:
Basic sprintf compatibility
I also added a very basic sprintf feature to the JS translate class. It only matches
%s
and%d
tokens, but that's pretty much all that is ever used in Magento translation files. Example:Comments
Most of the still included strings on the frontend are from
validation.js
. I moved some that are clearly admin only toMage/Adminhtml/etc/jstranslator.xml
.I also included a debug commit here that will be reverted. It simply includes all messages without a translation pack and pretty prints the JSON array in the source code.