added plugin to download l10n translation files
authorJaap Jansma <jaap.jansma@civicoop.org>
Tue, 14 Nov 2023 16:16:22 +0000 (17:16 +0100)
committerJaap Jansma <jaap.jansma@civicoop.org>
Tue, 14 Nov 2023 16:16:22 +0000 (17:16 +0100)
setup/plugins/init/Backdrop.civi-setup.php
setup/plugins/init/Drupal.civi-setup.php
setup/plugins/init/Drupal8.civi-setup.php
setup/plugins/init/Standalone.civi-setup.php
setup/plugins/init/WordPress.civi-setup.php
setup/plugins/installFiles/l10nDownload.civi-setup.php [new file with mode: 0644]
setup/src/Setup/Model.php

index 4805d998f46b6c8e3e411a3989207a4217b668cb..c5d832a3edfd9d39b45167ae9894b249e8e0803b 100644 (file)
@@ -57,6 +57,8 @@ if (!defined('CIVI_SETUP')) {
     // $model->paths['civicrm.files']['url'] = $filePublicPath;
     $model->paths['civicrm.files']['path'] = implode(DIRECTORY_SEPARATOR,
       [_backdrop_civisetup_getPublicFiles(), 'civicrm']);
+    $model->paths['civicrm.private']['path'] = implode(DIRECTORY_SEPARATOR,
+      [_backdrop_civisetup_getPrivateFiles(), 'civicrm']);
 
     // Compute templateCompileDir.
     $model->templateCompilePath = implode(DIRECTORY_SEPARATOR,
index c65d12f220ff445e04d0dce18b27240fdf843a73..02bf1bb87f72ab6fbeceb70a48c1604528f3ae36 100644 (file)
@@ -55,6 +55,8 @@ if (!defined('CIVI_SETUP')) {
     // $model->paths['civicrm.files']['url'] = $filePublicPath;
     $model->paths['civicrm.files']['path'] = implode(DIRECTORY_SEPARATOR,
       [_drupal_civisetup_getPublicFiles(), 'civicrm']);
+    $model->paths['civicrm.private']['path'] = implode(DIRECTORY_SEPARATOR,
+      [_drupal_civisetup_getPrivateFiles(), 'civicrm']);
 
     // Compute templateCompileDir.
     $model->templateCompilePath = implode(DIRECTORY_SEPARATOR,
index f81b362a51ec1bf066db5bd2632149e1494f57fb..32b789f2e1f3d060a445db59f1ae509ea6dee9e4 100644 (file)
@@ -59,6 +59,7 @@ if (!defined('CIVI_SETUP')) {
     // Compute general paths
     $model->paths['civicrm.files']['url'] = implode('/', [$model->cmsBaseUrl, \Drupal\Core\StreamWrapper\PublicStream::basePath(), 'civicrm']);
     $model->paths['civicrm.files']['path'] = implode(DIRECTORY_SEPARATOR, [_drupal8_civisetup_getPublicFiles(), 'civicrm']);
+    $model->paths['civicrm.private']['path'] = implode(DIRECTORY_SEPARATOR, [_drupal8_civisetup_getPrivateFiles(), 'civicrm']);
 
     // Compute templateCompileDir.
     $model->templateCompilePath = implode(DIRECTORY_SEPARATOR, [_drupal8_civisetup_getPrivateFiles(), 'civicrm', 'templates_c']);
index 7c0820b4c83e96477f18a5a53e90ed46ecf89693..82c34364f5c72b1c3b08d38cf62bf6fdd40343aa 100644 (file)
@@ -75,7 +75,7 @@ function _standalone_setup_scheme(): string {
     if (empty($projectRootPath)) {
       throw new \RuntimeException("Failed to identify standalone root. (TIP: Set extras.standaloneRoot)");
     }
-
+    $model->paths['civicrm.private']['path'] = implode(DIRECTORY_SEPARATOR, [$projectRootPath, 'data']);
     $model->settingsPath = implode(DIRECTORY_SEPARATOR, [$projectRootPath, 'data', 'civicrm.settings.php']);
     $model->templateCompilePath = implode(DIRECTORY_SEPARATOR, [$projectRootPath, 'data', 'templates_c']);
     // print "\n-------------------------\nSet model values:\n" . json_encode($model->getValues(), JSON_PRETTY_PRINT) . "\n-----------------------------\n";
index f273231d0c8e32f0eb60106b27d432a34c8e90c9..01c46bb313a94938cf165bee3659b18a9bf42713 100644 (file)
@@ -47,6 +47,7 @@ if (!defined('CIVI_SETUP')) {
       $model->settingsPath = $preferredSettingsPath;
     }
 
+    $model->paths['civicrm.private']['path'] = implode(DIRECTORY_SEPARATOR, [$uploadDir['basedir'], 'civicrm']);
     $model->templateCompilePath = implode(DIRECTORY_SEPARATOR, [$uploadDir['basedir'], 'civicrm', 'templates_c']);
 
     // Compute DSN.
diff --git a/setup/plugins/installFiles/l10nDownload.civi-setup.php b/setup/plugins/installFiles/l10nDownload.civi-setup.php
new file mode 100644 (file)
index 0000000..8c7f881
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+/**
+ * @file
+ *
+ * Generate the civicrm.settings.php file.
+ */
+
+if (!defined('CIVI_SETUP')) {
+  exit("Installation plugins must only be loaded by the installer.\n");
+}
+
+/**
+ * Validate the $model.
+ */
+\Civi\Setup::dispatcher()
+  ->addListener('civi.setup.checkRequirements', function(\Civi\Setup\Event\CheckRequirementsEvent $e) {
+    $lang = \Civi\Setup::instance()->getModel()->lang;
+    if ($lang && $lang != 'en_US') {
+      // Use CiviCRM Core directory as a fall back.
+      $baseDir = $e->getModel()->srcPath;
+      if (isset($e->getModel()->paths['civicrm.private']['path'])) {
+        // If the civicrm files directory is set use this as the base path.
+        $baseDir = $e->getModel()->paths['civicrm.private']['path'];
+      }
+      // Set l10n basedir as a define. The GenCode.php tries to locate the l10n files
+      // from this location if other than l10n in the civicrm core directory.
+      if (!isset($e->getModel()->paths['civicrm.l10n']['path'])) {
+        if (\CRM_Utils_Constant::value('CIVICRM_L10N_BASEDIR')) {
+          $e->getModel()->paths['civicrm.l10n']['path'] = \CRM_Utils_Constant::value('CIVICRM_L10N_BASEDIR');
+        }
+        else {
+          $e->getModel()->paths['civicrm.l10n']['path'] = $baseDir . DIRECTORY_SEPARATOR . 'l10n';
+        }
+      }
+      if (getenv('CIVICRM_L10N_BASEDIR') === FALSE) {
+        // Set the environment variable CIVICRM_L10N_BASEDIR which is used in xml/GenCode.php
+        // to create the localized sql files.
+        putenv('CIVICRM_L10N_BASEDIR=' . $e->getModel()->paths['civicrm.l10n']['path']);
+      }
+      if (!is_dir($e->getModel()->paths['civicrm.l10n']['path'])) {
+        \Civi\Setup::log()->info("Creating directory: " . $e->getModel()->paths['civicrm.l10n']['path']);
+        \CRM_Utils_File::createDir($e->getModel()->paths['civicrm.l10n']['path'], FALSE);
+      }
+    }
+  }, \Civi\Setup::PRIORITY_MAIN);
+
+\Civi\Setup::dispatcher()
+  ->addListener('civi.setup.installFiles', function (\Civi\Setup\Event\InstallFilesEvent $e) {
+    $lang = \Civi\Setup::instance()->getModel()->lang;
+    if ($lang && $lang != 'en_US') {
+      $downloadDir = $e->getModel()->paths['civicrm.l10n']['path'] . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'LC_MESSAGES';
+      if (!is_dir($downloadDir)) {
+        \Civi\Setup::log()->info("Creating directory: " . $downloadDir);
+        \CRM_Utils_File::createDir($downloadDir, FALSE);
+      }
+
+      foreach ($e->getModel()->moFiles as $moFile => $url) {
+        $l10DownloadFile = str_replace('[locale]', $lang, $url);
+        \Civi\Setup::log()
+          ->info("Download translation '.$moFile.' from " . $l10DownloadFile . ' into ' . $downloadDir);
+        $client = new \GuzzleHttp\Client();
+        $response = $client->get($l10DownloadFile);
+        if ($response->getStatusCode() == 200) {
+          $success = file_put_contents($downloadDir . DIRECTORY_SEPARATOR . $moFile, $response->getBody());
+          if (!$success) {
+            $e->addError('l10n', 'download', 'Unable to download translation file');
+          }
+        }
+      }
+    }
+  }, \Civi\Setup::PRIORITY_MAIN);
index a8e00e11478c6274aecb02d1ade3d0a0a0a8c2e6..d7c24a8ead49fe5de3d6692e57224405bf70974e 100644 (file)
@@ -3,6 +3,7 @@ namespace Civi\Setup;
 
 /**
  * Class Model
+ *
  * @package Civi\Setup
  *
  * The `Model` defines the main options and inputs that are used to configure
@@ -62,6 +63,10 @@ namespace Civi\Setup;
  *   Keys should be prefixed based on which plugin manages the field.
  *   Values must only be scalars (bool/int/string) and arrays.
  *   Ex: ['opt-in.version-check' => TRUE].
+ * @property array $moFiles
+ *   Open-ended list translations files which should be downloaded. Each entry is a url of an mo-file.
+ *   Provide each entry with en_US langugae code. That code will be replaced with the actual language.
+ *   The default is: ['https://download.civicrm.org/civicrm-l10n-core/mo/en_US/civicrm.mo']
  */
 class Model {
 
@@ -172,6 +177,14 @@ class Model {
       'type' => 'array',
       'value' => array(),
     ));
+    $this->addField(array(
+      'description' => 'l10n download files. The [locale] will be replaced with the selected language.',
+      'name' => 'moFiles',
+      'type' => 'array',
+      'value' => array(
+        'civicrm.mo' => 'https://download.civicrm.org/civicrm-l10n-core/mo/[locale]/civicrm.mo',
+      ),
+    ));
   }
 
   /**