View source
<?php
define('L10N_CLIENT_STRINGS', 100);
function l10n_client_menu($may_cache) {
$items = array();
if ($may_cache) {
$access = user_access('use on-page translation');
$items[] = array(
'path' => 'locale',
'title' => t('Translate interface'),
'callback' => 'l10n_client_translate_page',
'access' => $access,
);
$items[] = array(
'path' => 'locale/untranslated',
'title' => t('Untranslated'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[] = array(
'path' => 'locale/translated',
'title' => t('Translated'),
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'l10n_client/save',
'title' => 'Save string',
'callback' => 'l10n_client_save_string',
'access' => $access,
'type' => MENU_CALLBACK,
);
}
return $items;
}
function l10n_client_translate_page($op = 'untranslated') {
global $locale, $l10n_client_strings;
$output = '';
$sql = "SELECT s.source, t.translation, t.locale FROM {locales_source} s ";
switch ($op) {
case 'translated':
$sql .= " INNER JOIN {locales_target} t ON s.lid = t.lid";
$sql .= " WHERE t.locale = '%s' AND t.translation != ''";
break;
case 'untranslated':
default:
$sql .= " LEFT JOIN {locales_target} t ON s.lid = t.lid";
$sql .= " WHERE t.locale = '%s' AND (t.translation IS NULL OR t.translation = '')";
break;
}
$sql .= ' ORDER BY s.source';
$result = pager_query($sql, L10N_CLIENT_STRINGS, 0, NULL, $locale);
while ($data = db_fetch_object($result)) {
$list[] = array(
check_plain($data->source),
check_plain($data->translation),
);
$l10n_client_strings[$data->source] = empty($data->translation) ? TRUE : $data->translation;
}
if (!empty($list)) {
$output .= theme('pager', NULL, L10N_CLIENT_STRINGS);
$output .= theme('table', array(), $list);
$output .= theme('pager', NULL, L10N_CLIENT_STRINGS);
}
else {
$output .= t('No strings to translate');
}
return $output;
}
function l10n_client_perm() {
return array(
'use on-page translation',
);
}
function l10n_client_init() {
global $user, $conf, $locale;
if (function_exists('user_access') && user_access('use on-page translation')) {
$conf['locale_cache_strings'] = 0;
drupal_add_css(drupal_get_path('module', 'l10n_client') . '/l10n_client.css', 'module');
drupal_add_js(drupal_get_path('module', 'l10n_client') . '/jquery.cookie.js', 'module');
drupal_add_js(drupal_get_path('module', 'l10n_client') . '/l10n_client.js', 'module');
drupal_add_js('misc/textarea.js', 'module');
}
}
function l10n_client_footer() {
global $conf, $locale;
if (user_access('use on-page translation') && ($strings = _l10n_client_page_strings())) {
$l10n_strings = array();
foreach ($strings as $string => $translation) {
$l10n_strings[] = is_object($translation) ? array(
$string,
empty($translation->translation) ? TRUE : $translation->translation,
$translation->lid,
) : array(
$string,
$translation,
0,
);
}
array_multisort($l10n_strings);
$string_list = _l10n_client_string_list($l10n_strings);
$l10n_form = drupal_get_form('l10n_client_form', $l10n_strings);
$l10n_search = drupal_get_form('l10n_client_search_form');
$l10n_dom = _l10n_client_dom_strings($l10n_strings);
$string_label = '<h2>' . t('Page Text') . '</h2>';
$source_label = '<h2>' . t('Source') . '</h2>';
$languages = locale_supported_languages();
$languages = $languages['name'];
$translation_label = '<h2>' . t('Translation to %language', array(
'%language' => $languages[$locale],
)) . '</h2>';
$toggle_label = t('Translate Text');
$output = "\n <div id='l10n-client' class='hidden'>\n <div class='labels'>\n <span class='toggle'>{$toggle_label}</span>\n <div class='label strings'>{$string_label}</div>\n <div class='label source'>{$source_label}</div>\n <div class='label translation'>{$translation_label}</div>\n </div>\n <div id='l10n-client-string-select'>\n {$string_list}\n {$l10n_search}\n </div>\n <div id='l10n-client-string-editor'>\n <div class='source'>\n <div class='source-text'></div>\n </div>\n <div class='translation'>\n {$l10n_form}\n </div>\n </div>\n </div>\n {$l10n_dom}\n ";
return $output;
}
}
function _l10n_client_string_list($strings) {
foreach ($strings as $values) {
if ($values[1] === TRUE) {
$str_class = 'untranslated';
}
else {
$str_class = 'translated';
}
$original = $values[1] === TRUE ? $values[0] : $values[1];
$original = htmlentities($original);
$string = strip_tags(truncate_utf8($values[1] === TRUE ? $values[0] : $values[1], 78, TRUE));
$select_list[] = "<li class='{$str_class}'>" . $string . ($original == $string ? '' : '...') . "</li>";
}
$output = implode("\n", $select_list);
return "<ul class='string-list'>{$output}</ul>";
}
function _l10n_client_page_strings() {
global $locale, $l10n_client_strings;
$strings = !empty($l10n_client_strings) ? $l10n_client_strings : array();
if (arg(0) != 'locale' && is_array($locale_strings = locale())) {
$strings = array_merge($strings, $locale_strings);
$result = db_query("SELECT s.source, t.translation FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.locale = '%s' WHERE location = '%s'", $locale, request_uri());
while ($data = db_fetch_object($result)) {
if (!array_key_exists($data->source, $strings)) {
$strings[$data->source] = empty($data->translation) ? TRUE : $data->translation;
}
}
}
return $strings;
}
function l10n_client_form($strings) {
global $language;
$form = array();
$form['#action'] = url('l10n_client/save');
$form['target'] = array(
'#type' => 'textarea',
'#resizable' => false,
'#rows' => 6,
);
$form['save'] = array(
'#value' => t('Save translation'),
'#type' => 'submit',
);
$form['location'] = array(
'#type' => 'hidden',
'#value' => request_uri(),
);
$form['lid'] = array(
'#type' => 'hidden',
'#value' => 0,
);
$form['copy'] = array(
'#value' => "<input id='edit-copy' class='form-submit' type='button' value='" . t('Copy Source') . "'/>",
);
$form['clear'] = array(
'#value' => "<input id='edit-clear' class='form-submit' type='button' value='" . t('Clear') . "'/>",
);
return $form;
}
function l10n_client_search_form() {
global $language;
$form = array();
$form['search'] = array(
'#type' => 'textfield',
);
$form['search-button'] = array(
'#value' => "<input id='search-filter-go' class='form-submit' type='button' value='" . t('Search') . "'/>",
);
$form['clear-button'] = array(
'#value' => "<input id='search-filter-clear' class='form-submit' type='button' value='" . t('X') . "'/>",
);
return $form;
}
function _l10n_client_dom_strings($strings) {
$output = '';
foreach ($strings as $values) {
$lid = $values[2];
$source = $values[0] === TRUE ? '' : htmlspecialchars($values[0], ENT_NOQUOTES, 'UTF-8');
$target = $values[1] === TRUE ? '' : htmlspecialchars($values[1], ENT_NOQUOTES, 'UTF-8');
$output .= "<div><span class='lid'>{$lid}</span><span class='source'>{$source}</span><span class='target'>{$target}</span></div>";
}
return "<div id='l10n-client-data'>{$output}</div>";
}
function l10n_client_save_string() {
global $locale;
if (user_access('use on-page translation')) {
if (isset($_POST['source']) && isset($_POST['target']) && !empty($_POST['form_token']) && drupal_valid_token($_POST['form_token'], 'l10n_client_form')) {
include_once 'includes/locale.inc';
$report = array(
0,
0,
0,
);
_l10n_client_import_one_string_db($report, $locale, $_POST['lid'], $_POST['source'], $_POST['target'], $_POST['location']);
locale_refresh_cache();
}
}
}
function _l10n_client_import_one_string_db(&$report, $langcode, $lid, $source, $translation, $location, $plid = NULL, $plural = NULL) {
if (!$lid) {
$lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $source));
}
if (!empty($translation)) {
if ($lid) {
$exists = (bool) db_result(db_query("SELECT lid FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $langcode));
if (!$exists) {
db_query("INSERT INTO {locales_target} (lid, locale, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural);
$report[0]++;
}
else {
db_query("UPDATE {locales_target} SET translation = '%s', plid = %d, plural = %d WHERE locale = '%s' AND lid = %d", $translation, $plid, $plural, $langcode, $lid);
$report[1]++;
}
}
else {
db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", $location, $source);
$lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' ", $source));
db_query("INSERT INTO {locales_target} (lid, locale, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural);
$report[0]++;
}
}
else {
db_query("DELETE FROM {locales_target} WHERE language = '%s' AND lid = %d AND plid = %d AND plural = %d", $translation, $langcode, $lid, $plid, $plural);
$report[2]++;
}
return $lid;
}