Index: l10n_community/translate.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/l10n_server/l10n_community/Attic/translate.inc,v retrieving revision 1.1.2.7.2.31.2.2 diff -u -p -r1.1.2.7.2.31.2.2 translate.inc --- l10n_community/translate.inc 26 Jan 2010 15:49:44 -0000 1.1.2.7.2.31.2.2 +++ l10n_community/translate.inc 3 Feb 2010 15:33:45 -0000 @@ -167,7 +167,7 @@ function l10n_community_filter_form_subm } } -// = Translation view ========================================================== +// = Translation form building ================================================= /** * Menu callback: List translations and suggestions. @@ -219,39 +219,57 @@ function l10n_community_translate_form(& $redirect_url = $_GET; unset($redirect_url['q']); + // The form will show but the submit buttons will only appear if the user has + // permissions to submit suggestions. This allows the use of this form to review + // strings in the database. $form = array( - '#submit' => array('l10n_community_translate_submit'), - '#redirect' => array($_GET['q'], $redirect_url), - 'langcode' => array('#type' => 'value', '#value' => $language->language), - 'pager_top' => array('#weight' => -10, '#value' => $pager), - 'submit_top' => array('#type' => 'submit', '#value' => t('Save changes'), '#access' => user_access('submit suggestions')), - 'strings' => array('#tree' => TRUE, '#theme' => 'l10n_community_translate_table'), - 'submit' => array('#type' => 'submit', '#value' => t('Save changes'), '#access' => user_access('submit suggestions')), - 'pager_bottom' => array('#weight' => 10, '#value' => $pager), + '#redirect' => array( + $_GET['q'], + $redirect_url + ), + 'langcode' => array( + '#type' => 'value', + '#value' => $language->language + ), + 'pager_top' => array( + '#weight' => -10, + '#value' => $pager + ), + 'submit_top' => array( + '#type' => 'submit', + '#value' => t('Save changes'), + '#access' => user_access('submit suggestions') + ), + 'strings' => array( + '#tree' => TRUE, + '#theme' => 'l10n_community_translate_table' + ), + 'submit' => array( + '#type' => 'submit', + '#value' => t('Save changes'), + '#access' => user_access('submit suggestions') + ), + 'pager_bottom' => array( + '#weight' => 10, + '#value' => $pager + ), ); + // Fill in string values for the editor. foreach ($strings as $string) { - $form['strings'][$string->sid] = _l10n_community_translate_string($form_state, $string, $language); + $form['strings'][$string->sid] = _l10n_community_translate_form_item($form_state, $string, $language); } - return $form; -} -/** - * Return a marked-up string. - */ -function _l10n_community_translate_render_strings($strings, $empty = '') { - if ($empty) { - $empty = ' data-empty="'. check_plain($empty) .'"'; - } - return "". implode("", array_map('check_plain', $strings)) .''; + return $form; } /** * Creates the form fragment for a source string. */ -function _l10n_community_translate_string(&$form_state, $source, $language) { - // Normalize empty default translation. +function _l10n_community_translate_form_item(&$form_state, $source, $language) { + if (!$source->translation) { + // If there is no picked translation yet, simulate with a visible placeholder. $source->tid = '0'; $source->translation = array(t('(not translated)')); $source->is_active = '1'; @@ -260,26 +278,25 @@ function _l10n_community_translate_strin else { $source->translation = l10n_community_unpack_string($source->translation); } - $source->value = l10n_community_unpack_string($source->value); $form = array( '#string' => $source, '#langcode' => $language->language, 'source' => array( - 'string' => array('#value' => _l10n_community_translate_render_strings($source->value)), + 'string' => array('#value' => _l10n_community_translate_render_textarray($source->value)), ), ); if (user_access('submit suggestions')) { $form['source']['edit'] = array( - '#value' => t('Edit Copy'), - '#prefix' => '', + '#value' => t('Edit a copy'), + '#prefix' => '', '#suffix' => '', ); } - // Add the current string (either a approved translation or a mock object + // Add the current string (either as approved translation or a mock object // for the "untranslated" string). $form[$source->tid] = _l10n_community_translate_translation($form_state, $source, $source); @@ -302,12 +319,27 @@ function _l10n_community_translate_strin } /** + * Generate markup for an unpacked string. + * + * @param $textarray + * An array of strings as generated by l10n_community_unpack_string(). + * @param $empty + * Specific data to include as the data to use when empty. + */ +function _l10n_community_translate_render_textarray($textarray, $empty = '') { + // data-empty is a proprietary attribute used in editor.css to be displayed when + // starting to submit a new suggestion. + $empty = !empty($empty) ? ' data-empty="'. check_plain($empty) .'"' : ''; + return "". implode("", array_map('check_plain', $textarray)) .''; +} + +/** * Build mock object for new textarea. */ function _l10n_community_translate_translation_textarea($source) { global $user; - return (object)array( + return (object) array( 'sid' => $source->sid, 'tid' => 'new', 'translation' => array_fill(0, count($source->value), ''), @@ -318,31 +350,6 @@ function _l10n_community_translate_trans } /** - * Generates the byline containing meta information about a string. - */ -function l10n_community_translate_byline($string) { - $params = array( - '!author' => theme('username', (object)array('name' => $string->username, 'uid' => $string->uid_entered)), - '@date' => format_date($string->time_entered), - '@ago' => t('@time ago', array('@time' => format_interval(time() - $string->time_entered))), - ); - if (!empty($string->uid_approved) && $string->uid_entered === $string->uid_approved && $string->time_entered === $string->time_approved) { - return t('translated and approved by !author on @date', $params); - } - else { - $title = t('suggested by !author on @date', $params); - if (!empty($string->uid_approved)) { - $title .= ''. t('approved by !author on @date', array( - '!author' => theme('username', (object)array('name' => $string->username_approved, 'uid' => $string->uid_approved)), - '@date' => format_date($string->time_approved), - '@ago' => t('@time ago', array('@time' => format_interval(time() - $string->time_approved))), - )); - } - return $title; - } -} - -/** * Creates the form fragment for a translated string. */ function _l10n_community_translate_translation(&$form_state, $string, $source) { @@ -352,17 +359,19 @@ function _l10n_community_translate_trans $is_active = $string->is_active && !$string->is_suggestion; $is_new = $string->tid == 'new'; $may_moderate = ($is_own ? user_access('moderate own suggestions') : user_access('moderate suggestions from others')); - //$may_stabilize = $permission & ($is_own ? L10N_PERM_STABILIZE_OWN : L10N_PERM_STABILIZE_OTHERS); $form = array( '#theme' => 'l10n_community_translate_translation', - 'original' => array('#type' => 'value', '#value' => $string), + 'original' => array( + '#type' => 'value', + '#value' => $string + ), ); $form['active'] = array( '#type' => 'radio', '#theme' => 'l10n_community_translate_radio', - '#title' => _l10n_community_translate_render_strings($string->translation, $is_new ? t('(empty)') : FALSE), + '#title' => _l10n_community_translate_render_textarray($string->translation, $is_new ? t('(empty)') : FALSE), '#return_value' => $string->tid, '#default_value' => $is_active ? $string->tid : NULL, '#parents' => array('strings', $string->sid, 'active'), @@ -374,18 +383,13 @@ function _l10n_community_translate_trans if ($may_moderate && $string->tid != 'new') { $form['declined'] = array( '#type' => 'checkbox', - '#title' => t('Declined'), + '#title' => t('Decline'), '#default_value' => !($string->is_active || $string->is_suggestion), ); } - // if ($may_stabilize) { - // $form['stable'] = array( - // '#type' => 'checkbox', - // '#title' => t('Stable'), - // '#default_value' => FALSE, // $string->is_stable, - // ); - // } if ($string->tid == 'new') { + // Fill in with as many textareas as required to enter translation + // for this string. $form['value'] = array_fill(0, count($source->value), array( '#type' => 'textarea', '#cols' => 60, @@ -396,8 +400,8 @@ function _l10n_community_translate_trans else { if (user_access('submit suggestions')) { $form['edit'] = array( - '#value' => t('Edit Copy'), - '#prefix' => '', + '#value' => t('Edit a copy'), + '#prefix' => '', '#suffix' => '', ); } @@ -412,9 +416,71 @@ function _l10n_community_translate_trans return $form; } +/** + * Generates the byline containing meta information about a string. + */ +function l10n_community_translate_byline($string) { + $params = array( + '!author' => theme('username', (object)array('name' => $string->username, 'uid' => $string->uid_entered)), + '@date' => format_date($string->time_entered), + '@ago' => t('@time ago', array('@time' => format_interval(time() - $string->time_entered))), + ); + if (!empty($string->uid_approved) && $string->uid_entered === $string->uid_approved && $string->time_entered === $string->time_approved) { + return t('translated and approved by !author on @date', $params); + } + else { + $title = t('suggested by !author on @date', $params); + if (!empty($string->uid_approved)) { + $title .= ''. t('approved by !author on @date', array( + '!author' => theme('username', (object)array('name' => $string->username_approved, 'uid' => $string->uid_approved)), + '@date' => format_date($string->time_approved), + '@ago' => t('@time ago', array('@time' => format_interval(time() - $string->time_approved))), + )); + } + return $title; + } +} + +// = Translation form theming ================================================== + +/** + * Main theme function for translation table. + */ +function theme_l10n_community_translate_table($element) { + $header = array( + t('Source Text'), + t('Translations'), + ); + + $rows = array(); + + foreach (element_children($element) as $key) { + $rows[] = array( + array('class' => 'source', 'data' => theme('l10n_community_translate_source', $element[$key])), + array('class' => 'translation', 'data' => theme('l10n_community_translate_translation_list', $element[$key])), + ); + } + + return theme('table', $header, $rows, array('class' => 'l10n-table')); +} + +/** + * Theme function for a source cell in the translation table. + */ +function theme_l10n_community_translate_source($element) { + $output = theme('l10n_community_translate_actions', $element['source']); + $output .= ''. drupal_render($element['source']['string']) .''; + $output .= theme('l10n_community_in_context', $element['#string']); + $output .= ''. t('More information') .''; + return $output; +} + +/** + * Theme appropriate actions for the given string element. + */ function theme_l10n_community_translate_actions($element) { $actions = ''; - foreach (array('declined', /*'stable', */'edit') as $type) { + foreach (array('declined', 'edit') as $type) { if (isset($element[$type])) { $actions .= ''. drupal_render($element[$type]) .''; } @@ -427,6 +493,19 @@ function theme_l10n_community_translate_ } } +/** + * Theme context information for source strings. + * + * @param $string + * Source string object. + */ +function theme_l10n_community_in_context($string) { + if (!empty($string->context)) { + return ''. t('in context: @context', array('@context' => $string->context)) .''; + } + return ''; +} + function theme_l10n_community_translate_translation($element) { if (!isset($element['#attributes']['class'])) { $element['#attributes']['class'] = ''; @@ -487,33 +566,14 @@ function theme_l10n_community_translate_ return $output; } -function theme_l10n_community_translate_source($element) { - $output = theme('l10n_community_translate_actions', $element['source']); - $output .= ''. drupal_render($element['source']['string']) .''; - $output .= theme('l10n_community_in_context', $element['#string']); - $output .= ''. t('More information') .''; - return $output; -} - -function theme_l10n_community_translate_table($element) { - $header = array( - t('Source Text'), - t('Translations'), - ); - - $rows = array(); - - foreach (element_children($element) as $key) { - $rows[] = array( - array('class' => 'source', 'data' => theme('l10n_community_translate_source', $element[$key])), - array('class' => 'translation', 'data' => theme('l10n_community_translate_translation_list', $element[$key])), - ); - } +// = Translation form submission =============================================== - return theme('table', $header, $rows, array('class' => 'l10n-table')); -} - -function l10n_community_translate_submit($form, &$form_state) { +/** + * Form submit callback for l10n_community_translate_form(). + * + * @see l10n_community_translate_form(). + */ +function l10n_community_translate_form_submit($form, &$form_state) { $langcode = $form_state['values']['langcode']; foreach ($form_state['values']['strings'] as $sid => $string) { @@ -544,13 +604,8 @@ function l10n_community_translate_submit l10n_community_approve_string($langcode, $sid, $tid); l10n_community_counter(L10N_COUNT_APPROVED); } - // if (/*!$options['original']->is_stable && */!empty($options['stable'])) { - // dsm('mark as stable'); - // l10n_community_counter('stabilized'); - // } } elseif (!empty($options['declined'])) { - // also remove stable flag! l10n_community_counter($options['original']->is_suggestion ? L10N_COUNT_SUGGESTION_DECLINED : L10N_COUNT_DECLINED); l10n_community_decline_string($langcode, $sid, $tid); } @@ -561,21 +616,6 @@ function l10n_community_translate_submit l10n_community_update_message(); } -// = Miscellaneous ============================================================= - -/** - * Theme context information for source strings. - * - * @param $string - * Source string object (based on l10n_community_string columns). - */ -function theme_l10n_community_in_context($string) { - if (!empty($string->context)) { - return ''. t('in context: @context', array('@context' => $string->context)) .''; - } - return ''; -} - // = API functions ============================================================= /** @@ -821,11 +861,16 @@ function l10n_community_approve_string($ /** * Unpacks a string as retrieved from the database. * + * Creates an array out of the string. If it was a single string, the array + * will have one item. If the string was a plural string, the array will have + * as many items as the language requires (two for source strings). + * * @param $string - * The string with separation markers (NULL byte) + * The string with optional separation markers (NULL bytes) * @return * An array of strings with one element for each plural form in case of - * a plural string, or one element in case of a regular string. + * a plural string, or one element in case of a regular string. This + * is called a $textarray elsewhere. */ function l10n_community_unpack_string($string) { return explode("\0", $string);