foreach ($englisha->get_iterator() as $strenglisha) { $strenglishb = $englishb->get_string($strenglisha->id); $strtranslateda = $translateda->get_string($strenglisha->id); $strtranslatedb = $translatedb->get_string($strenglisha->id); // nothing compares to you, my dear null string if (is_null($strenglishb) or is_null($strtranslateda) or is_null($strtranslatedb)) { continue; } // in case we will need it, decide which of the translations is the more recent if ($strtranslateda->timemodified >= $strtranslatedb->timemodified) { $strtranslatedrecent = $strtranslateda; } else { $strtranslatedrecent = $strtranslatedb; } $englishchanged = mlang_string::differ($strenglisha, $strenglishb); $translatedchanged = mlang_string::differ($strtranslateda, $strtranslatedb); // English strings have changed but translated ones have not if ($data->mode == 1) { if ($englishchanged and !$translatedchanged) { $worka->add_string($strtranslateda); $workb->add_string($strtranslatedb); $num++; } } // English strings have not changed but translated ones have if ($data->mode == 2) { if (!$englishchanged and $translatedchanged) { if ($data->action == 1) { $worka->add_string($strtranslateda); $workb->add_string($strtranslatedb); } else {
die; } if (!confirm_sesskey()) { header('HTTP/1.1 403 Forbidden'); die; } $stringid = optional_param('stringid', null, PARAM_ALPHANUMEXT); $text = optional_param('text', null, PARAM_RAW); if (is_null($stringid) or is_null($text)) { header('HTTP/1.1 400 Bad Request'); die; } list($lang, $originalid, $translationid) = local_amos_translator::decode_identifier($stringid); $record = $DB->get_record('amos_repository', array('id' => $originalid), 'id,stringid,component,branch', MUST_EXIST); $version = mlang_version::by_code($record->branch); $component = new mlang_component($record->component, $lang, $version); if ($version->code < mlang_version::MOODLE_20) { header('HTTP/1.1 400 Bad Request'); die; } $string = new mlang_string($record->stringid, $text); $string->clean_text(); $component->add_string($string); $stage = mlang_persistent_stage::instance_for_user($USER->id, sesskey()); $stage->add($component, true); $stage->store(); mlang_stash::autosave($stage); header('Content-Type: application/json; charset: utf-8'); $response = new stdclass(); $response->text = local_amos_renderer::add_breaks(s($string->text)); echo json_encode($response);
/** * Converting 1.x strings into 2.x strings * - unescape all variables * - wrap all placeholders in curly brackets * - unescape quoting marks * - collapse percent signs */ public function test_fix_syntax_converting_from_v1_to_v2() { $this->assertEqual(mlang_string::fix_syntax('No change', 2, 1), 'No change'); $this->assertEqual(mlang_string::fix_syntax('Completed 100% of work', 2, 1), 'Completed 100% of work'); $this->assertEqual(mlang_string::fix_syntax('Completed 100%% of work', 2, 1), 'Completed 100% of work'); $this->assertEqual(mlang_string::fix_syntax("Windows\r\nsucks", 2, 1), "Windows\nsucks"); $this->assertEqual(mlang_string::fix_syntax("Linux\nsucks", 2, 1), "Linux\nsucks"); $this->assertEqual(mlang_string::fix_syntax("Mac\rsucks", 2, 1), "Mac\nsucks"); $this->assertEqual(mlang_string::fix_syntax("LINE TABULATION\vnewline", 2, 1), "LINE TABULATION\nnewline"); $this->assertEqual(mlang_string::fix_syntax("FORM FEED\fnewline", 2, 1), "FORM FEED\nnewline"); $this->assertEqual(mlang_string::fix_syntax("END OF TRANSMISSION BLOCKnewline", 2, 1), "END OF TRANSMISSION BLOCK\nnewline"); $this->assertEqual(mlang_string::fix_syntax("END OF MEDIUMnewline", 2, 1), "END OF MEDIUM\nnewline"); $this->assertEqual(mlang_string::fix_syntax("SUBSTITUTEnewline", 2, 1), "SUBSTITUTE\nnewline"); $this->assertEqual(mlang_string::fix_syntax("BREAK PERMITTED HEREnewline", 2, 1), "BREAK PERMITTED HERE\nnewline"); $this->assertEqual(mlang_string::fix_syntax("NEXT LINE newline", 2, 1), "NEXT LINE\nnewline"); $this->assertEqual(mlang_string::fix_syntax("START OF STRINGnewline", 2, 1), "START OF STRING\nnewline"); $this->assertEqual(mlang_string::fix_syntax("STRING TERMINATORnewline", 2, 1), "STRING TERMINATOR\nnewline"); $this->assertEqual(mlang_string::fix_syntax("Unicode Zl newline", 2, 1), "Unicode Zl\nnewline"); $this->assertEqual(mlang_string::fix_syntax("Unicode Zp newline", 2, 1), "Unicode Zp\nnewline"); $this->assertEqual(mlang_string::fix_syntax("Empty\n\n\n\n\n\nlines", 2, 1), "Empty\n\n\nlines"); $this->assertEqual(mlang_string::fix_syntax('Do not escape $variable names', 2, 1), 'Do not escape $variable names'); $this->assertEqual(mlang_string::fix_syntax('Do not escape \\$variable names', 2, 1), 'Do not escape $variable names'); $this->assertEqual(mlang_string::fix_syntax('Do not escape $alike names', 2, 1), 'Do not escape $alike names'); $this->assertEqual(mlang_string::fix_syntax('Do not escape \\$alike names', 2, 1), 'Do not escape $alike names'); $this->assertEqual(mlang_string::fix_syntax('Do not escape \\$a names', 2, 1), 'Do not escape $a names'); $this->assertEqual(mlang_string::fix_syntax('String $a placeholder', 2, 1), 'String {$a} placeholder'); $this->assertEqual(mlang_string::fix_syntax('String {$a} placeholder', 2, 1), 'String {$a} placeholder'); $this->assertEqual(mlang_string::fix_syntax('Trailing $a', 2, 1), 'Trailing {$a}'); $this->assertEqual(mlang_string::fix_syntax('$a leading', 2, 1), '{$a} leading'); $this->assertEqual(mlang_string::fix_syntax('$a', 2, 1), '{$a}'); $this->assertEqual(mlang_string::fix_syntax('$a->single', 2, 1), '{$a->single}'); $this->assertEqual(mlang_string::fix_syntax('Trailing $a->foobar', 2, 1), 'Trailing {$a->foobar}'); $this->assertEqual(mlang_string::fix_syntax('Trailing {$a}', 2, 1), 'Trailing {$a}'); $this->assertEqual(mlang_string::fix_syntax('Hit $a-times', 2, 1), 'Hit {$a}-times'); $this->assertEqual(mlang_string::fix_syntax('This is $a_book', 2, 1), 'This is $a_book'); $this->assertEqual(mlang_string::fix_syntax('Object $a->foo placeholder', 2, 1), 'Object {$a->foo} placeholder'); $this->assertEqual(mlang_string::fix_syntax('Object {$a->foo} placeholder', 2, 1), 'Object {$a->foo} placeholder'); $this->assertEqual(mlang_string::fix_syntax('Trailing $a->bar', 2, 1), 'Trailing {$a->bar}'); $this->assertEqual(mlang_string::fix_syntax('Trailing {$a->bar}', 2, 1), 'Trailing {$a->bar}'); $this->assertEqual(mlang_string::fix_syntax('Invalid $a-> placeholder', 2, 1), 'Invalid {$a}-> placeholder'); // weird but BC $this->assertEqual(mlang_string::fix_syntax('<strong>AMOS</strong>', 2, 1), '<strong>AMOS</strong>'); $this->assertEqual(mlang_string::fix_syntax("'Murder!', she wrote", 2, 1), "'Murder!', she wrote"); // will be escaped by var_export() $this->assertEqual(mlang_string::fix_syntax("\\'Murder!\\', she wrote", 2, 1), "'Murder!', she wrote"); // will be escaped by var_export() $this->assertEqual(mlang_string::fix_syntax("\t Trim Hunter \t\t", 2, 1), 'Trim Hunter'); $this->assertEqual(mlang_string::fix_syntax('Delete role "$a->role"?', 2, 1), 'Delete role "{$a->role}"?'); $this->assertEqual(mlang_string::fix_syntax('Delete role \\"$a->role\\"?', 2, 1), 'Delete role "{$a->role}"?'); $this->assertEqual(mlang_string::fix_syntax('See $CFG->foo', 2, 1), 'See $CFG->foo'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII NULL control character", 2, 1), 'Delete ASCII NULL control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII ENQUIRY control character", 2, 1), 'Delete ASCII ENQUIRY control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII ACKNOWLEDGE control character", 2, 1), 'Delete ASCII ACKNOWLEDGE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII BELL control character", 2, 1), 'Delete ASCII BELL control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII SHIFT OUT control character", 2, 1), 'Delete ASCII SHIFT OUT control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII SHIFT IN control character", 2, 1), 'Delete ASCII SHIFT IN control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII DATA LINK ESCAPE control character", 2, 1), 'Delete ASCII DATA LINK ESCAPE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII DEVICE CONTROL ONE control character", 2, 1), 'Delete ASCII DEVICE CONTROL ONE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII DEVICE CONTROL TWO control character", 2, 1), 'Delete ASCII DEVICE CONTROL TWO control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII DEVICE CONTROL THREE control character", 2, 1), 'Delete ASCII DEVICE CONTROL THREE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII DEVICE CONTROL FOUR control character", 2, 1), 'Delete ASCII DEVICE CONTROL FOUR control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII NEGATIVE ACKNOWLEDGE control character", 2, 1), 'Delete ASCII NEGATIVE ACKNOWLEDGE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII SYNCHRONOUS IDLE control character", 2, 1), 'Delete ASCII SYNCHRONOUS IDLE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII ESCAPE control character", 2, 1), 'Delete ASCII ESCAPE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ASCII DELETE control character", 2, 1), 'Delete ASCII DELETE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 PADDING CHARACTER control character", 2, 1), 'Delete ISO 8859 PADDING CHARACTER control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 HIGH OCTET PRESET control character", 2, 1), 'Delete ISO 8859 HIGH OCTET PRESET control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 NO BREAK HERE control character", 2, 1), 'Delete ISO 8859 NO BREAK HERE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 INDEX control character", 2, 1), 'Delete ISO 8859 INDEX control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 START OF SELECTED AREA control character", 2, 1), 'Delete ISO 8859 START OF SELECTED AREA control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 END OF SELECTED AREA control character", 2, 1), 'Delete ISO 8859 END OF SELECTED AREA control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 CHARACTER TABULATION SET control character", 2, 1), 'Delete ISO 8859 CHARACTER TABULATION SET control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 CHARACTER TABULATION WITH JUSTIFICATION control character", 2, 1), 'Delete ISO 8859 CHARACTER TABULATION WITH JUSTIFICATION control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 LINE TABULATION SET control character", 2, 1), 'Delete ISO 8859 LINE TABULATION SET control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 PARTIAL LINE FORWARD control character", 2, 1), 'Delete ISO 8859 PARTIAL LINE FORWARD control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 PARTIAL LINE BACKWARD control character", 2, 1), 'Delete ISO 8859 PARTIAL LINE BACKWARD control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 REVERSE LINE FEED control character", 2, 1), 'Delete ISO 8859 REVERSE LINE FEED control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 SINGLE SHIFT TWO control character", 2, 1), 'Delete ISO 8859 SINGLE SHIFT TWO control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 SINGLE SHIFT THREE control character", 2, 1), 'Delete ISO 8859 SINGLE SHIFT THREE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 DEVICE CONTROL STRING control character", 2, 1), 'Delete ISO 8859 DEVICE CONTROL STRING control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 PRIVATE USE ONE control character", 2, 1), 'Delete ISO 8859 PRIVATE USE ONE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 PRIVATE USE TWO control character", 2, 1), 'Delete ISO 8859 PRIVATE USE TWO control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 SET TRANSMIT STATE control character", 2, 1), 'Delete ISO 8859 SET TRANSMIT STATE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 MESSAGE WAITING control character", 2, 1), 'Delete ISO 8859 MESSAGE WAITING control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 START OF GUARDED AREA control character", 2, 1), 'Delete ISO 8859 START OF GUARDED AREA control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 END OF GUARDED AREA control character", 2, 1), 'Delete ISO 8859 END OF GUARDED AREA control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 SINGLE GRAPHIC CHARACTER INTRODUCER control character", 2, 1), 'Delete ISO 8859 SINGLE GRAPHIC CHARACTER INTRODUCER control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 SINGLE CHARACTER INTRODUCER control character", 2, 1), 'Delete ISO 8859 SINGLE CHARACTER INTRODUCER control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 CONTROL SEQUENCE INTRODUCER control character", 2, 1), 'Delete ISO 8859 CONTROL SEQUENCE INTRODUCER control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 OPERATING SYSTEM COMMAND control character", 2, 1), 'Delete ISO 8859 OPERATING SYSTEM COMMAND control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 PRIVACY MESSAGE control character", 2, 1), 'Delete ISO 8859 PRIVACY MESSAGE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete ISO 8859 APPLICATION PROGRAM COMMAND control character", 2, 1), 'Delete ISO 8859 APPLICATION PROGRAM COMMAND control character'); $this->assertEqual(mlang_string::fix_syntax("Delete Unicode ZERO WIDTH SPACE control character", 2, 1), 'Delete Unicode ZERO WIDTH SPACE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete Unicode ZERO WIDTH NO-BREAK SPACE control character", 2, 1), 'Delete Unicode ZERO WIDTH NO-BREAK SPACE control character'); $this->assertEqual(mlang_string::fix_syntax("Delete Unicode� REPLACEMENT CHARACTER control character", 2, 1), 'Delete Unicode REPLACEMENT CHARACTER control character'); }
/** * Merges all strings from one component to another and fixes syntax if needed * * If the string already exists in the target component, it is skipped (even * if it is set as deleted there). Does not modify the source component. * * @param mlang_component $source component to take strings from * @param mlang_component $target component to add strings to * @return void modifies $target component */ public static function merge(mlang_component $source, mlang_component $target) { if ($source->version->code <= mlang_version::MOODLE_19) { $sourceformat = 1; } else { $sourceformat = 2; } if ($target->version->code <= mlang_version::MOODLE_19) { throw new mlang_exception('Can not merge into Moodle 1.x branches'); } else { $targetformat = 2; } foreach ($source->get_iterator() as $string) { $stringid = clean_param($string->id, PARAM_STRINGID); if (empty($stringid)) { throw new mlang_exception('Invalid string identifier ' . s($string->id)); } if (!$target->has_string($stringid)) { $text = mlang_string::fix_syntax($string->text, $targetformat, $sourceformat); $target->add_string(new mlang_string($stringid, $text)); } } }
* Register new language * * @package local-amos * @copyright 2010 David Mudrak <*****@*****.**> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php'; require_once dirname(dirname(__FILE__)) . '/mlanglib.php'; require_once dirname(__FILE__) . '/newlanguage_form.php'; require_login(SITEID, false); require_capability('local/amos:manage', get_system_context()); $PAGE->set_pagelayout('standard'); $PAGE->set_url('/local/amos/admin/newlanguage.php'); $PAGE->set_title('AMOS ' . get_string('newlanguage', 'local_amos')); $PAGE->set_heading('AMOS ' . get_string('newlanguage', 'local_amos')); $form = new local_amos_newlanguage_form(); if ($data = $form->get_data()) { $component = new mlang_component('langconfig', $data->langcode, mlang_version::by_code(mlang_version::MOODLE_22)); $data->langname = mlang_string::fix_syntax($data->langname); $data->langnameint = mlang_string::fix_syntax($data->langnameint); $component->add_string(new mlang_string('thislanguage', $data->langname)); $component->add_string(new mlang_string('thislanguageint', $data->langnameint)); $stage = mlang_persistent_stage::instance_for_user($USER->id, sesskey()); $stage->add($component); $stage->store(); redirect(new moodle_url('/local/amos/stage.php')); } /// Output starts here echo $OUTPUT->header(); $form->display(); echo $OUTPUT->footer();
/** * Parses the given data in Moodle PHP string format * * @param string $data definition of the associative array * @param mlang_component $component component to add strings to * @param int $format the data format on the input, defaults to the one used since 2.0 * @return void */ public function parse($data, mlang_component $component, $format = 2) { $strings = $this->extract_strings($data); foreach ($strings as $id => $text) { $text = mlang_string::fix_syntax($text, 2, $format); $component->add_string(new mlang_string($id, $text)); } }