Skip to content

Add action to quickly generate a translation file #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev-2.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions packages/core/actions/config/generate-i18n.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/*
This file is part of the eQual framework <http://www.github.com/equalframework/equal>
Some Rights Reserved, eQual framework, 2010-2025
Licensed under GNU GPL 3 license <http://www.gnu.org/licenses/>
*/

[$params, $providers] = eQual::announce([
'description' => "Creates a new i18n translation file, for a given entity.",
'params' => [

'entity' => [
'type' => 'string',
'description' => "Name of the entity.",
'required' => true
],

'lang' => [
'type' => 'string',
'description' => "Code of the desired lang.",
'required' => true
],

'overwrite' => [
'type' => 'boolean',
'description' => "If true the file will be overwritten if it already exists.",
'default' => false
]

],
'access' => [
'visibility' => 'protected',
'groups' => ['admins']
],
'response' => [
'content-type' => 'application/json',
'charset' => 'utf-8',
'accept-origin' => '*'
],
'providers' => ['context', 'orm']
]);

/**
* @var \equal\php\Context $context
* @var \equal\orm\ObjectManager $orm
*/
['context' => $context, 'orm' => $orm] = $providers;

$model = $orm->getModel($params['entity']);
if(!$model) {
throw new Exception("unknown_entity", EQ_ERROR_INVALID_PARAM);
}

$parts = explode("\\", $params['entity']);
$package = array_shift($parts);
$entity = implode("/", $parts);
if(!file_exists("packages/$package")) {
throw new Exception("unknown_package", EQ_ERROR_INVALID_CONFIG);
}
if(!file_exists("packages/$package/i18n")) {
throw new Exception("missing_i18n_dir", EQ_ERROR_INVALID_CONFIG);
}

$lang = $params['lang'];
if(strlen($lang) !== 2) {
throw new Exception("invalid_lang", EQ_ERROR_INVALID_PARAM);
}

$file = EQ_BASEDIR."/packages/$package/i18n/$lang/$entity.json";
// prevent overwriting existing file
if(file_exists($file) && !$params['overwrite']) {
throw new Exception("i18n_already_exists", EQ_ERROR_INVALID_PARAM);
}

$path = dirname($entity);
if(!is_dir(EQ_BASEDIR."/packages/$package/i18n/$lang/$path")) {
mkdir(EQ_BASEDIR."/packages/$package/i18n/$lang/$path", 0777, true);
if(!is_dir(EQ_BASEDIR."/packages/$package/i18n/$lang/$path")) {
throw new Exception("file_access_denied", EQ_ERROR_UNKNOWN);
}
}

$columns = $model::getColumns();

$model_data = [];
foreach($columns as $column => $data) {
if(in_array($data['type'], ['one2many', 'many2many'])) {
continue;
}

$model_data[$column] = [
'label' => '',
'description' => $data['description'] ?? '',
'help' => $data['help'] ?? ''
];

if($data['selection']) {
$selection = [];
foreach($data['selection'] as $key => $sel) {
if(is_string($key)) {
$selection[$key] = $sel;
}
else {
$selection[$sel] = '';
}
}

$model_data[$column]['selection'] = $selection;
}
}

$form_layout = [];
foreach($columns as $column => $data) {
if (!in_array($data['type'], ['one2many', 'many2many'])) {
continue;
}

$form_layout["section.$column"] = ['label' => ''];
}
$view = [
'form.default' => [
'name' => '',
'description' => '',
'layout' => $form_layout
],
'list.default' => [
'name' => '',
'description' => '',
'layout' => []
]
];

$error = [];

$result = [
'name' => $model::getName(),
'plural' => $model::getName().'s',
'description' => $model::getDescription(),
'model' => $model_data,
'view' => $view,
'error' => $error
];

if(!file_put_contents($file, json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES))) {
throw new Exception("file_access_denied", EQ_ERROR_UNKNOWN);
}

$context->httpResponse()
->status(201)
->body($result)
->send();