Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Generate Zend Framework 2 Validate messages from Validator'S messageTemplates

View 1_generate_validate_message.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
<?php
use Zend\Loader\StandardAutoloader;
use Zend\Code\Scanner\DirectoryScanner;
require 'library/Zend/Loader/StandardAutoloader.php';
//$dir = __DIR__.'/library/Zend/Validator';
$i18n_dir = __DIR__.'/library/Zend/I18n';
$validator_dir = __DIR__.'/library/Zend/Validator';
$loader = new StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
$translateSet = array_merge(scan_messageTemplates($i18n_dir), scan_messageTemplates($validator_dir));
ksort($translateSet);
// translate ja
$translateSet = replace($translateSet, include 'Zend_Validate.php');
make_resourcefile($translateSet);
/**
* functions
*/
function scan_messageTemplates($dir) {
$scanner = new DirectoryScanner($dir);
$classes = $scanner->getClasses();
$translateSet = array();
foreach ($classes as $class) {
foreach ($class->getProperties() as $property) {
if ($property->getName() === 'messageTemplates') {
//var_dump($property->getValue());
$reflection = new ReflectionClass($class->getName());
$defaults = $reflection->getDefaultProperties();
$translateSet[str_replace('\\', '_', $class->getName())] = $defaults['messageTemplates'];
}
}
}
return $translateSet;
}
function replace($en, $target) {
$translateSet = array();
foreach ($en as $k => $m) {
foreach ($m as $key => $message) {
$translateSet[$k][$message] = (isset($target[$message]))? $target[$message] : $message;
}
}
return $translateSet;
}
function make_resourcefile($translateSet) {
$translate = "\n";
$alreadyKeyHas = array();
foreach ($translateSet as $class_name => $messages) {
$translate .= " // $class_name"."\n";
foreach ($messages as $key => $message) {
if (in_array($key, $alreadyKeyHas)) {
$translate .= <<<EOF
// "$key" - same message already appeared above.
EOF;
continue;
}
$alreadyKeyHas[]= $key;
$translate .= <<<EOF
"$key" => "$message",
EOF;
$translate .= "\n";
}
$translate .= "\n";
}
file_put_contents(__DIR__.'/Zend_Validate.ja.php', "<?php \nreturn array($translate);");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.