'. t('Supports translation for profile module field names and descriptions.') .'

'; $output .= '

'. t('To search and translate strings, use the translation interface pages.', array('@translate-interface' => url('admin/build/translate'))) .'

'; return $output; } } /** * Implementation of hook_locale(). */ function i18nprofile_locale($op = 'groups', $group = NULL) { switch ($op) { case 'groups': return array('profile' => t('Profile')); case 'info': $info['profile']['refresh callback'] = 'i18nprofile_locale_refresh'; $info['profile']['format'] = FALSE; return $info; } } /** * Refresh strings. */ function i18nprofile_locale_refresh() { $result = db_query('SELECT * FROM {profile_fields}'); $categories = array(); while ($field = db_fetch_object($result)) { // Store strings to translate: title, explanation, options. i18nstrings_update_object("profile:field:$field->name", $field, array('title', 'explanation', 'options')); // Store category if not there yet. if (!isset($categories[$field->category])) { i18nstrings_update("profile:category", $field->category); $categories[$field->category] = 1; } } return TRUE; // Meaning it completed with no issues } /** * Implementation of hook_menu_alter(). * * Replace title callbacks for profile categories. */ function i18nprofile_menu_alter(&$items) { $empty_account = new stdClass(); if (($categories = _user_categories($empty_account)) && (count($categories) > 1)) { foreach ($categories as $key => $category) { // 'account' is already handled by the MENU_DEFAULT_LOCAL_TASK. $path = 'user/%user_category/edit/'. $category['name']; if ($category['name'] != 'account' && !empty($items[$path])) { $items[$path]['title callback'] = 'i18nprofile_translate_category'; // Was 'check_plain', $items[$path]['title arguments'] = array($category['title']); // Was array($category['title']) } } } } function i18nprofile_translate_category($title) { return check_plain(i18nstrings('profile:category', $title)); } /** * Implementation of hook_profile_alter(). * * Translates categories and fields. */ function i18nprofile_profile_alter(&$account) { foreach (profile_categories() as $category) { $name = $category['name']; if (!empty($account->content[$name])) { // First ranslate category title then fields. $account->content[$name]['#title'] = i18nstrings('profile:category', $account->content[$name]['#title']); foreach (element_children($account->content[$name]) as $field) { i18nprofile_form_translate_field($account->content[$name], $field); // Translate value if options field if (!empty($account->content[$name][$field]['#value']) && $options = i18nprofile_field_options($field)) { // Get the value from the account because this one may have been formatted. if (isset($options[$account->$field])) { // It may be a link or a paragraph, trick for not loading the field again. if (!preg_match('|^name:$property"); } // Delete category too if no more fields in the same category if (!db_result(db_query("SELECT COUNT(*) FROM {profile_fields} WHERE category = '%s'", $field->category))) { i18nstrings_remove_string("profile:category", $values->category); } } /** * Process profile_field_form submissions. */ function i18nprofile_field_form_submit($form, &$form_state) { $values = (object)$form_state['values']; // Check old field name in case it has changed. $oldname = $form['fields']['name']['#default_value']; if ($oldname != 'profile_' && $oldname != $values->name) { i18nstrings_update_context("profile:field:$oldname:*", "profile:field:$values->name:*"); } // Store category. i18nstrings_update("profile:category", $values->category); // Store strings to translate: title, explanation, options. i18nstrings_update_object("profile:field:$values->name", $values, array('title', 'explanation', 'options')); } /** * Translate form fields for a given category. */ function i18nprofile_form_translate_category(&$form, $category) { if (!empty($form[$category])) { $form[$category]['#title'] = i18nstrings('profile:category', $form[$category]['#title']); foreach (element_children($form[$category]) as $field) { i18nprofile_form_translate_field($form[$category], $field); } } } /** * Translate form field. */ function i18nprofile_form_translate_field(&$form, $field) { if (!empty($form[$field]['#title'])) { $form[$field]['#title'] = i18nstrings("profile:field:$field:title", $form[$field]['#title']); } elseif (!empty($form[$field]['#value'])) { // Special treating for checboxes. $field_type = db_result(db_query("SELECT type FROM {profile_fields} WHERE name = '%s'", $field)); if ($field_type == 'checkbox') { $form[$field]['#value'] = i18nstrings("profile:field:$field:title", $form[$field]['#value']); } } if (!empty($form[$field]['#description'])) { $form[$field]['#description'] = i18nstrings("profile:field:$field:explanation", $form[$field]['#description']); } if (!empty($form[$field]['#options'])) { if ($options = i18nprofile_field_options($field, $form[$field]['#options'])) { $form[$field]['#options'] = $options; } } } /** * Translates field options. */ function i18nprofile_field_options($field, $source = array()) { if ($translation = i18nstrings("profile:field:$field:options", '')) { // Troubles when doing the split, produces empty lines, quick fix $translation = str_replace("\r", '', $translation); $translation = explode("\n", $translation); if ($source) { $options = $source; } elseif ($source = db_result(db_query("SELECT options FROM {profile_fields} WHERE name = '%s'", $field))) { $source = str_replace("\r", '', $source); $source = explode("\n", $source); $options = array(); } else { return NULL; } foreach ($source as $value) { if ($value != '--') { $string = $translation ? trim(array_shift($translation)) : trim($value); $options[trim($value)] = $string; } } return $options; } } /** * Translate form fields for all categories. * * This is useful when we don't know which categories we have, like in the user register form. */ function i18nprofile_form_translate_all($form_id, &$form) { $categories = profile_categories(); if (is_array($categories)) { foreach ($categories as $category) { if (isset($form[$category['name']])) { i18nprofile_form_translate_category( $form, $category['name']); } } } }