Example: Localization (with Zend_Translate)

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