2011-08-02 00:37:34 +00:00
< ? php
/**
2011-08-07 13:10:57 +00:00
* @ file
* Interface translation summary , editing and deletion user interfaces .
2011-08-02 00:37:34 +00:00
*/
2012-06-02 19:41:40 +00:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException ;
2011-08-02 00:37:34 +00:00
/**
* String search screen .
*/
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
function locale_translate_page () {
return array (
'filter' => drupal_get_form ( 'locale_translate_filter_form' ),
'form' => drupal_get_form ( 'locale_translate_edit_form' ),
);
2011-08-02 00:37:34 +00:00
}
/**
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
* Build a string search query .
2011-08-02 00:37:34 +00:00
*/
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
function locale_translate_query () {
$filter_values = locale_translate_filter_values ();
2011-08-02 00:37:34 +00:00
$sql_query = db_select ( 'locales_source' , 's' );
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
// Language is sanitized to be one of the possible options in
// locale_translate_filter_values().
$sql_query -> leftJoin ( 'locales_target' , 't' , " t.lid = s.lid AND t.language = :langcode " , array ( ':langcode' => $filter_values [ 'langcode' ]));
2011-08-07 13:10:57 +00:00
$sql_query -> fields ( 's' , array ( 'source' , 'location' , 'context' , 'lid' ));
2012-04-09 18:24:12 +00:00
$sql_query -> fields ( 't' , array ( 'translation' , 'language' , 'customized' ));
2011-08-02 00:37:34 +00:00
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
if ( ! empty ( $filter_values [ 'string' ])) {
$sql_query -> condition ( db_or ()
-> condition ( 's.source' , '%' . db_like ( $filter_values [ 'string' ]) . '%' , 'LIKE' )
-> condition ( 't.translation' , '%' . db_like ( $filter_values [ 'string' ]) . '%' , 'LIKE' )
);
}
// Add translation status conditions.
switch ( $filter_values [ 'translation' ]) {
2011-08-02 00:37:34 +00:00
case 'translated' :
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$sql_query -> isNotNull ( 't.translation' );
if ( $filter_values [ 'customized' ] != 'all' ) {
$sql_query -> condition ( 't.customized' , $filter_values [ 'customized' ]);
2012-04-09 18:24:12 +00:00
}
2011-08-02 00:37:34 +00:00
break ;
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
2011-08-02 00:37:34 +00:00
case 'untranslated' :
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$sql_query -> isNull ( 't.translation' );
2011-08-02 00:37:34 +00:00
break ;
}
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$sql_query = $sql_query -> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' ) -> limit ( 30 );
return $sql_query -> execute ();
2011-08-02 00:37:34 +00:00
}
/**
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
* Build array out of search criteria specified in request variables .
2011-08-02 00:37:34 +00:00
*/
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
function locale_translate_filter_values () {
$filter_values = & drupal_static ( __FUNCTION__ );
if ( ! isset ( $filter_values )) {
$filter_values = array ();
$filters = locale_translate_filters ();
foreach ( $filters as $key => $filter ) {
$filter_values [ $key ] = $filter [ 'default' ];
// Let the filter defaults be overwritten by parameters in the URL.
if ( isset ( $_GET [ $key ])) {
// Only allow this value if it was among the options, or
// if there were no fixed options to filter for.
if ( ! isset ( $filter [ 'options' ]) || isset ( $filter [ 'options' ][ $_GET [ $key ]])) {
$filter_values [ $key ] = $_GET [ $key ];
}
}
elseif ( isset ( $_SESSION [ 'locale_translate_filter' ][ $key ])) {
// Only allow this value if it was among the options, or
// if there were no fixed options to filter for.
if ( ! isset ( $filter [ 'options' ]) || isset ( $filter [ 'options' ][ $_SESSION [ 'locale_translate_filter' ][ $key ]])) {
$filter_values [ $key ] = $_SESSION [ 'locale_translate_filter' ][ $key ];
}
2011-08-02 00:37:34 +00:00
}
}
}
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
return $filter_values ;
2011-08-02 00:37:34 +00:00
}
/**
* List locale translation filters that can be applied .
*/
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
function locale_translate_filters () {
2011-08-02 00:37:34 +00:00
$filters = array ();
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
// Get all languages, except English.
2011-08-02 00:37:34 +00:00
drupal_static_reset ( 'language_list' );
2012-04-25 23:44:20 +00:00
$languages = language_list ();
2012-01-23 15:46:29 +00:00
$language_options = array ();
foreach ( $languages as $langcode => $language ) {
if ( $langcode != 'en' || locale_translate_english ()) {
$language_options [ $langcode ] = $language -> name ;
}
2011-10-27 03:55:02 +00:00
}
2011-08-02 00:37:34 +00:00
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
// Pick the current interface language code for the filter.
$default_langcode = drupal_container () -> get ( LANGUAGE_TYPE_INTERFACE ) -> langcode ;
if ( ! isset ( $language_options [ $default_langcode ])) {
$available_langcodes = array_keys ( $language_options );
$default_langcode = array_shift ( $available_langcodes );
}
2011-08-02 00:37:34 +00:00
$filters [ 'string' ] = array (
'title' => t ( 'String contains' ),
'description' => t ( 'Leave blank to show all strings. The search is case sensitive.' ),
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'default' => '' ,
2011-08-02 00:37:34 +00:00
);
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$filters [ 'langcode' ] = array (
'title' => t ( 'Translation language' ),
'options' => $language_options ,
'default' => $default_langcode ,
2011-08-02 00:37:34 +00:00
);
$filters [ 'translation' ] = array (
'title' => t ( 'Search in' ),
2012-04-09 18:24:12 +00:00
'options' => array (
'all' => t ( 'Both translated and untranslated strings' ),
'translated' => t ( 'Only translated strings' ),
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'untranslated' => t ( 'Only untranslated strings' ),
2012-04-09 18:24:12 +00:00
),
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'default' => 'all' ,
2012-04-09 18:24:12 +00:00
);
$filters [ 'customized' ] = array (
'title' => t ( 'Translation type' ),
'options' => array (
'all' => t ( 'All' ),
LOCALE_NOT_CUSTOMIZED => t ( 'Non-customized translation' ),
LOCALE_CUSTOMIZED => t ( 'Customized translation' ),
),
'states' => array (
'visible' => array (
':input[name=translation]' => array ( 'value' => 'translated' ),
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
),
2012-04-09 18:24:12 +00:00
),
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'default' => 'all' ,
2011-08-02 00:37:34 +00:00
);
return $filters ;
}
/**
* Return form for locale translation filters .
*
* @ ingroup forms
*/
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
function locale_translate_filter_form ( $form , & $form_state ) {
$filters = locale_translate_filters ();
$filter_values = locale_translate_filter_values ();
$form [ '#attached' ][ 'css' ] = array (
drupal_get_path ( 'module' , 'locale' ) . '/locale.admin.css' ,
);
2011-08-02 00:37:34 +00:00
$form [ 'filters' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Filter translatable strings' ),
'#collapsible' => TRUE ,
'#collapsed' => FALSE ,
);
foreach ( $filters as $key => $filter ) {
// Special case for 'string' filter.
if ( $key == 'string' ) {
$form [ 'filters' ][ 'status' ][ 'string' ] = array (
2012-03-27 06:17:38 +00:00
'#type' => 'search' ,
2011-08-02 00:37:34 +00:00
'#title' => $filter [ 'title' ],
'#description' => $filter [ 'description' ],
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'#default_value' => $filter_values [ $key ],
2011-08-02 00:37:34 +00:00
);
}
else {
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$empty_option = isset ( $filter [ 'options' ][ $filter [ 'default' ]]) ? $filter [ 'options' ][ $filter [ 'default' ]] : '<none>' ;
2011-08-02 00:37:34 +00:00
$form [ 'filters' ][ 'status' ][ $key ] = array (
'#title' => $filter [ 'title' ],
'#type' => 'select' ,
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'#empty_value' => $filter [ 'default' ],
'#empty_option' => $empty_option ,
2011-08-02 00:37:34 +00:00
'#size' => 0 ,
'#options' => $filter [ 'options' ],
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'#default_value' => $filter_values [ $key ],
2011-08-02 00:37:34 +00:00
);
2012-04-09 18:24:12 +00:00
if ( isset ( $filter [ 'states' ])) {
$form [ 'filters' ][ 'status' ][ $key ][ '#states' ] = $filter [ 'states' ];
}
2011-08-02 00:37:34 +00:00
}
}
$form [ 'filters' ][ 'actions' ] = array (
'#type' => 'actions' ,
'#attributes' => array ( 'class' => array ( 'container-inline' )),
);
$form [ 'filters' ][ 'actions' ][ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Filter' ),
);
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
if ( ! empty ( $_SESSION [ 'locale_translate_filter' ])) {
2011-08-02 00:37:34 +00:00
$form [ 'filters' ][ 'actions' ][ 'reset' ] = array (
'#type' => 'submit' ,
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'#value' => t ( 'Reset' ),
2011-08-02 00:37:34 +00:00
);
}
return $form ;
}
/**
* Process result from locale translation filter form .
*/
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
function locale_translate_filter_form_submit ( $form , & $form_state ) {
2011-08-02 00:37:34 +00:00
$op = $form_state [ 'values' ][ 'op' ];
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$filters = locale_translate_filters ();
2011-08-02 00:37:34 +00:00
switch ( $op ) {
case t ( 'Filter' ) :
foreach ( $filters as $name => $filter ) {
if ( isset ( $form_state [ 'values' ][ $name ])) {
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$_SESSION [ 'locale_translate_filter' ][ $name ] = $form_state [ 'values' ][ $name ];
2011-08-02 00:37:34 +00:00
}
}
break ;
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
2011-08-02 00:37:34 +00:00
case t ( 'Reset' ) :
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$_SESSION [ 'locale_translate_filter' ] = array ();
2011-08-02 00:37:34 +00:00
break ;
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
2011-08-02 00:37:34 +00:00
}
$form_state [ 'redirect' ] = 'admin/config/regional/translate/translate' ;
}
/**
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
* User interface for string editing as one table .
2011-08-07 13:10:57 +00:00
*
* @ ingroup forms
2011-08-02 00:37:34 +00:00
*/
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
function locale_translate_edit_form ( $form , & $form_state ) {
$filter_values = locale_translate_filter_values ();
$langcode = $filter_values [ 'langcode' ];
drupal_static_reset ( 'language_list' );
$languages = language_list ();
$langname = isset ( $langcode ) ? $languages [ $langcode ] -> name : " <none> " ;
$path = drupal_get_path ( 'module' , 'locale' );
$form [ '#attached' ][ 'css' ] = array (
$path . '/locale.admin.css' ,
2011-08-02 00:37:34 +00:00
);
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$form [ '#attached' ][ 'js' ] = array (
$path . '/locale.admin.js' ,
2011-08-02 00:37:34 +00:00
);
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$form [ 'langcode' ] = array (
'#type' => 'value' ,
'#value' => $filter_values [ 'langcode' ],
);
2012-03-11 02:35:21 +00:00
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$form [ 'strings' ] = array (
'#type' => 'item' ,
'#tree' => TRUE ,
'#language' => $langname ,
'#theme' => 'locale_translate_edit_form_strings' ,
2012-03-11 02:35:21 +00:00
);
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
if ( isset ( $langcode )) {
$strings = locale_translate_query ();
$plural_formulas = variable_get ( 'locale_translation_plurals' , array ());
foreach ( $strings as $string ) {
// Split source to work with plural values.
$source_array = explode ( LOCALE_PLURAL_DELIMITER , $string -> source );
$translation_array = explode ( LOCALE_PLURAL_DELIMITER , $string -> translation );
if ( count ( $source_array ) == 1 ) {
// Add original string value and mark as non-plural.
$form [ 'strings' ][ $string -> lid ][ 'plural' ] = array (
'#type' => 'value' ,
'#value' => 0 ,
);
$form [ 'strings' ][ $string -> lid ][ 'original' ] = array (
'#type' => 'item' ,
'#title' => t ( 'Source string' ),
'#title_display' => 'invisible' ,
'#markup' => check_plain ( $source_array [ 0 ]),
);
2012-03-11 02:35:21 +00:00
}
else {
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
// Add original string value and mark as plural.
$form [ 'strings' ][ $string -> lid ][ 'plural' ] = array (
'#type' => 'value' ,
'#value' => 1 ,
);
$form [ 'strings' ][ $string -> lid ][ 'original_singular' ] = array (
'#type' => 'item' ,
2012-06-12 12:27:48 +00:00
'#title' => t ( 'Singular form' ),
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'#markup' => check_plain ( $source_array [ 0 ]),
2012-03-11 02:35:21 +00:00
);
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$form [ 'strings' ][ $string -> lid ][ 'original_plural' ] = array (
'#type' => 'item' ,
2012-03-11 02:35:21 +00:00
'#title' => t ( 'Plural form' ),
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'#markup' => check_plain ( $source_array [ 1 ]),
);
}
if ( ! empty ( $string -> context )) {
$form [ 'strings' ][ $string -> lid ][ 'context' ] = array (
'#type' => 'value' ,
'#value' => check_plain ( $string -> context ),
);
}
$form [ 'strings' ][ $string -> lid ][ 'location' ] = array (
'#type' => 'value' ,
'#value' => $string -> location ,
);
// Approximate the number of rows to use in the default textarea.
$rows = min ( ceil ( str_word_count ( $source_array [ 0 ]) / 12 ), 10 );
if ( empty ( $form [ 'strings' ][ $string -> lid ][ 'plural' ][ '#value' ])) {
$form [ 'strings' ][ $string -> lid ][ 'translations' ][ 0 ] = array (
'#type' => 'textarea' ,
'#title' => t ( 'Translated string' ),
'#title_display' => 'invisible' ,
2012-03-11 02:35:21 +00:00
'#rows' => $rows ,
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'#default_value' => $translation_array [ 0 ],
2012-03-11 02:35:21 +00:00
);
}
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
else {
// Dealing with plural strings.
if ( isset ( $plural_formulas [ $langcode ][ 'plurals' ]) && $plural_formulas [ $langcode ][ 'plurals' ] > 2 ) {
// Add a textarea for each plural variant.
for ( $i = 0 ; $i < $plural_formulas [ $langcode ][ 'plurals' ]; $i ++ ) {
$form [ 'strings' ][ $string -> lid ][ 'translations' ][ $i ] = array (
'#type' => 'textarea' ,
'#title' => ( $i == 0 ? t ( 'Singular form' ) : format_plural ( $i , 'First plural form' , '@count. plural form' )),
'#rows' => $rows ,
'#default_value' => isset ( $translation_array [ $i ]) ? $translation_array [ $i ] : '' ,
);
}
}
else {
// Fallback for unknown number of plurals.
$form [ 'strings' ][ $string -> lid ][ 'translations' ][ 0 ] = array (
'#type' => 'textarea' ,
'#title' => t ( 'Singular form' ),
'#rows' => $rows ,
'#default_value' => $translation_array [ 0 ],
);
$form [ 'strings' ][ $string -> lid ][ 'translations' ][ 1 ] = array (
'#type' => 'textarea' ,
'#title' => t ( 'Plural form' ),
'#rows' => $rows ,
'#default_value' => isset ( $translation_array [ 1 ]) ? $translation_array [ 1 ] : '' ,
);
}
}
2012-03-11 02:35:21 +00:00
}
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
if ( count ( element_children ( $form [ 'strings' ]))) {
$form [ 'actions' ] = array ( '#type' => 'actions' );
$form [ 'actions' ][ 'submit' ] = array ( '#type' => 'submit' , '#value' => t ( 'Save translations' ));
2012-03-11 02:35:21 +00:00
}
2011-08-02 00:37:34 +00:00
}
return $form ;
}
/**
* Validate string editing form submissions .
*/
function locale_translate_edit_form_validate ( $form , & $form_state ) {
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$langcode = $form_state [ 'values' ][ 'langcode' ];
foreach ( $form_state [ 'values' ][ 'strings' ] as $lid => $translations ) {
foreach ( $translations [ 'translations' ] as $key => $value ) {
2012-03-11 02:35:21 +00:00
if ( ! locale_string_is_safe ( $value )) {
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
form_set_error ( " strings][ $lid ][translations][ $key " , t ( 'The submitted string contains disallowed HTML: %string' , array ( '%string' => $value )));
2012-03-11 02:35:21 +00:00
form_set_error ( " translations][ $langcode ][ $key " , t ( 'The submitted string contains disallowed HTML: %string' , array ( '%string' => $value )));
watchdog ( 'locale' , 'Attempted submission of a translation string with disallowed HTML: %string' , array ( '%string' => $value ), WATCHDOG_WARNING );
}
2011-08-02 00:37:34 +00:00
}
}
}
/**
* Process string editing form submissions .
*/
function locale_translate_edit_form_submit ( $form , & $form_state ) {
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$langcode = $form_state [ 'values' ][ 'langcode' ];
foreach ( $form_state [ 'values' ][ 'strings' ] as $lid => $translations ) {
2012-03-11 02:35:21 +00:00
// Serialize plural variants in one string by LOCALE_PLURAL_DELIMITER.
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$translation_new = implode ( LOCALE_PLURAL_DELIMITER , $translations [ 'translations' ]);
$translation_old = db_query ( " SELECT translation FROM { locales_target} WHERE lid = :lid AND language = :language " , array ( ':lid' => $lid , ':language' => $langcode )) -> fetchField ();
2012-03-11 02:35:21 +00:00
// No translation when all strings are empty.
$has_translation = FALSE ;
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
foreach ( $translations [ 'translations' ] as $string ) {
2012-03-11 02:35:21 +00:00
if ( ! empty ( $string )) {
$has_translation = TRUE ;
break ;
}
}
if ( $has_translation ) {
2011-08-02 00:37:34 +00:00
// Only update or insert if we have a value to use.
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
if ( ! empty ( $translation_old ) && $translation_old != $translation_new ) {
2011-08-02 00:37:34 +00:00
db_update ( 'locales_target' )
-> fields ( array (
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'translation' => $translation_new ,
2012-04-09 18:24:12 +00:00
'customized' => LOCALE_CUSTOMIZED ,
2011-08-02 00:37:34 +00:00
))
-> condition ( 'lid' , $lid )
2012-03-11 02:35:21 +00:00
-> condition ( 'language' , $langcode )
2011-08-02 00:37:34 +00:00
-> execute ();
}
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
if ( empty ( $translation_old )) {
2011-08-02 00:37:34 +00:00
db_insert ( 'locales_target' )
-> fields ( array (
'lid' => $lid ,
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
'translation' => $translation_new ,
2012-03-11 02:35:21 +00:00
'language' => $langcode ,
2012-04-09 18:24:12 +00:00
'customized' => LOCALE_CUSTOMIZED ,
2011-08-02 00:37:34 +00:00
))
-> execute ();
}
}
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
elseif ( ! empty ( $translation_old )) {
2011-08-02 00:37:34 +00:00
// Empty translation entered: remove existing entry from database.
db_delete ( 'locales_target' )
-> condition ( 'lid' , $lid )
2012-03-11 02:35:21 +00:00
-> condition ( 'language' , $langcode )
2011-08-02 00:37:34 +00:00
-> execute ();
}
}
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
drupal_set_message ( t ( 'The strings have been saved.' ));
// Keep the user on the current pager page.
if ( isset ( $_GET [ 'page' ])) {
$form_state [ 'redirect' ] = array ( 'admin/config/regional/translate' , array ( 'query' => array ( 'page' => $_GET [ 'page' ])));
}
2011-08-02 00:37:34 +00:00
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
// Force JavaScript translation file recreation for this language.
_locale_invalidate_js ( $langcode );
2011-08-02 00:37:34 +00:00
// Clear locale cache.
2011-09-11 16:14:18 +00:00
cache () -> deletePrefix ( 'locale:' );
2011-08-02 00:37:34 +00:00
}
/**
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
* Default theme function for translatione edit form .
2011-08-02 00:37:34 +00:00
*/
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
function theme_locale_translate_edit_form_strings ( $variables ) {
$output = '' ;
$form = $variables [ 'form' ];
$header = array (
t ( 'Source string' ),
t ( 'Translation for @language' , array ( '@language' => $form [ '#language' ])),
);
$rows = array ();
foreach ( element_children ( $form ) as $lid ) {
$string = $form [ $lid ];
if ( $string [ 'plural' ][ '#value' ]) {
$source = drupal_render ( $string [ 'original_singular' ]) . '<br />' . drupal_render ( $string [ 'original_plural' ]);
}
else {
$source = drupal_render ( $string [ 'original' ]);
}
$source .= empty ( $string [ 'context' ]) ? '' : '<br /><small>' . t ( 'In Context' ) . ': ' . $string [ 'context' ][ '#value' ] . '</small>' ;
$rows [] = array (
array ( 'data' => $source ),
array ( 'data' => $string [ 'translations' ]),
);
2011-08-02 00:37:34 +00:00
}
Issue #1452188 by Schnitzel, droplet, Sutharsan, Bojhan, Kristen Pol, Gábor Hojtsy, ershov.andrey, perusio, nod_, rvilar, andypost: Added New UI for string translation.
2012-06-15 10:01:31 +00:00
$output .= theme ( 'table' , array (
'header' => $header ,
'rows' => $rows ,
'empty' => t ( 'No strings available.' ),
'attributes' => array ( 'class' => array ( 'locale-translate-edit-table' )),
));
$output .= theme ( 'pager' );
return $output ;
2011-08-02 00:37:34 +00:00
}