apeisa Posted July 13, 2020 Share Posted July 13, 2020 Hi Has anyone tried to install (and/or) update languages without using admin UI? I would love to automate that process. 1 Link to comment Share on other sites More sharing options...
bernhard Posted July 13, 2020 Share Posted July 13, 2020 Tried, but failed: https://github.com/BernhardBaumrock/RockMigrations/blob/b15fc176e3fa3736275a943f18882cdd87b19bf3/RockMigrations.module.php#L1701-L1758 Would also love to get a solid solution for that! If you are doing more automation stuff, I'd also love to get help for RockMigrations (thx for your PR yesterday) ? Link to comment Share on other sites More sharing options...
apeisa Posted July 13, 2020 Author Share Posted July 13, 2020 Thanks @bernhard Yep, I guessed this might be tricky one... Things I would like to to do via API is a) install basic language support (this is probably easy one) b) add new languages (this probably too, since language is a page) c) upload json files to a new language (maybe this also, since json files are just files inside the language) If/when I get this working, I will definitely add support to RockMigrations for this. Link to comment Share on other sites More sharing options...
bernhard Posted July 13, 2020 Share Posted July 13, 2020 Just now, apeisa said: a) install basic language support (this is probably easy one) I hope so, but I think I hit some walls there... Seems that PW relies on several page reloads for that to work properly. $modules->refresh() after installing each module was not enough. And I also had problems breaking my site, that's why the restore method exists... If you find a good way to flush pw completely after some changes have been made that might be a good addition to RockMigrations! Thx and good luck ? Link to comment Share on other sites More sharing options...
apeisa Posted July 13, 2020 Author Share Posted July 13, 2020 Ok, it was "fairly" simple. I didn't find a way to use WireUpload to handle unzipping, so had to do it manually. But this seems to work: <?php namespace ProcessWire; $upgrade = function (RockMigrations $rm) { // Install lang support module modules('LanguageSupport'); // get default language $default = languages()->get('default'); // Download lang pack from github to cache folder, unzip and loop and add .json files to language_files field for the language $http = new WireHttp(); $zipUrl = "https://github.com/apeisa/Finnish-ProcessWire/archive/master.zip"; $zipLocal = config()->paths->cache . "language.zip"; $http->download($zipUrl, $zipLocal); $items = files()->unzip($zipLocal, config()->paths->cache); if(count($items)) { foreach($items as $item) { if (strpos($item, ".json") === false) continue; // Yeah, there could be more proper check here... $default->language_files->add(config()->paths->cache . $item); } } // finally save the language $default->save(); // UI update needs still logout/login or modules => refresh modules()->refresh(); }; Not sure what part of that could be part of RM and how? Also - in this level uninstall of LanguageSupport works nicely - I think it might be a lot more complex when multilang fields are involved. 2 Link to comment Share on other sites More sharing options...
teppo Posted July 13, 2020 Share Posted July 13, 2020 (edited) Regarding a) and b) you might perhaps find some useful bits and pieces from here: https://github.com/teppokoivula/VersionControlTests/blob/master/tests/VersionControlTest.php. If I remember correctly, installing language support and adding new languages was indeed relatively simple, though if you want ProcessWire to recognize them right away you need to go through some extra hoops (look for "reloadLanguages" and "LS_init"). In fact reloadLanguages() was initially added for this case. That being said, this is some very old code, so I have no idea if it still runs (properly) ? Edit: looks like you solved it already ? Edited July 13, 2020 by teppo 1 Link to comment Share on other sites More sharing options...
apeisa Posted July 13, 2020 Author Share Posted July 13, 2020 Thanks @teppo Those links you added come handy if the need is add new language instead of update default one. EDIT: I realize that adding new languages was exactly what I asked for in first post. But actually needed only updating the current default. Anyways, I think adding new languages is trivial to this. Link to comment Share on other sites More sharing options...
apeisa Posted July 14, 2020 Author Share Posted July 14, 2020 @bernhard just created a new PR for basic language support (creating new languages and setting translation files for a language). Usage inside migrations is simple: $finnish = $rm->addNewLanguage("finnish", "Finnish"); $rm->setTranslationsToLanguage( "https://github.com/apeisa/Finnish-ProcessWire/archive/master.zip", $finnish->name, ); Or if you just want to set translations to default language: $rm->setTranslationsToLanguage("https://github.com/apeisa/Finnish-ProcessWire/archive/master.zip"); 1 Link to comment Share on other sites More sharing options...
apeisa Posted July 14, 2020 Author Share Posted July 14, 2020 Will add deleteLanguage as well, will add new PR soon(ish). 1 Link to comment Share on other sites More sharing options...
apeisa Posted July 14, 2020 Author Share Posted July 14, 2020 Updated the original PR. Link to comment Share on other sites More sharing options...
kongondo Posted July 14, 2020 Share Posted July 14, 2020 15 hours ago, apeisa said: I didn't find a way to use WireUpload to handle unzipping, $u = new WireUpload('fileinputname'); $u->setExtractArchives(true); But probably not useful in your case since you are downloading from GitHub 1 Link to comment Share on other sites More sharing options...
apeisa Posted July 14, 2020 Author Share Posted July 14, 2020 Thanks @kongondo! Never actually used WireUpload - is there way to "attach" it to certain field in a page? If I do $language->language_files->add($urltozip) it just adds the zip without unzipping it. Link to comment Share on other sites More sharing options...
kongondo Posted July 14, 2020 Share Posted July 14, 2020 (edited) 17 minutes ago, apeisa said: is there way to "attach" it to certain field in a page? I am not sure. If it is a normal file upload it checks and uses PHP's $_FILES (see the method getPhpFiles()). I am wondering though if an ajax upload would work in your case (not sure worth the hassle though if you are not using Ajax). See getPhpFilesAjax(). Class docs https://processwire.com/api/ref/wire-upload/ I don't think setName() would work either. Edited July 14, 2020 by kongondo More info. Link to comment Share on other sites More sharing options...
apeisa Posted July 14, 2020 Author Share Posted July 14, 2020 Ajax upload is not possibility here. I tried to find a cleaner solution than downloading, extracting and filtering json files myself, but final method wasn't that messy so I think it goes (just worried about the edge cases though). Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now