Example: Localization (with Zend_Translate)

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette\Forms example 3
  5. 5:  *
  6. 6:  * - localization (with Zend_Translate)
  7. 7:  */
  8. 8:  
  9. 9:  
  10. 10: require '../../Nette/loader.php';
  11. 11:  
  12. 12: // set_include_path();
  13. 13: include_once 'Zend/Translate.php';
  14. 14:  
  15. 15: if (!class_exists('Zend_Translate')) {
  16. 16:     die('This example requires Zend Framework');
  17. 17: }
  18. 18:  
  19. 19:  
  20. 20:  
  21. 22:  
  22. 23:  
  23. 24: class MyTranslator extends Zend_Translate implements ITranslator
  24. 25: {
  25. 26:     /**
  26. 27:      * Translates the given string.
  27. 28:      * @param  string   message
  28. 29:      * @param  int      plural count
  29. 30:      * @return string 
  30. 31:      */
  31. 32:     public function translate($message$count NULL)
  32. 33:     {
  33. 34:         return parent::translate($message);
  34. 35:     }
  35. 36: }
  36. 37:  
  37. 38:  
  38. 39: $countries array(
  39. 40:     'Select your country',
  40. 41:     'Europe' => array(
  41. 42:         'CZ' => 'Czech Republic',
  42. 43:         'SK' => 'Slovakia',
  43. 44:     ),
  44. 45:     'US' => 'USA',
  45. 46:     '?'  => 'other',
  46. 47: );
  47. 48:  
  48. 49: $sex array(
  49. 50:     'm' => 'male',
  50. 51:     'f' => 'female',
  51. 52: );
  52. 53:  
  53. 54:  
  54. 55:  
  55. 56: // Step 1: Define form with validation rules
  56. 57: $form new Form;
  57. 58: // enable translator
  58. 59: $translator new MyTranslator('gettext'dirname(__FILE__'/messages.mo''cs');
  59. 60: $translator->setLocale('cs');
  60. 61: $form->setTranslator($translator);
  61. 62:  
  62. 63: // group Personal data
  63. 64: $form->addGroup('Personal data');
  64. 65: $form->addText('name''Your name:'35)
  65. 66:     ->addRule(Form::FILLED'Enter your name');
  66. 67:  
  67. 68: $form->addText('age''Your age:'5)
  68. 69:     ->addRule(Form::FILLED'Enter your age')
  69. 70:     ->addRule(Form::INTEGER'Age must be numeric value')
  70. 71:     ->addRule(Form::RANGE'Age must be in range from %d to %d'array(10100));
  71. 72:  
  72. 73: $form->addRadioList('gender''Your gender:'$sex);
  73. 74:  
  74. 75: $form->addText('email''E-mail:'35)
  75. 76:     ->setEmptyValue('@')
  76. 77:     ->addCondition(Form::FILLED// conditional rule: if is email filled, ...
  77. 78:         ->addRule(Form::EMAIL'Incorrect E-mail Address')// ... then check email
  78. 79:  
  79. 80:  
  80. 81: // group Shipping address
  81. 82: $form->addGroup('Shipping address')
  82. 83:     ->setOption('embedNext'TRUE);
  83. 84:  
  84. 85: $form->addCheckbox('send''Ship to address')
  85. 86:     ->addCondition(Form::EQUALTRUE// conditional rule: if is checkbox checked...
  86. 87:         ->toggle('sendBox')// toggle div #sendBox
  87. 88:  
  88. 89:  
  89. 90: // subgroup
  90. 91: $form->addGroup()
  91. 92:     ->setOption('container'Html::el('div')->id('sendBox'));
  92. 93:  
  93. 94: $form->addText('street''Street:'35);
  94. 95:  
  95. 96: $form->addText('city''City:'35)
  96. 97:     ->addConditionOn($form['send']Form::EQUALTRUE)
  97. 98:         ->addRule(Form::FILLED'Enter your shipping address');
  98. 99:  
  99. 100: $form->addSelect('country''Country:'$countries)
  100. 101:     ->skipFirst()
  101. 102:     ->addConditionOn($form['send']Form::EQUALTRUE)
  102. 103:         ->addRule(Form::FILLED'Select your country');
  103. 104:  
  104. 105:  
  105. 106: // group Your account
  106. 107: $form->addGroup('Your account');
  107. 108:  
  108. 109: $form->addPassword('password''Choose password:'20)
  109. 110:     ->addRule(Form::FILLED'Choose your password')
  110. 111:     ->addRule(Form::MIN_LENGTH'The password is too short: it must be at least %d characters'3);
  111. 112:  
  112. 113: $form->addPassword('password2''Reenter password:'20)
  113. 114:     ->addConditionOn($form['password']Form::VALID)
  114. 115:         ->addRule(Form::FILLED'Reenter your password')
  115. 116:         ->addRule(Form::EQUAL'Passwords do not match'$form['password']);
  116. 117:  
  117. 118: $form->addFile('avatar''Picture:');
  118. 119:  
  119. 120: $form->addHidden('userid');
  120. 121:  
  121. 122: $form->addTextArea('note''Comment:'305);
  122. 123:  
  123. 124:  
  124. 125: // group for buttons
  125. 126: $form->addGroup();
  126. 127:  
  127. 128: $form->addSubmit('submit1''Send');
  128. 129:  
  129. 130:  
  130. 131:  
  131. 132: // Step 2: Check if form was submitted?
  132. 133: if ($form->isSubmitted()) {
  133. 134:  
  134. 135:     // Step 2c: Check if form is valid
  135. 136:     if ($form->isValid()) {
  136. 137:         echo '<h2>Form was submitted and successfully validated</h2>';
  137. 138:  
  138. 139:         $values $form->getValues();
  139. 140:         Debug::dump($values);
  140. 141:  
  141. 142:         // this is the end, my friend :-)
  142. 143:         if (empty($disableExit)) exit;
  143. 144:     }
  144. 145:  
  145. 146: else {
  146. 147:     // not submitted, define default values
  147. 148:     $defaults array(
  148. 149:         'name'    => 'John Doe',
  149. 150:         'userid'  => 231,
  150. 151:         'country' => 'CZ'// Czech Republic
  151. 152:     );
  152. 153:  
  153. 154:     $form->setDefaults($defaults);
  154. 156:  
  155. 157:  
  156. 158:  
  157. 159: // Step 3: Render form
  158. 160: ?>
  159. 161: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  160. 162: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  161. 163: <head>
  162. 164:     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  163. 165:     <meta http-equiv="content-language" content="en" />
  164. 166:  
  165. 167:     <title>Nette\Forms example 3 | Nette Framework</title>
  166. 168:  
  167. 169:     <style type="text/css">
  168. 170:     <!--
  169. 171:     .required {
  170. 172:         color: darkred
  171. 173:     }
  172. 174:  
  173. 175:     fieldset {
  174. 176:         padding: .5em;
  175. 177:         margin: .3em 0;
  176. 178:         background: #EAF3FA;
  177. 179:         border: 1px solid #b2d1eb;
  178. 180:     }
  179. 181:  
  180. 182:     input.button {
  181. 183:         font-size: 120%;
  182. 184:     }
  183. 185:  
  184. 186:     th {
  185. 187:         width: 8em;
  186. 188:         text-align: right;
  187. 189:     }
  188. 190:     -->
  189. 191:     </style>
  190. 192: </head>
  191. 193:  
  192. 194: <body>
  193. 195:     <h1>Nette\Forms example 3</h1>
  194. 196:  
  195. 197:     <?php echo $form ?>
  196. 198: </body>
  197. 199: </html>