Skip to content

Commit

Permalink
PHRAS-3967: Translator - Move translator configuration in config/conf…
Browse files Browse the repository at this point in the history
…iguration.yml (#4428)

* move translator configuration

* some catch
  • Loading branch information
aynsix authored Dec 4, 2023
1 parent f0baae9 commit fa8377e
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 50 deletions.
41 changes: 41 additions & 0 deletions config/configuration.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,44 @@ feedback-report:
method: "prepend"
delimiter: "\n"
value: 'Vote initated on {{ vote.created }} by {{ initiator ? initiator.getEmail() : "?" }} expired {{ vote.expired }} : {{ vote.voters_count }} participants, {{ vote.votes_unvoted }} unvoted, {{ vote.votes_no }} "no", {{ vote.votes_yes}} "yes".'


translator:
jobs:
#
# - translate EN keywords to FR and EN
# nb: since we clean the destination fields, we MUST (re)write EN to EN
# - add country to keywords, both FR/EN
keywords:
active: true
databox: my_databox
if_collection: to_translate
if_status: x1xxxx
actions:

KeywordsENtoFREN:
active: true
source_field: KeywordsEN
source_lng: en
destination_fields:
- fr:keywordsFR
- en:keywordsEN
cleanup_source: if_translated
# action 1 cleans the destination fields
cleanup_destination: true

CountryENtoKeywordsFREN:
active: true
source_field: CountryEN
source_lng: en
# add translated country to the keywords
destination_fields:
- fr:keywordsFR
- en:keywordsEN
cleanup_source: if_translated
# action 2 must NOT erase what action 1 did
cleanup_destination: false

# end of job : change coll status
set_status: 10xxxx
set_collection: online
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

Class GlobalConfiguration
{
const CONFIG_DIR = "/config/translator/";
const CONFIG_FILE = "configuration.yml";
const REPORT_FORMAT_ALL = "all";
const REPORT_FORMAT_TRANSLATED = "translated";

Expand Down Expand Up @@ -105,12 +103,9 @@ private function __construct($appBox, Unicode $unicode, $global_conf, bool $dryR
public static function create(appbox $appBox, Unicode $unicode, string $root, bool $dryRun, string $reportFormat, OutputInterface $output): GlobalConfiguration
{
try {
$config_file = ($config_dir = $root . self::CONFIG_DIR) . self::CONFIG_FILE;
$app = $appBox->getPhraseApplication();

@mkdir($config_dir, 0777, true);

$config = Yaml::parse(file_get_contents($config_file));
return new self($appBox, $unicode, $config['translator'], $dryRun, $reportFormat, $output);
return new self($appBox, $unicode, $app['conf']->get(['translator']), $dryRun, $reportFormat, $output);
}
catch (\Exception $e) {
throw new ConfigurationException(sprintf("missing or bad configuration (%s)", $e->getMessage()));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Translator play __jobs__ one after one, each __job__ can define his own settings
Jobs and settings are declared in a configuration file (yml):

```yaml
# <phraseanet>/config/translator/configuration.yml

translator:
jobs:
Expand Down
96 changes: 96 additions & 0 deletions lib/classes/patch/418RC8PHRAS3967.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Core\Configuration\PropertyAccess;
use Symfony\Component\Yaml\Yaml;

class patch_418RC8PHRAS3967 implements patchInterface
{
/** @var string */
private $release = '4.1.8-rc8';

/** @var array */
private $concern = [base::APPLICATION_BOX];

/**
* {@inheritdoc}
*/
public function get_release()
{
return $this->release;
}

/**
* {@inheritdoc}
*/
public function getDoctrineMigrations()
{
return [];
}

/**
* {@inheritdoc}
*/
public function require_all_upgrades()
{
return false;
}

/**
* {@inheritdoc}
*/
public function concern()
{
return $this->concern;
}

/**
* {@inheritdoc}
*/
public function apply(base $base, Application $app)
{
if ($base->get_base_type() === base::DATA_BOX) {
$this->patch_databox($base, $app);
} elseif ($base->get_base_type() === base::APPLICATION_BOX) {
$this->patch_appbox($base, $app);
}

return true;
}

private function patch_databox(databox $databox, Application $app)
{
}

private function patch_appbox(base $appbox, Application $app)
{
/** @var PropertyAccess $conf */
$conf = $app['conf'];

if (!$conf->has(['translator'])) {
try {
// retrive value for the old conf file if possible
$config_file = ($config_dir = $app['root.path'] . "/config/translator/") . "configuration.yml";

if (!file_exists($config_file)) {
throw new Exception("file not found");
}

$config_file = file_get_contents($config_file);
if ( $config_file == false) {
throw new Exception("can not get file content");
}

// throw ParseException if not a valid yaml
$oldConf = Yaml::parse($config_file);

$conf->set(['translator'], $oldConf['translator']);
} catch (\Exception $e) {
// if missing configuration
$conf->set(['translator'], ['jobs' => ['keywords' => []]]);
} catch(\Throwable $e) {
$conf->set(['translator'], ['jobs' => ['keywords' => []]]);
}
}
}
}
40 changes: 40 additions & 0 deletions lib/conf.d/configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,43 @@ feedback-report:
method: prepend
delimiter: "\n"
value: 'Vote initated on {{ vote.created }} by {{ initiator ? initiator.getEmail() : "?" }} expired {{ vote.expired }} : {{ vote.voters_count }} participants, {{ vote.votes_unvoted }} unvoted, {{ vote.votes_no }} "no", {{ vote.votes_yes}} "yes".'

translator:
jobs:
#
# - translate EN keywords to FR and EN
# nb: since we clean the destination fields, we MUST (re)write EN to EN
# - add country to keywords, both FR/EN
keywords:
active: false
databox: my_databox
if_collection: to_translate
if_status: x1xxxx
actions:

KeywordsENtoFREN:
active: true
source_field: KeywordsEN
source_lng: en
destination_fields:
- fr:keywordsFR
- en:keywordsEN
cleanup_source: if_translated
# action 1 cleans the destination fields
cleanup_destination: true

CountryENtoKeywordsFREN:
active: true
source_field: CountryEN
source_lng: en
# add translated country to the keywords
destination_fields:
- fr:keywordsFR
- en:keywordsEN
cleanup_source: if_translated
# action 2 must NOT erase what action 1 did
cleanup_destination: false

# end of job : change coll status
set_status: 10xxxx
set_collection: online

0 comments on commit fa8377e

Please sign in to comment.