Changeset 8408
- Timestamp:
- 04/11/08 09:39:47 (6 months ago)
- Files:
-
- branches/1.1 (modified) (1 prop)
- branches/1.1/lib/autoload/sfCoreAutoload.class.php (modified) (1 diff)
- branches/1.1/lib/config/sfFactoryConfigHandler.class.php (modified) (1 diff)
- branches/1.1/lib/form/sfForm.class.php (modified) (1 diff)
- branches/1.1/lib/form/sfFormField.class.php (modified) (2 diffs)
- branches/1.1/lib/validator/sfValidatorError.class.php (modified) (2 diffs)
- branches/1.1/lib/widget/sfWidgetForm.class.php (modified) (1 diff)
- branches/1.1/lib/widget/sfWidgetFormSchema.class.php (modified) (4 diffs)
- branches/1.1/lib/widget/sfWidgetFormSchemaDecorator.class.php (modified) (1 diff)
- branches/1.1/lib/widget/sfWidgetFormSchemaFormatter.class.php (modified) (7 diffs)
- branches/1.1/test/functional/fixtures/project/apps/i18n/modules/i18n/actions/actions.class.php (modified) (1 diff)
- branches/1.1/test/functional/fixtures/project/apps/i18n/modules/i18n/i18n/messages.fr.xml (modified) (1 diff)
- branches/1.1/test/functional/fixtures/project/apps/i18n/modules/i18n/lib/I18nForm.class.php (added)
- branches/1.1/test/functional/fixtures/project/apps/i18n/modules/i18n/templates/i18nFormSuccess.php (added)
- branches/1.1/test/functional/i18nFormTest.php (added)
- branches/1.1/test/unit/widget/sfWidgetFormSchemaFormatterListTest.php (modified) (1 diff)
- branches/1.1/test/unit/widget/sfWidgetFormSchemaFormatterTableTest.php (modified) (1 diff)
- branches/1.1/test/unit/widget/sfWidgetFormSchemaFormatterTest.php (modified) (4 diffs)
- branches/1.1/test/unit/widget/sfWidgetFormSchemaTest.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1
- Property svn:ignore changed from
.project
.projectOptions
.cache
to
.project
.projectOptions
.cache
.settings
- Property svn:ignore changed from
branches/1.1/lib/autoload/sfCoreAutoload.class.php
r8404 r8408 335 335 'sfViewCacheManagerUpgrade' => 'task/project/upgrade1.1', 336 336 'sfWebDebugUpgrade' => 'task/project/upgrade1.1', 337 'sfTaskInitTask' => 'task/task',338 337 'sfBaseTask' => 'task', 339 338 'sfCommandApplicationTask' => 'task', 340 339 'sfFilesystem' => 'task', 341 340 'sfTask' => 'task', 341 'sfTaskInitTask' => 'task/task', 342 342 'sfTestAllTask' => 'task/test', 343 343 'sfTestFunctionalTask' => 'task/test', branches/1.1/lib/config/sfFactoryConfigHandler.class.php
r8226 r8408 143 143 "%s". 144 144 " \$this->factories['i18n'] = new \$class(\$this->configuration, \$cache, %s);\n". 145 " sfWidgetFormSchemaFormatter::setTranslationCallable(array(\$this->factories['i18n'], '__'));\n". 145 146 " }\n" 146 147 , $class, $cache, var_export($parameters, true) branches/1.1/lib/form/sfForm.class.php
r8158 r8408 259 259 if (!isset($labels[$i])) 260 260 { 261 $labels[$i] = sprintf('%s (%s)', $widgetSchema->ge nerateLabelName($name), $i);261 $labels[$i] = sprintf('%s (%s)', $widgetSchema->getFormFormatter()->generateLabelName($name), $i); 262 262 } 263 263 branches/1.1/lib/form/sfFormField.class.php
r7667 r8408 122 122 } 123 123 124 return $this->parent->getWidget()->ge nerateLabel($this->name);124 return $this->parent->getWidget()->getFormFormatter()->generateLabel($this->name); 125 125 } 126 126 … … 137 137 } 138 138 139 return $this->parent->getWidget()->ge nerateLabelName($this->name);139 return $this->parent->getWidget()->getFormFormatter()->generateLabelName($this->name); 140 140 } 141 141 branches/1.1/lib/validator/sfValidatorError.class.php
r7902 r8408 105 105 * 106 106 * $i18n->__($error->getMessageFormat(), $error->getArguments()); 107 * 108 * If no message format has been set in the validator, the exception standard 109 * message is returned. 107 110 * 108 111 * @return string The message format … … 110 113 public function getMessageFormat() 111 114 { 112 return $this->validator->getMessage($this->code); 115 $messageFormat = $this->validator->getMessage($this->code); 116 if (!$messageFormat) 117 { 118 $messageFormat = $this->getMessage(); 119 } 120 121 return $messageFormat; 113 122 } 114 123 } branches/1.1/lib/widget/sfWidgetForm.class.php
r5937 r8408 160 160 * @return string The field id or null. 161 161 */ 162 p rotectedfunction generateId($name, $value = null)162 public function generateId($name, $value = null) 163 163 { 164 164 if (false === $this->getOption('id_format')) branches/1.1/lib/widget/sfWidgetFormSchema.class.php
r8284 r8408 141 141 } 142 142 143 $this->formFormatters[$name] = new $class( );143 $this->formFormatters[$name] = new $class($this); 144 144 } 145 145 … … 343 343 344 344 // don't add a label tag and errors if we embed a form schema 345 $label = $widget instanceof sfWidgetFormSchema ? $this->ge nerateLabelName($name) : $this->generateLabel($name);345 $label = $widget instanceof sfWidgetFormSchema ? $this->getFormFormatter()->generateLabelName($name) : $this->getFormFormatter()->generateLabel($name); 346 346 $error = $widget instanceof sfWidgetFormSchema ? array() : $error; 347 347 … … 389 389 if (isset($errors[$name])) 390 390 { 391 $globalErrors[$this->ge nerateLabelName($name)] = $errors[$name];391 $globalErrors[$this->getFormFormatter()->generateLabelName($name)] = $errors[$name]; 392 392 } 393 393 } … … 395 395 396 396 return $globalErrors; 397 }398 399 /**400 * Generates a label for the given field name.401 *402 * @param string The field name403 *404 * @return string The label tag405 */406 public function generateLabel($name)407 {408 $labelName = $this->generateLabelName($name);409 410 if (false === $labelName)411 {412 return '';413 }414 415 return $this->renderContentTag('label', $labelName, array('for' => $this->generateId($this->generateName($name))));416 }417 418 /**419 * Generates the label name for the given field name.420 *421 * @param string The field name422 *423 * @return string The label name424 */425 public function generateLabelName($name)426 {427 $label = $this->getLabel($name);428 if (!$label && false !== $label)429 {430 $label = str_replace('_', ' ', ucfirst($name));431 }432 433 return $label;434 397 } 435 398 branches/1.1/lib/widget/sfWidgetFormSchemaDecorator.class.php
r7053 r8408 194 194 195 195 /** 196 * @see sfWidgetFormSchema 196 * @see sfWidgetFormSchemaFormatter 197 197 */ 198 198 public function generateLabel($name) 199 199 { 200 return $this->widget->ge nerateLabel($name);201 } 202 203 /** 204 * @see sfWidgetFormSchema 200 return $this->widget->getFormFormatter()->generateLabel($name); 201 } 202 203 /** 204 * @see sfWidgetFormSchemaFormatter 205 205 */ 206 206 public function generateLabelName($name) 207 207 { 208 return $this->widget->ge nerateLabelName($name);208 return $this->widget->getFormFormatter()->generateLabelName($name); 209 209 } 210 210 branches/1.1/lib/widget/sfWidgetFormSchemaFormatter.class.php
r6220 r8408 19 19 abstract class sfWidgetFormSchemaFormatter 20 20 { 21 protected static 22 $translationCallable = null; 23 21 24 protected 22 25 $rowFormat = '', … … 26 29 $errorRowFormatInARow = " <li>%error%</li>\n", 27 30 $namedErrorRowFormatInARow = " <li>%name%: %error%</li>\n", 28 $decoratorFormat = ''; 29 31 $decoratorFormat = '', 32 $widgetSchema = null; 33 34 /** 35 * Constructor 36 * 37 * @param sfWidgetFormSchema $widgetSchema 38 */ 39 public function __construct(sfWidgetFormSchema $widgetSchema) 40 { 41 $this->widgetSchema = $widgetSchema; 42 } 43 30 44 public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null) 31 45 { … … 38 52 )); 39 53 } 40 54 55 /** 56 * Translates a string using an i18n callable, if it has been provided 57 * 58 * @param mixed $subject The subject to translate 59 * @param array $parameters Additional parameters to pass back to the callable 60 * 61 * @return string 62 */ 63 static public function translate($subject, $parameters = array()) 64 { 65 if (false === $subject) 66 { 67 return false; 68 } 69 70 if (is_null(self::$translationCallable)) 71 { 72 return strtr($subject, $parameters); 73 } 74 75 if (self::$translationCallable instanceof sfCallable) 76 { 77 return self::$translationCallable->call($subject, $parameters); 78 } 79 80 return call_user_func(self::$translationCallable, $subject, $parameters); 81 } 82 83 /** 84 * Returns the current i18n callable 85 * 86 * @return mixed 87 */ 88 static public function getTranslationCallable() 89 { 90 return self::$translationCallable; 91 } 92 93 /** 94 * Sets a callable which aims to translate form labels, errors and help messages 95 * 96 * @param mixed $callable 97 * 98 * @throws InvalidArgumentException if an invalid php callable or sfCallable has been provided 99 */ 100 static public function setTranslationCallable($callable) 101 { 102 if (!$callable instanceof sfCallable && !is_callable($callable)) 103 { 104 throw new InvalidArgumentException('Provided i18n callable should be either an instance of sfCallable or a valid PHP callable'); 105 } 106 107 self::$translationCallable = $callable; 108 } 109 41 110 public function formatHelp($help) 42 111 { … … 46 115 } 47 116 48 return strtr($this->getHelpFormat(), array('%help%' => $help));117 return strtr($this->getHelpFormat(), array('%help%' => self::translate($help))); 49 118 } 50 119 … … 72 141 73 142 return strtr($this->getErrorListFormatInARow(), array('%errors%' => implode('', $this->unnestErrors($errors)))); 143 } 144 145 /** 146 * Generates a label for the given field name. 147 * 148 * @param string The field name 149 * 150 * @return string The label tag 151 */ 152 public function generateLabel($name) 153 { 154 $labelName = $this->generateLabelName($name); 155 156 if (false === $labelName) 157 { 158 return ''; 159 } 160 161 $widgetId = $this->widgetSchema->generateId($this->widgetSchema->generateName($name)); 162 return $this->widgetSchema->renderContentTag('label', $labelName, array('for' => $widgetId)); 163 } 164 165 /** 166 * Generates the label name for the given field name. 167 * 168 * @param string The field name 169 * 170 * @return string The label name 171 */ 172 public function generateLabelName($name) 173 { 174 $label = $this->widgetSchema->getLabel($name); 175 176 if (!$label && false !== $label) 177 { 178 $label = str_replace('_', ' ', ucfirst($name)); 179 } 180 181 return self::translate($label); 74 182 } 75 183 … … 86 194 else 87 195 { 88 $err = is_object($error) ? $error->__toString() : $error; 196 if ($error instanceof sfValidatorError) 197 { 198 $err = self::translate($error->getMessageFormat(), $error->getArguments()); 199 } 200 else 201 { 202 $err = self::translate($error); 203 } 89 204 90 205 if (!is_integer($name)) … … 101 216 return $newErrors; 102 217 } 103 218 104 219 public function setRowFormat($format) 105 220 { branches/1.1/test/functional/fixtures/project/apps/i18n/modules/i18n/actions/actions.class.php
r4344 r8408 30 30 $this->forward('i18n', 'index'); 31 31 } 32 33 public function executeI18nForm(sfWebRequest $request) 34 { 35 $this->form = new I18nForm(); 36 if ($request->isMethod('post')) 37 { 38 $this->form->bind($request->getParameter('i18n')); 39 } 40 } 32 41 } branches/1.1/test/functional/fixtures/project/apps/i18n/modules/i18n/i18n/messages.fr.xml
r2757 r8408 7 7 <target>une phrase locale en français</target> 8 8 </trans-unit> 9 <trans-unit id="2"> 10 <source>First name</source> 11 <target>Prénom</target> 12 </trans-unit> 13 <trans-unit id="3"> 14 <source>Last name</source> 15 <target>Nom</target> 16 </trans-unit> 17 <trans-unit id="4"> 18 <source>Put your first name here</source> 19 <target>Mettez votre prénom ici</target> 20 </trans-unit> 21 <trans-unit id="4"> 22 <source>Required.</source> 23 <target>Champ requis.</target> 24 </trans-unit> 25 <trans-unit id="5"> 26 <source>Email address</source> 27 <target>Adresse email</target> 28 </trans-unit> 29 <trans-unit id="6"> 30 <source>%value% is an invalid email address</source> 31 <target>%value% est une adresse email invalide</target> 32 </trans-unit> 9 33 </body> 10 34 </file> branches/1.1/test/unit/widget/sfWidgetFormSchemaFormatterListTest.php
r5996 r8408 13 13 $t = new lime_test(2, new lime_output_color()); 14 14 15 $f = new sfWidgetFormSchemaFormatterList( );15 $f = new sfWidgetFormSchemaFormatterList(new sfWidgetFormSchema()); 16 16 17 17 // ->formatRow() branches/1.1/test/unit/widget/sfWidgetFormSchemaFormatterTableTest.php
r5996 r8408 13 13 $t = new lime_test(2, new lime_output_color()); 14 14 15 $f = new sfWidgetFormSchemaFormatterTable( );15 $f = new sfWidgetFormSchemaFormatterTable(new sfWidgetFormSchema()); 16 16 17 17 // ->formatRow() branches/1.1/test/unit/widget/sfWidgetFormSchemaFormatterTest.php
r5937 r8408 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 10, new lime_output_color());13 $t = new lime_test(22, new lime_output_color()); 14 14 15 15 class MyFormatter extends sfWidgetFormSchemaFormatter … … 24 24 return parent::unnestErrors($errors, $prefix); 25 25 } 26 27 static public function dropTranslationCallable() 28 { 29 self::$translationCallable = null; 30 } 26 31 } 27 32 28 $f = new MyFormatter(); 33 $w1 = new sfWidgetFormInput(); 34 $w2 = new sfWidgetFormInput(); 35 $w = new sfWidgetFormSchema(array('w1' => $w1, 'w2' => $w2)); 36 $f = new MyFormatter($w); 29 37 30 38 // ->formatRow() … … 32 40 $output = <<<EOF 33 41 <li> 34 label35 <input /> help42 <label>label</label> 43 <input /><p>help</p> 36 44 </li> 37 45 38 46 EOF; 39 $t->is($f->formatRow(' label', '<input />', array(), 'help', ''), $output, '->formatRow() formats a field in a row');47 $t->is($f->formatRow('<label>label</label>', '<input />', array(), '<p>help</p>', ''), $output, '->formatRow() formats a field in a row'); 40 48 41 49 // ->formatErrorRow() … … 70 78 $t->is($f->$getter(), $value, sprintf('->%s() ->%s()', $getter, $setter)); 71 79 } 80 81 $t->diag('::setTranslationCallable() ::getTranslationCallable()'); 82 function my__($string) 83 { 84 return sprintf('[%s]', $string); 85 } 86 87 class myI18n 88 { 89 static public function __($string) 90 { 91 return my__($string); 92 } 93 } 94 MyFormatter::setTranslationCallable('my__'); 95 96 $t->is(MyFormatter::getTranslationCallable(), 'my__', 'get18nCallable() retrieves i18n callable correctly'); 97 98 MyFormatter::setTranslationCallable(new sfCallable('my__')); 99 $t->isa_ok(MyFormatter::getTranslationCallable(), 'sfCallable', 'get18nCallable() retrieves i18n sfCallable correctly'); 100 101 try 102 { 103 $f->setTranslationCallable('foo'); 104 $t->fail('setTranslationCallable() does not throw InvalidException when i18n callable is invalid'); 105 } 106 catch (InvalidArgumentException $e) 107 { 108 $t->pass('setTranslationCallable() throws InvalidException if i18n callable is not a valid callable'); 109 } 110 catch (Exception $e) 111 { 112 $t->fail('setTranslationCallable() throws unexpected exception'); 113 } 114 115 $t->diag('->translate()'); 116 $t->is(MyFormatter::translate('label'), '[label]', 'translate() call i18n sfCallable as expected'); 117 118 MyFormatter::setTranslationCallable(array('myI18n', '__')); 119 $t->is(MyFormatter::translate('label'), '[label]', 'translate() call i18n callable as expected'); 120 121 $t->diag('->generateLabel() ->generateLabelName() ->setLabel() ->setLabels()'); 122 MyFormatter::dropTranslationCallable(); 123 $w = new sfWidgetFormSchema(array( 124 'first_name' => new sfWidgetFormInput(), 125 'last_name' => new sfWidgetFormInput(), 126 )); 127 $f = new MyFormatter($w); 128 $t->is($f->generateLabelName('first_name'), 'First name', '->generateLabelName() generates a label value from a label name'); 129 130 $w->setLabels(array('first_name' => 'The first name')); 131 $t->is($f->generateLabelName('first_name'), 'The first name', '->setLabels() changes all current labels'); 132 133 $w->setLabel('first_name', 'A first name'); 134 $t->is($f->generateLabelName('first_name'), 'A first name', '->setLabel() sets a label value'); 135 136 $w->setLabel('first_name', false); 137 $t->is($f->generateLabel('first_name'), '', '->generateLabel() returns an empty string if the label is false'); 138 139 $w->setLabel('first_name', 'Your First Name'); 140 $t->is($f->generateLabel('first_name'), '<label for="first_name">Your First Name</label>', '->generateLabelName() returns a label tag'); 141 142 $w->setLabel('last_name', 'Your Last Name'); 143 $t->is($f->generateLabel('last_name'), '<label for="last_name">Your Last Name</label>', '->generateLabelName() returns a label tag'); 144 MyFormatter::setTranslationCallable('my__'); 145 $t->is($f->generateLabel('last_name'), '<label for="last_name">[Your Last Name]</label>', '->generateLabelName() returns a i18ned label tag'); branches/1.1/test/unit/widget/sfWidgetFormSchemaTest.php
r8284 r8408 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 61, new lime_output_color());13 $t = new lime_test(57, new lime_output_color()); 14 14 15 15 $w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); … … 75 75 $t->is(get_class($w->getFormFormatter()), 'sfWidgetFormSchemaFormatterTable', '->getFormFormatter() returns a sfWidgetSchemaFormatter object'); 76 76 77 $w->addFormFormatter('custom', $customFormatter = new sfWidgetFormSchemaFormatterList( ));77 $w->addFormFormatter('custom', $customFormatter = new sfWidgetFormSchemaFormatterList($w)); 78 78 $w->setFormFormatterName('custom'); 79 79 $t->is(get_class($w->getFormFormatter()), 'sfWidgetFormSchemaFormatterList', '->addFormFormatter() associates a name with a sfWidgetSchemaFormatter object'); … … 141 141 $t->diag('->setLabels() ->setLabel() ->getLabels() ->getLabel() ->generateLabelName()'); 142 142 $w = new sfWidgetFormSchema(); 143 $t->is($w->generateLabelName('first_name'), 'First name', '->generateLabelName() generates a label value from a label name'); 143 $w->setLabel('first_name', 'A first name'); 144 $t->is($w->getLabels(), array('first_name' => 'A first name'), '->getLabels() returns all current labels'); 145 144 146 $w->setLabels(array('first_name' => 'The first name')); 145 $t->is($w->generateLabelName('first_name'), 'The first name', '->setLabels() changes all current labels'); 147 $t->is($w->getFormFormatter()->generateLabelName('first_name'), 'The first name', '->setLabels() changes all current labels'); 148 146 149 $w->setLabel('first_name', 'A first name'); 147 $t->is($w->generateLabelName('first_name'), 'A first name', '->setLabel() sets a label value'); 148 $t->is($w->getLabels(), array('first_name' => 'A first name'), '->getLabels() returns all current labels'); 150 $t->is($w->getFormFormatter()->generateLabelName('first_name'), 'A first name', '->setLabel() sets a label value'); 149 151 150 152 // ->setHelps() ->getHelps() ->setHelp() ->getHelp() … … 155 157 $w->setHelp('last_name', 'Please, provide your last name'); 156 158 $t->is($w->getHelp('last_name'), 'Please, provide your last name', '->setHelp() changes one help message'); 157 158 // ->generateLabel()159 $t->diag('->generateLabel()');160 $w = new sfWidgetFormSchema();161 $w->setLabel('first_name', false);162 $t->is($w->generateLabel('first_name'), '', '->generateLabelName() returns an empty string if the label is false');163 $w->setLabel('first_name', 'The First Name');164 $t->is($w->generateLabel('first_name'), '<label for="first_name">The First Name</label>', '->generateLabelName() returns a label tag');165 $t->is($w->generateLabel('last_name'), '<label for="last_name">Last name</label>', '->generateLabelName() returns a label tag');166 159 167 160 // ->needsMultipartForm()