/[drupal]/contributions/modules/i18n/i18nprofile/i18nprofile.module
ViewVC logotype

Contents of /contributions/modules/i18n/i18nprofile/i18nprofile.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Sun Feb 17 17:25:39 2008 UTC (21 months, 1 week ago) by jareyero
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.5: +31 -5 lines
File MIME type: text/x-php
Updated i18nprofile for Drupal 6.0
Improved strings, handling of source strings indexed by string.
Updated syntax for i18nviews.info.
1 <?php
2 // $Id: i18nprofile.module,v 1.5 2007/11/02 02:05:11 jareyero Exp $
3
4 /**
5 * Internationalization (i18n) submodule: Profile translation
6 *
7 * Allows translation of profile categories and fields
8 *
9 * @author Jose A. Reyero, 2006
10 *
11 */
12
13 /**
14 * Implementation of hook_help()
15 */
16 function i18nprofile_help($section = 'admin/help#i18nmenu' ) {
17 switch ($section) {
18 case 'admin/modules#description' :
19 return t('Supports translation for profile module field names and descriptions.' );
20 }
21 }
22
23 /**
24 * Implementation of hook_locale().
25 */
26 function i18nprofile_locale($op = 'groups') {
27 switch ($op) {
28 case 'groups':
29 return array('profile' => t('Profile'));
30 }
31 }
32
33 /**
34 * Implementation of hook_menu_alter().
35 *
36 * Replace title callbacks for profile categories
37 */
38 function i18nprofile_menu_alter(&$items) {
39 $empty_account = new stdClass();
40 if (($categories = _user_categories($empty_account)) && (count($categories) > 1)) {
41 foreach ($categories as $key => $category) {
42 // 'account' is already handled by the MENU_DEFAULT_LOCAL_TASK.
43 $path = 'user/%user_category/edit/'. $category['name'];
44 if ($category['name'] != 'account' && !empty($items[$path])) {
45 $items[$path]['title callback'] = 'i18nprofile_translate_category'; // Was 'check_plain',
46 $items[$path]['title arguments'] = array($category['title']); // Was array($category['title'])
47 }
48 }
49 }
50 }
51
52 function i18nprofile_translate_category($title) {
53 return check_plain(tt('profile:category', $title));
54 }
55
56 /**
57 * Implementation of hook_profile_alter()
58 *
59 * Translates categories and fields
60 */
61 function i18nprofile_profile_alter(&$account) {
62 foreach (profile_categories() as $category) {
63 $name = $category['name'];
64 if (!empty($account->content[$name])) {
65 // First ranslate category title then fields
66 $account->content[$name]['#title'] = tt('profile:category', $account->content[$name]['#title']);
67 foreach (element_children($account->content[$name]) as $field) {
68 i18nprofile_form_translate_field($account->content[$name], $field);
69 // Translate value if options field
70 if (!empty($account->content[$name][$field]['#value']) && $options = i18nprofile_field_options($field)) {
71 // Get the value from the account because this one may have been formatted
72 if (isset($options[$account->$field])) {
73 // It may be a link or a paragraph, trick for not loading the field again
74 if ($account->content[$name][$field]['#value'] == check_markup($account->$field)) {
75 $account->content[$name][$field]['#value'] = check_markup($options[$account->$field]);
76 // Plain html
77 } else {
78 // Link
79 $account->content[$name][$field]['#value'] == check_markup(l($account->$field, 'profile/'. $field .'/'. $account->$field));
80 }
81 }
82 }
83
84 }
85 }
86 }
87 }
88
89 /**
90 * Implementation of hook_form_alter()
91 */
92 function i18nprofile_form_alter(&$form, $form_state, $form_id) {
93 switch ($form_id) {
94 case 'profile_field_form':
95 $form['#submit'][] = 'i18nprofile_field_form_submit';
96 break;
97 case 'user_profile_form':
98 if (($category = $form['_category']['#value']) && $category != 'account') {
99 i18nprofile_form_translate_category($form, $category);
100 }
101 break;
102 /*
103 elseif ($form_id == 'user_register') {
104 i18nprofile_form_translate_all($form_id, $form);
105 }
106 */
107 break;
108 }
109 }
110
111 /**
112 * Process profile_field_form submissions.
113 */
114 function i18nprofile_field_form_submit($form, &$form_state) {
115 $values = (object)$form_state['values'];
116 // Check old field name in case it has changed
117 $oldname = $form['fields']['name']['#default_value'];
118 if ($oldname != 'profile_' && $oldname != $values->name) {
119 i18nstrings_update_context("profile:field:$oldname:*", "profile:field:$values->name:*");
120 }
121 // Store category
122 tt("profile:category", $values->category, NULL, TRUE);
123 // Store strings to translate: title, explanation, options
124 to("profile:field:$values->name", $values, array('title', 'explanation', 'options'), NULL, TRUE);
125 }
126
127 /**
128 * Translate form fields for a given category
129 */
130 function i18nprofile_form_translate_category(&$form, $category){
131 if (!empty($form[$category])) {
132 $form[$category]['#title'] = tt('profile:category', $form[$category]['#title']);
133 foreach (element_children($form[$category]) as $field) {
134 i18nprofile_form_translate_field($form[$category], $field);
135 }
136 }
137 }
138
139 /**
140 * Translate form field
141 *
142 */
143 function i18nprofile_form_translate_field(&$form, $field) {
144 if (!empty($form[$field]['#title'])) {
145 $form[$field]['#title'] = tt("profile:field:$field:title", $form[$field]['#title']);
146 }
147 if (!empty($form[$field]['#description'])) {
148 $form[$field]['#description'] = tt("profile:field:$field:description", $form[$field]['#description']);
149 }
150 if (!empty($form[$field]['#options'])) {
151 if ($options = i18nprofile_field_options($field, $form[$field]['#options'])) {
152 $form[$field]['#options'] = $options;
153 }
154 }
155
156 }
157
158 /**
159 * Translates field options
160 */
161 function i18nprofile_field_options($field, $source = array()) {
162 if ($translation = tt("profile:field:$field:options", '')) {
163 // Troubles when doing the split, produces empty lines, quick fix
164 $translation = str_replace("\n\r", "\n", $translation);
165 $translation = split("[,\n]", $translation);
166 if ($source) {
167 $options = $source;
168 } else if($source = db_result(db_query("SELECT options FROM {profile_fields} WHERE name = '%s'", $field))) {
169 $source = split("[,\n\r]", $source);
170 $options = array();
171 } else {
172 return NULL;
173 }
174 foreach ($source as $value) {
175 if ($value != '--') {
176 $string = $translation ? trim(array_shift($translation)) : trim($value);
177 $options[trim($value)] = $string;
178 }
179 }
180 return $options;
181 }
182 }
183
184 /**
185 * Translate form fields for all categories
186 *
187 * This is useful when we don't know which categories we have, like in the user register form
188 */
189 function i18nprofile_form_translate_all($form_id, &$form) {
190 $categories = profile_categories();
191 if(is_array($categories)) {
192 foreach($categories as $category) {
193 if(isset($form[$category['name']])) {
194 i18nprofile_form_translate($form_id, $form, $category['name']);
195 }
196 }
197 }
198 }
199
200 /**
201 * Returns translated categories
202 *
203 * @ TODO Drupal 6
204 *
205 * Note: weight must be minor than profile module's for them to be added first
206 *
207 * @param $getraw
208 * Return the raw array of translations
209 */
210 function i18nprofile_categories($getraw = FALSE) {
211 $language = i18n_get_lang();
212 if ($translation = variable_get('i18nprofile_'.$language, 0)) {
213 if($getraw) {
214 return $translation;
215 }
216 foreach($translation as $name => $value) {
217 $categories[] = array('name' => $name, 'title' => $value, 'weight' => 2);
218 }
219 return $categories;
220 } else {
221 return array();
222 }
223 }
224
225 /**
226 * Returns field translations
227 *
228 * @ TODO Drupal 6
229 */
230 function i18nprofile_fields($category){
231 static $_fields;
232 $language = i18n_get_lang();
233 if(!isset($_fields[$category])) {
234 $_fields[$category] = array();
235 // Some special categories
236 $result = db_query("SELECT p.name, p.type, t.* FROM {profile_fields} p INNER JOIN {i18n_profile_fields} t ON p.fid = t.fid WHERE LOWER(p.category) = LOWER('%s') AND t.language='%s'", $category, $language);
237 while($field = db_fetch_object($result)) {
238 $_fields[$category][$field->name] = $field;
239 }
240 }
241 return $_fields[$category];
242 }
243

  ViewVC Help
Powered by ViewVC 1.1.2