MDL-69582 tool_customlang: add export langstring feature

This commit is contained in:
Thomas Wedekind 2020-08-24 15:42:34 +02:00 committed by Ferran Recio
parent 757f832ffc
commit 1f4a5e485e
10 changed files with 249 additions and 5 deletions

View File

@ -0,0 +1,68 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Creates Formular for customlang file export
*
* @package tool_customlang
* @copyright 2020 Thomas Wedekind <Thomas.Wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\form;
use tool_customlang_utils;
/**
* Formular for customlang file export
*
* @copyright 2020 Thomas Wedekind <Thomas.Wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class export extends \moodleform {
/**
* Add elements to form
*/
public function definition() {
$lng = $this->_customdata['lng'];
$mform = $this->_form;
$langdir = tool_customlang_utils::get_localpack_location($lng);
// The export button only appears if a local lang is present.
if (!check_dir_exists($langdir) || !count(glob("$langdir/*"))) {
print_error('nolocallang', 'tool_customlang');
}
$langfiles = scandir($langdir);
$fileoptions = [];
foreach ($langfiles as $file) {
if (substr($file, 0, 1) != '.') {
$fileoptions[$file] = $file;
}
}
$mform->addElement('hidden', 'lng', $lng);
$mform->setType('lng', PARAM_LANG);
$select = $mform->addElement('select', 'files', get_string('exportfilter', 'tool_customlang'), $fileoptions);
$select->setMultiple(true);
$mform->addRule('files', get_string('required'), 'required', null, 'client');
$mform->setDefault('files', $fileoptions);
$this->add_action_buttons(true, get_string('export', 'tool_customlang'));
}
}

View File

@ -46,5 +46,14 @@ $capabilities = array(
'manager' => CAP_ALLOW
),
),
/* allows the user to export the current language customization */
'tool/customlang:export' => array(
'riskbitmask' => RISK_CONFIG,
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
),
),
);

View File

@ -0,0 +1,68 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Performs the custom lang export.
*
* @package tool_customlang
* @subpackage customlang
* @copyright 2020 Thomas Wedekind <thomas.wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php');
require_once($CFG->libdir.'/adminlib.php');
global $PAGE, $CFG;
require_login(SITEID, false);
require_capability('tool/customlang:export', context_system::instance());
$lng = required_param('lng', PARAM_LANG);
admin_externalpage_setup('toolcustomlang', '', null,
new moodle_url('/admin/tool/customlang/import.php', ['lng' => $lng]));
$form = new \tool_customlang\form\export(null, ['lng' => $lng]);
if ($form->is_cancelled()) {
redirect('index.php');
die();
} else if ($formdata = $form->get_data()) {
$tempzip = tempnam($CFG->tempdir . '/', 'tool_customlang_export');
$filelist = [];
foreach ($formdata->files as $file) {
$filepath = tool_customlang_utils::get_localpack_location($lng). '/' . $file;
if (file_exists($filepath)) {
$filelist[$file] = $filepath;
}
}
$zipper = new zip_packer();
if (!empty($filelist) && $zipper->archive_to_pathname($filelist, $tempzip)) {
// Filename include the lang name so the file can be imported with automatic language detection.
send_temp_file($tempzip, "customlang_$lng.zip");
die();
}
}
$output = $PAGE->get_renderer('tool_customlang');
echo $output->header();
echo $output->heading(get_string('pluginname', 'tool_customlang'));
$form->display();
echo $OUTPUT->footer();

View File

@ -32,7 +32,6 @@ class tool_customlang_filter_form extends moodleform {
function definition() {
$mform = $this->_form;
$current = $this->_customdata['current'];
$mform->addElement('header', 'filtersettings', get_string('filter', 'tool_customlang'));

View File

@ -35,6 +35,7 @@ require_capability('tool/customlang:view', context_system::instance());
$action = optional_param('action', '', PARAM_ALPHA);
$confirm = optional_param('confirm', false, PARAM_BOOL);
$lng = optional_param('lng', '', PARAM_LANG);
$next = optional_param('next', 'edit', PARAM_ALPHA);
admin_externalpage_setup('toolcustomlang');
$langs = get_string_manager()->get_list_of_translations();
@ -59,11 +60,10 @@ if ($action === 'checkout') {
raise_memory_limit(MEMORY_EXTRA);
tool_customlang_utils::checkout($lng, $progressbar);
echo $output->continue_button(new moodle_url('/admin/tool/customlang/edit.php', array('lng' => $lng)), 'get');
echo $output->continue_button(new moodle_url("/admin/tool/customlang/{$next}.php", array('lng' => $lng)), 'get');
echo $output->footer();
exit;
}
if ($action === 'checkin') {
require_sesskey();
require_capability('tool/customlang:edit', context_system::instance());
@ -133,6 +133,16 @@ if (has_capability('tool/customlang:edit', context_system::instance())) {
);
}
}
if (has_capability('tool/customlang:export', context_system::instance())) {
$langdir = tool_customlang_utils::get_localpack_location($lng);
if (check_dir_exists(dirname($langdir)) && count(glob("$langdir/*"))) {
$menu['export'] = [
'title' => get_string('export', 'tool_customlang'),
'url' => new moodle_url("/admin/tool/customlang/export.php", ['lng' => $lng]),
'method' => 'post',
];
}
}
echo $output->render(new tool_customlang_menu($menu));
echo $output->footer();

View File

@ -30,9 +30,19 @@ $string['checkin'] = 'Save strings to language pack';
$string['checkout'] = 'Open language pack for editing';
$string['checkoutdone'] = 'Language pack loaded';
$string['checkoutinprogress'] = 'Loading language pack';
$string['cliexportfileexists'] = 'File for {$a->lang} already exists, skipping. If you want to overwrite add the --override=true option.';
$string['cliexportheading'] = 'Starting to export lang files.';
$string['cliexportnofilefoundforlang'] = 'No file found to export. Skipping export for this language.';
$string['cliexportfilenotfoundforcomponent'] = 'File {$a->filepath} not found for language {$a->lang}.Skipping this file.';
$string['cliexportstartexport'] = 'Exporting language "{$a}"';
$string['cliexportzipdone'] = 'Zip created: {$a}';
$string['cliexportzipfail'] = 'Cannot create zip {$a}';
$string['confirmcheckin'] = 'You are about to save modifications to your local language pack. This will export the customised strings from the translator into your site data directory and your site will start using the modified strings. Press \'Continue\' to proceed with saving.';
$string['customlang:edit'] = 'Edit local translation';
$string['customlang:export'] = 'Export local translation';
$string['customlang:view'] = 'View local translation';
$string['export'] = 'Export custom strings';
$string['exportfilter'] = 'Select component(s) to export';
$string['filter'] = 'Filter strings';
$string['filtercomponent'] = 'Show strings of these components';
$string['filtercustomized'] = 'Customised only';
@ -50,6 +60,7 @@ $string['markinguptodate_help'] = 'The customised translation may get outdated i
$string['markuptodate'] = 'mark as up-to-date';
$string['modifiedno'] = 'There are no modified strings to save.';
$string['modifiednum'] = 'There are {$a} modified strings. Do you wish to save these changes to your local language pack?';
$string['nolocallang'] = 'No local strings found.';
$string['nostringsfound'] = 'No strings found, please modify the filter settings';
$string['placeholder'] = 'Placeholders';
$string['placeholder_help'] = 'Placeholders are special statements like `{$a}` or `{$a->something}` within the string. They are replaced with a value when the string is actually printed.

View File

@ -243,7 +243,7 @@ class tool_customlang_utils {
* @param string $lang language code
* @return string full path
*/
protected static function get_localpack_location($lang) {
public static function get_localpack_location($lang) {
global $CFG;
return $CFG->langlocalroot.'/'.$lang.'_local';

View File

@ -0,0 +1,30 @@
@tool @tool_customlang
Feature: Within a moodle instance, an administrator should be able to modify langstrings for the entire Moodle installation.
In order to change langstrings in the adminsettings of the instance,
As an admin
I need to be able to access and change values in the the language customisation of the language pack.
Background:
Given I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
And I press "Open language pack for editing"
And I press "Continue"
And I set the field "Show strings of these components" to "moodle.php"
And I set the field "String identifier" to "administrationsite"
And I press "Show strings"
And I set the field "core/administrationsite" to "Custom string example"
@javascript
Scenario: Edit an string but don't save it to lang pack.
When I press "Apply changes and continue editing"
Then I should see "Site administration" in the "page-header" "region"
And I should not see "Custom string example" in the "page-header" "region"
@javascript
Scenario: Customize an string as admin and save it to lang pack.
Given I press "Save changes to the language pack"
And I should see "There are 1 modified strings."
When I click on "Continue" "button"
Then I should see "Custom string example" in the "page-header" "region"
And I should not see "Site administration" in the "page-header" "region"

View File

@ -0,0 +1,49 @@
@tool @tool_customlang
Feature: Within a moodle instance, an administrator should be able to export modified langstrings.
In order to export modified langstrings in the adminsettings of the instance,
As an admin
I need to be able to export the php-files of the language customisation of a language.
@javascript
Scenario: Export button should not appear if no customization is made
Given I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
Then I should see "Open language pack for editing"
And I should not see "Export custom strings"
@javascript
Scenario: Export button should not appear if no customization is saved into langpack
Given I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
And I press "Open language pack for editing"
And I press "Continue"
And I set the field "Show strings of these components" to "moodle.php"
And I set the field "String identifier" to "accept"
And I press "Show strings"
And I set the field "core/accept" to "Accept-custom_export"
When I press "Apply changes and continue editing"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
Then I should see "Open language pack for editing"
And I should see "There are 1 modified strings."
And I should not see "Export custom strings"
@javascript
Scenario: Export the php-file including a customised langstring.
Given I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
And I press "Open language pack for editing"
And I press "Continue"
And I set the field "Show strings of these components" to "moodle.php"
And I set the field "String identifier" to "accept"
And I press "Show strings"
And I set the field "core/accept" to "Accept-custom_export"
When I press "Save changes to the language pack"
And I should see "There are 1 modified strings."
And I click on "Continue" "button"
Then I set the field "lng" to "en"
And I click on "Export custom strings" "button"
And I set the field "Select component(s) to export" to "moodle.php"

View File

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2021052500;
$plugin->version = 2021052501;
$plugin->requires = 2021052500;
$plugin->component = 'tool_customlang'; // Full name of the plugin (used for diagnostics)