bytesource
Members-
Posts
220 -
Joined
-
Last visited
Everything posted by bytesource
-
I need to export all translatable fields (FieldtypeTextLanguage, FieldtypeTextareaLanguage), translate them, and then import the translated text. For normal page fields this is relatively easy. The same is true for repeaters, as a repeater is just a PageArray holding pages. So, in order to import the translated text into these fields, I just need the page id and field name for identification. However, as far as I know, FieldtypeImage is not represented as an array of pages (holding images). I still could iterate through all images to retrieve the image descriptions, but then how could I find the correct image later when I want to import the translated image description? Does a single image carry some kind of ID that I could use to retrieve it via the API? Just for completeness, here is a quick and dirty proof of concept of what I am trying to accomplish: <?php // bootstrap Processwire include("./index.php"); function getURL(Page $p) { $id = $p->id; echo $id . ", " . $p->name; echo "</br>"; } function getLanguageValues (Page $p) { $pageFields = $p->fields; // iterate through all fields of a page foreach ($pageFields as $field) { $type = $field->type; // only select a field if it is translatable if ($type == "FieldtypeTextLanguage" || $type == "FieldtypeTextareaLanguage") { convertToCSV($p, $field); } elseif ($type == "FieldtypeRepeater" || $type == "FieldtypePageTable") { $fieldName = $field->name; // Repeater and and PageTable fields are PageArrays holding pages. foreach($p->$fieldName as $subpage) { getLanguageValues($subpage); } } elseif ($type == "FieldtypeImage") { $fieldName = $field->name; foreach ($p->$fieldName as $img) { // ? } } } } function convertToCSV ($p, $field) { $defaultLanguage = wire('languages')->get('default'); $defaultText = $p->getLanguageValue($defaultLanguage, $field); // only translate if the field value for the default language is not empty if (!empty($defaultText)) { $id = $p->id . "-" . $field->name; echo $id . ", " . $defaultText; echo "</br>"; } } function getValues ($p) { getURL($p); getLanguageValues($p); } // =============================================================== $p = wire('pages')->get("/"); $lang = wire('languages')->get("de"); getValues($p); // Sample output [id (page id - field name), text] // [...] // 1-portfolio_slogan, Our Strengths // 1012-subject, Advanced Drilling Equipment // field belonging to a repeater // [...] // Simple test to check how re-importing translated values might work // http://processwire.com/api/multi-language-support/multi-language-fields/#multi-language-field-values // set 'subject' textfield value from repeater to target language $targetPage = wire('pages')->get(1012); $deutsch = "Bohrgeräte"; $targetPage->subject->setLanguageValue($lang, $deutsch); $targetPage->save(); ?> Cheers, Stefan
-
Thanks for this fantastic information! I was always under the impression I had to manually upload the files I wanted to be cached on different servers around the globe. Tomorrow I will start reading through the docs. Cheers, Stefan
-
Access $page and $pages Variables from inside a Javascript File?
bytesource replied to bytesource's topic in API & Templates
@Soma I just wanted to let you know that thanks to your suggestions I was able to move all my javascript code from the page bottom to an external script file. Thanks again for your help! Cheers, Stefan -
Joss, You mention that you also used Cloudfare to speed up load times. Can you tell a bit more about how do you manage/synchronize the files stored in assets and stored on Cloudfare? My has much longer load times outside of Europe, where the server is located, so I thought about adding a CDN. However, I am just not sure about the steps I need to take. Cheers,
-
Access $page and $pages Variables from inside a Javascript File?
bytesource replied to bytesource's topic in API & Templates
This truly is an elegant solution! To bad I won't be able to implement it until after the weekend, but I am really looking forward to it. Thanks a lot for your help! -
Hi, I have over 200 lines of javascript code at the bottom of every page that does not get compressed and therefore adds to the page load. I'd therefore like to add this code to all the other javascript code that is located at /site/templates/styles/js/ and output as a single minified file via AIOM at /site/assets/aiom/ The problem is that the javascript code at the bottom of every page includes calls to the Processwire API such as: <?php // URLs for $.ajax() $subscribe = $pages->get("template=some-template, include=hidden")->url; $productLinks = $pages->get("template=another-template, include=hidden")->url; ?> <script> // [...] $.ajax({ url: "<?php echo $productLinks ?>", type: "POST", data: { category: category, id: <?= $page->id ?> } // [...] $(.some-class).html("<p><?= _("This text needs to be translated") ?></p>"); </script> As far as I know, the above API calls can only be made from within a template, so I am afraid there is no straightforward way to bundle this code with my other javascript code. Still, I highly appreciate any ideas of how one might go around this problem. Maybe is is possible to make the API calls in the template, and then somehow pass the return values to the javascript file. Cheers, Stefan
-
Thanks for this suggestion. I read the introduction about the ProFields Table field type, but to be fair, I could not see how to use them to my advantage in this case. I am looking for a way to do the following via the API: Get all translatable files of the 'template' folder and its subfolders. Extract all translatable sentences Do an automatic translation for language x (I already solved this one for other field types, like FieldtypeTextareaLanguage) Set and save the translation Alternatively, exporting all the sentences as JSON would help, too. Then I could just export the data via code, add the translations, and import the data again. I am pretty sure the above is possible, I just don't know where to look for the relevant information. Cheers,
-
Hi all, I am planning to have the initial translation of a site of mine done automatically via Bing's translation API, and I therefore need to retrieve all translatable static text, like <?php echo _("Some static dummy text") ?> and set the language value. Is there a way to do this using the Processwire API, or do I need to manually export the translation file? Cheers, Stefan
-
How to Iterate Through All Fields of a Repeater?
bytesource replied to bytesource's topic in API & Templates
@Wanze I just wanted to let you know that the code you posted is working as expected. Besides, it seems FieldtypePageTable also is a PageArray containing pages, so I added this one, too: <?php // bootstrap Processwire include("./index.php"); echo "Translation starts now..."; function setLanguageValue (Page $p, $language) { $pageFields = $p->fields; // iterate through all fields of a page foreach ($pageFields as $field) { $type = $field->type; // only select a field if it is translatable if ($type == "FieldtypeTextLanguage" || $type == "FieldtypeTextareaLanguage") { // only translate if the field value for the default language is not empty $defaultValue = $p->getLanguageValue(wire('languages')->get('default'), $field); if (!empty($defaultValue)) { $p->setLanguageValue($language, $field->name, "some dummy text"); } } elseif ($type == "FieldtypeRepeater" || $type == "FieldtypePageTable") { $fieldName = $field->name; // Repeater and and PageTable fields are PageArrays holding pages. foreach($p->$fieldName as $subpage) { setLanguageValue($subpage, $language); } } } $p->save(); } $p = wire('pages')->get("/"); $russian = wire('languages')->get("ru"); setLanguageValue($p, $russian); Thanks again for your help! Cheers, Stefan -
How to Iterate Through All Fields of a Repeater?
bytesource replied to bytesource's topic in API & Templates
@Wanze Wow, this is much more than I expected! Thank you so much! I will test your code thoroughly, hopefully over the weekend, and then come back to you. Cheers, Stefan -
Hi all, I have been trying to get the fields of a given page, and that page can also contains repeaters - which have their own set of fields. Retrieving all fields was relatively easy, but I am not sure how to iterate through the fields of a repeater. As the below code is meant to be expanded to more than just one page, I also don't know the names of the repeaters fields. This is what I have come up with so far: <?php // bootstrap Processwire include("./index.php"); $languagePage = wire('pages')->get("/"); $pageFields = $languagePage->fields; $russian = wire('languages')->get("ru"); // iterate through all fields of a page foreach ($pageFields as $field) { $type = $field->type; // only select a field if it is translatable if ($type == "FieldtypeTextLanguage" || $type == "FieldtypeTextareaLanguage") { $languagePage->setLanguageValue($russian, $field->name, "some dummy text"); } elseif ($type == "FieldtypeRepeater") { // How to I itereate through all fields of a repeater? foreach($field->fields as $subfield) { $type = $subfield->type; if ($type == "FieldtypeTextLanguage" || $type == "FieldtypeTextareaLanguage") { $languagePage->setLanguageValue($russian, $subfield->name, "some dummy text"); } } } $languagePage->save(); The problem is the code inside the elseif statement. I am aware that a repeater can contain other repeaters, which calls for a recursive approach, but before I tackle this problem I wanted to be sure everything else is working. Any suggestions are highly welcome! Cheers, Stefan
-
Is There a Recommended Quality Setting for JPEG Images?
bytesource replied to bytesource's topic in General Support
@pwired That is a good idea! Thank you very much! Cheers, Stefan -
Is There a Recommended Quality Setting for JPEG Images?
bytesource replied to bytesource's topic in General Support
Hi Horst, Thank you very much for this valuable information! I will try to find the 'perfect' quality setting within the 80 to 90 range you mentioned. Cheers, Stefan -
Is There a Recommended Quality Setting for JPEG Images?
bytesource posted a topic in General Support
Hi, For my website, currently located at http://staging.sovonex.com, I have been using jpeg images at the following quality: Cropped in GIMP and exported using the "Save for Web" plugin with a quality of 85% Most images are further resized in Processwire with an image quality of 90% and sharpening set to 'soft'. In order reduce page load times (*), I was wondering if setting the quality below 90 would be appropriate. The problem is that I have not the eye of a photographer, and some of the images on the site are not high quality shots to begin with, so I have problems figuring out the best quality setting. With 'best' meaning in this this case, the lowest quality at which the image still looks fine. On the web have found posts stating that setting the quality to 85 was the best choice most of the time. But then again I already saved the original with a quality of 90, so I am not sure if this "rule" was still applicable in my case. (*) I am currently trying out different options for serving adaptive images, that is serving images with sizes based on the size of the screen they are viewed on. So here I only want to ask about the recommended image quality. Cheers, Stefan -
Oliver's Responsive Images Plugin: Some Questions
bytesource replied to bytesource's topic in Dev Talk
Some more observations: I get different behaviours on different devices: Desktop, Ubuntu, Firefox or Chrome: The large gallery images don't load. Mobile, Android, Firefox: The large gallery images load on all pages tested (except for one single image, which did not load). iPad, Safari: The large gallery images don't load. In addition, the thumbnail images first appear and then disappear again, leaving the gallery without images. Cheers, Stefan -
Oliver's Responsive Images Plugin: Some Questions
bytesource replied to bytesource's topic in Dev Talk
Oliver, I am anxious to try out the new version of your plugin! Also thanks for mentioning the typos. They should be all fixed by now. Cheers, Stefan -
Oliver's Responsive Images Plugin: Some Questions
bytesource replied to bytesource's topic in Dev Talk
Oliver, I finally managed to update the site. I hope you find the changes useful. Cheers, Stefan -
Oliver's Responsive Images Plugin: Some Questions
bytesource replied to bytesource's topic in Dev Talk
Oliver, Unfortunately reaching web pages outside China has become so slow that this will take some time. So I guess, the updated version won't be online until tomorrow. Cheers, Stefan -
Oliver's Responsive Images Plugin: Some Questions
bytesource replied to bytesource's topic in Dev Talk
Dear Oliver, Thanks for taking a look at my site. I wrapped the initialization call inside $(function() {...}). I also used $(document).ready(function () {...}), but to no avail as getPath still does not get called. So, assuming the plugin is initialized correctly, do you see any reason of why getPath can be called on the console, but not on the server? Also, thanks for you tip about setting the image width and height! Cheers, Stefan -
Oliver's Responsive Images Plugin: Some Questions
bytesource replied to bytesource's topic in Dev Talk
Oliver, Neither the original url nor the return value of getPath is logged when clicking on a thumbnail (= calling the change callback). This is why I suspect getPath does not get called in this case. I uploaded the latest changes as requested. One of the galleries on this site can be found at: http://staging.sovonex.com/top-drive-services/ The non-minified javascript file is located at /site/assets/aiom/. I really appreciate your support and patience! Cheers, Stefan -
Oliver's Responsive Images Plugin: Some Questions
bytesource replied to bytesource's topic in Dev Talk
When I set the initial value of newPath to the string "test", the final value after calling responsiveImages() is still null. So it seems that at least something does happen inside repsonsiveImages(). -
Oliver's Responsive Images Plugin: Some Questions
bytesource replied to bytesource's topic in Dev Talk
Oliver, I assume you mean this function: // Generates new URL name from original getURL: function(original_url, params, debug) { console.log("getURL: input url: " + original_url); var swidth = params.swidth || 0, pwidth = params.pwidth || 0, pxratio = params.pxratio || 1; var url = original_url.replace(/(\.[a-z]+)$/, '.s' + swidth + '.p' + pwidth + '.r' + pxratio + '$1'); console.log("getURL: output url: " + url); return url; } Could it be that getURL never gets called inside the change callback? I logged the original url as well as the return value of this function, which output the urls - original and modified - of all images on the page, like this: However, when I clicked on a thumbnail to trigger the change callback, nothing is output from getURL. Cheers, Stefan -
Oliver's Responsive Images Plugin: Some Questions
bytesource replied to bytesource's topic in Dev Talk
Oliver, That's odd. I logged the variables, and null is returned for newPath: $('.product-gallery').magnificPopup({ // ... callbacks: { change: function() { var target = this.currItem; var pathname = target.src; console.log("pathname: " + pathname); var newPath = $('body').responsiveImages( 'getURL', pathname, { swidth: screen.width, pwidth: $(window).width(), pxratio: window.devicePixelRatio || 1 }); console.log("newPath: " + newPath); pathname = newPath; } } }); Console output: Still, somewhere I must have made a mistake. I just can't figure out where. Cheers, Stefan