Newer
Older
Gábor Hojtsy
committed
<?php
/**
* @file
* Internationalization (i18n) package - translatable strings reusable admin UI.
*
* @author Jose A. Reyero, 2007
*/
Jose Antonio Reyero del Prado
committed
/**
* Generate translate page from object
*/
function i18n_string_translate_page_object($object_type, $object_value, $langcode = NULL) {
// Get base keys for all these strings. Object key may be multiple like for blocks (module, delta)
Jose Antonio Reyero del Prado
committed
$object = i18n_object($object_type, $object_value);
$strings = $object->get_strings(array('empty' => TRUE));
Gábor Hojtsy
committed
if (empty($langcode)) {
Jose Antonio Reyero del Prado
committed
drupal_set_title(t('Translate @name', array('@name' => i18n_object_info($object_type, 'title'))));
return i18n_string_translate_page_overview($object, $strings);
Gábor Hojtsy
committed
}
else {
Jose Antonio Reyero del Prado
committed
drupal_set_title(t('Translate to @language', array('@language' => i18n_language_name($langcode))));
Jose Antonio Reyero del Prado
committed
return drupal_get_form('i18n_string_translate_page_form', $strings, $langcode);
Gábor Hojtsy
committed
}
}
/**
* Provide a core translation module like overview page for this object.
*/
Jose Antonio Reyero del Prado
committed
function i18n_string_translate_page_overview($object, $strings) {
Gábor Hojtsy
committed
include_once DRUPAL_ROOT . '/includes/language.inc';
Jose Antonio Reyero del Prado
committed
// Set the default item key, assume it's the first.
$item_title = reset($strings);
$header = array(t('Language'), t('Title'), t('Status'), t('Operations'));
Jose Antonio Reyero del Prado
committed
$source_language = variable_get_value('i18n_string_source_language');
Gábor Hojtsy
committed
$rows = array();
foreach (language_list() as $langcode => $language) {
Jose Antonio Reyero del Prado
committed
if ($langcode == $source_language) {
Gábor Hojtsy
committed
$rows[] = array(
$language->name . ' ' . t('(source)'),
Jose Antonio Reyero del Prado
committed
check_plain($item_title->get_string()),
Gábor Hojtsy
committed
t('original'),
Jose Antonio Reyero del Prado
committed
l(t('edit'), $object->get_edit_path()),
Gábor Hojtsy
committed
);
}
else {
// Try to figure out if this item has any of its properties translated.
$translated = FALSE;
Jose Antonio Reyero del Prado
committed
foreach ($strings as $i18nstring) {
Jose Antonio Reyero del Prado
committed
if ($i18nstring->get_translation($langcode)) {
Gábor Hojtsy
committed
$translated = TRUE;
break;
}
}
// Translate the item that was requested to be displayed as title.
$rows[] = array(
$language->name,
$item_title->format_translation($langcode, array('sanitize default' => TRUE)),
Gábor Hojtsy
committed
$translated ? t('translated') : t('not translated'),
Jose Antonio Reyero del Prado
committed
l(t('translate'), $object->get_translate_path($langcode), array('query' => drupal_get_destination())),
Gábor Hojtsy
committed
);
}
}
$build['i18n_string_translation_overview'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
return $build;
}
/**
* Form builder callback for in-place string translation.
*/
Jose Antonio Reyero del Prado
committed
function i18n_string_translate_page_form($form, &$form_state, $strings, $langcode) {
$form = i18n_string_translate_page_form_base($form, $langcode);
$form['strings'] += i18n_string_translate_page_form_strings($strings, $langcode);
return $form;
}
/**
* Create base form for string translation
*/
function i18n_string_translate_page_form_base($form, $langcode, $redirect = NULL) {
Gábor Hojtsy
committed
$form['langcode'] = array(
'#type' => 'value',
'#value' => $langcode,
);
$form['strings'] = array(
// Use a tree, so we can access the values easily.
'#tree' => TRUE,
);
Jose Antonio Reyero del Prado
committed
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save translation'),
);
if ($redirect) {
$form['#redirect'] = array(
$redirect,
);
}
// Add explicit validate and submit hooks so this can be used from inside any form.
Jose Antonio Reyero del Prado
committed
$form['#validate'] = array('i18n_string_translate_page_form_validate');
$form['#submit'] = array('i18n_string_translate_page_form_submit');
return $form;
}
/**
* Create field elements for strings
*/
function i18n_string_translate_page_form_strings($strings, $langcode) {
$formats = filter_formats();
foreach ($strings as $item) {
$disabled = FALSE;
$description = '';
Jose Antonio Reyero del Prado
committed
// We may have a source or not. Maybe the format is disallowed for all.
Jose Antonio Reyero del Prado
committed
$source = $item->get_source();
Jose Antonio Reyero del Prado
committed
$format_id = $source ? $source->format : $item->format;
if ($format_id) {
$format = filter_format_load($format_id);
$disabled = !i18n_string_translate_access($item);
Jose Antonio Reyero del Prado
committed
if ($disabled) {
$description = t('This string uses the %name text format. You are not allowed to translate or edit texts with this format.', array('%name' => $format->name));
}
else {
Jose Antonio Reyero del Prado
committed
$description = '<h5>' . t('Text format: @name', array('@name' => $format->name)) . '</h5>' . theme('filter_tips', array('tips' => _filter_tips($format->format, FALSE)));
Gábor Hojtsy
committed
}
}
Jose Antonio Reyero del Prado
committed
$default_value = $item->format_translation($langcode, array('langcode' => $langcode, 'sanitize' => FALSE, 'debug' => FALSE));
Jose Antonio Reyero del Prado
committed
$form[$item->get_name()] = array(
Jose Antonio Reyero del Prado
committed
'#title' => $item->get_title(),
Gábor Hojtsy
committed
'#type' => 'textarea',
'#default_value' => $default_value,
'#disabled' => $disabled,
'#description' => $description,
'#i18n_string_format' => !empty($source) ? $source->format : 0,
'#rows' => min(ceil(str_word_count($default_value) / 12), 10),
Jose Antonio Reyero del Prado
committed
'#parents' => array('strings', $item->get_name()),
Gábor Hojtsy
committed
);
}
Jose Antonio Reyero del Prado
committed
return $form;
Gábor Hojtsy
committed
}
/**
* Validation submission callback for in-place string translation.
*/
function i18n_string_translate_page_form_validate($form, &$form_state) {
foreach ($form_state['values']['strings'] as $key => $value) {
Gábor Hojtsy
committed
// We don't need to validate disabled form fields because those are already
// validated by the FormAPI.
if (empty($form['strings'][$key]['#i18n_string_format'])) {
i18n_string_validate_submission("strings][$key", $value);
}
}
}
/**
* Form submission callback for in-place string translation.
*/
function i18n_string_translate_page_form_submit($form, &$form_state) {
foreach ($form_state['values']['strings'] as $key => $value) {
Gábor Hojtsy
committed
list($textgroup, $context) = i18n_string_context(explode(':', $key));
i18n_string_textgroup($textgroup)->update_translation($context, $form_state['values']['langcode'], $value);
}
drupal_set_message(t('Translations saved.'));
Jose Antonio Reyero del Prado
committed
if (isset($form['#redirect'])) {
$form_state['redirect'] = $form['#redirect'];
}
Gábor Hojtsy
committed
}
/**
* String submission validation callback.
*/
function i18n_string_validate_submission($formkey, $value) {
// Validation based on locale_translate_edit_form_validate.
if (!locale_string_is_safe($value)) {
form_set_error($formkey, 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);
}
}
Jose Antonio Reyero del Prado
committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Menu callback. Saves a string translation coming as POST data.
*/
function i18n_string_l10n_client_save_string() {
global $user, $language;
if (user_access('use on-page translation')) {
$textgroup = !empty($_POST['textgroup']) ? $_POST['textgroup'] : 'default';
// Default textgroup will be handled by l10n_client module
if ($textgroup == 'default') {
return l10n_client_save_string();
}
elseif (isset($_POST['source']) && isset($_POST['target']) && !empty($_POST['context']) && !empty($_POST['form_token']) && drupal_valid_token($_POST['form_token'], 'l10n_client_form')) {
$name = $textgroup . ':' . $_POST['context'];
$result = i18n_string_translation_update($name, $_POST['target'], $language->language, $_POST['source']);
if ($result) {
$message = theme('l10n_client_message', array('message' => t('Translation saved locally for user defined string.'), 'level' => WATCHDOG_INFO));
}
elseif ($result === FALSE) {
$message = theme('l10n_client_message', array('message' => t('Not saved due to insufficient permissions.')));
}
else {
$message = theme('l10n_client_message', array('message' => t('Not saved due to source string missing.')));
}
}
else {
$message = theme('l10n_client_message', array('message' => t('Not saved due to missing form values.')));
}
drupal_json_output($message);
exit;
}
}