Jump to content

Possibility to install and update language packs without admin interface (cli, api etc)


apeisa
 Share

Recommended Posts

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

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

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.

  • Like 2
Link to comment
Share on other sites

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 by teppo
  • Like 1
Link to comment
Share on other sites

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

@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");

 

  • Thanks 1
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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 by kongondo
More info.
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...