| 1 |
<?php
|
| 2 |
// $Id: translatable.block.inc,v 1.4 2008/02/20 22:30:44 sun Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides language and translation language blocks and user integration.
|
| 7 |
* @todo Move to .module.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_block().
|
| 12 |
*/
|
| 13 |
function translatable_block($op = 'list', $delta = 0, $edit = array()) {
|
| 14 |
switch ($op) {
|
| 15 |
case 'list':
|
| 16 |
$blocks['ui'] = array('info' => t('Language'), 'region' => 'right', 'status' => 1);
|
| 17 |
$blocks['translation']['info'] = t('Translation language');
|
| 18 |
$blocks['translation']['region'] = 'right';
|
| 19 |
$blocks['translation']['status'] = 1;
|
| 20 |
$blocks['translation']['weight'] = -10;
|
| 21 |
return $blocks;
|
| 22 |
|
| 23 |
case 'configure':
|
| 24 |
$form = array();
|
| 25 |
if ($delta == 'ui' || $delta == 'translation') {
|
| 26 |
$form['translatable_block_'. $delta .'_hidecurrentlocale'] = array(
|
| 27 |
'#type' => 'checkbox',
|
| 28 |
'#title' => t('Hide current language'),
|
| 29 |
'#default_value' => variable_get('translatable_block_'. $delta .'_hidecurrentlocale', TRUE),
|
| 30 |
'#description' => t('If this option is enabled, the current language is not displayed in the language switching block.'),
|
| 31 |
);
|
| 32 |
$flag_options = array(
|
| 33 |
'flag' => t('Show flags'),
|
| 34 |
'name' => t('Show language names'),
|
| 35 |
'flag-name' => t('Show flags and language names'),
|
| 36 |
);
|
| 37 |
$form['translatable_block_'. $delta .'_style'] = array(
|
| 38 |
'#type' => 'select',
|
| 39 |
'#title' => t('Display style'),
|
| 40 |
'#options' => $flag_options,
|
| 41 |
'#default_value' => variable_get('translatable_block_'. $delta .'_style', 'flag-name'),
|
| 42 |
);
|
| 43 |
$form['translatable_block_'. $delta .'_flagspath'] = array(
|
| 44 |
'#type' => 'textfield',
|
| 45 |
'#title' => t('Flag icons path'),
|
| 46 |
'#default_value' => variable_get('translatable_block_'. $delta .'_flagspath', drupal_get_path('module', 'translatable') .'/flags/*.png'),
|
| 47 |
'#size' => 50,
|
| 48 |
'#maxlength' => 180,
|
| 49 |
'#description' => t("Path to flag images, relative to the Drupal installation directory. '*' will be replaced with the ISO language code."),
|
| 50 |
);
|
| 51 |
}
|
| 52 |
return $form;
|
| 53 |
|
| 54 |
case 'save':
|
| 55 |
if ($delta == 'ui' || $delta == 'translation') {
|
| 56 |
variable_set('translatable_block_'. $delta .'_hidecurrentlocale', $edit['translatable_block_'. $delta .'_hidecurrentlocale']);
|
| 57 |
variable_set('translatable_block_'. $delta .'_style', $edit['translatable_block_'. $delta .'_style']);
|
| 58 |
variable_set('translatable_block_'. $delta .'_flagspath', $edit['translatable_block_'. $delta .'_flagspath']);
|
| 59 |
}
|
| 60 |
return;
|
| 61 |
|
| 62 |
case 'view':
|
| 63 |
if ($delta == 'ui') {
|
| 64 |
$block['subject'] = t('Language');
|
| 65 |
$languages = theme('translatable_languages', 'ui');
|
| 66 |
$block['content'] = theme('item_list', theme('translatable_language_link', $languages));
|
| 67 |
}
|
| 68 |
else if ($delta == 'translation') {
|
| 69 |
$block['subject'] = t('Translation language');
|
| 70 |
$languages = translatable_adminlocale_formselect(NULL, NULL);
|
| 71 |
if (!empty($languages)) {
|
| 72 |
$block['content'] = theme('item_list', theme('translatable_language_link', $languages));
|
| 73 |
}
|
| 74 |
}
|
| 75 |
return $block;
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Create the links for the switching language block
|
| 81 |
*
|
| 82 |
* @param string $block
|
| 83 |
* The Translatable block name, can be 'ui', 'content' or 'translation'.
|
| 84 |
* @param array $form
|
| 85 |
* The form on which the languages should be based on.
|
| 86 |
*
|
| 87 |
* @return
|
| 88 |
* A HTML list of links of available languages.
|
| 89 |
*/
|
| 90 |
function theme_translatable_languages($block = 'ui', $form = NULL) {
|
| 91 |
global $user;
|
| 92 |
static $tset, $nodes, $translation_form;
|
| 93 |
|
| 94 |
drupal_add_css(drupal_get_path('module', 'translatable') .'/translatable.css');
|
| 95 |
$style = variable_get('translatable_block_'. $block .'_style', 'flag-name');
|
| 96 |
|
| 97 |
// Cache the form for subsequent requests.
|
| 98 |
if (!empty($form)) {
|
| 99 |
$translation_form = $form;
|
| 100 |
}
|
| 101 |
|
| 102 |
// When editing a node, fetch existent translations.
|
| 103 |
if (!isset($tset) && arg(0) == 'node' && arg(2) == 'edit') {
|
| 104 |
$tset = translatable_findbyid('node', arg(1));
|
| 105 |
if ($tset) {
|
| 106 |
$nodes = translatable_find('node', "tnid = ". $tset['tnid']);
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
| 110 |
foreach (translatable_available_languages() as $lang => $langname) {
|
| 111 |
// Skip active language if enabled.
|
| 112 |
if (variable_get('translatable_block_'. $block .'_hidecurrentlocale', FALSE) && $lang == translatable_get_locale()) {
|
| 113 |
continue;
|
| 114 |
}
|
| 115 |
|
| 116 |
$langname = translatable_t($langname, 0, $lang);
|
| 117 |
|
| 118 |
switch ($style) {
|
| 119 |
case 'flag':
|
| 120 |
$title = theme('translatable_flag', $lang, $langname, $block);
|
| 121 |
break;
|
| 122 |
|
| 123 |
case 'name':
|
| 124 |
$title = $langname;
|
| 125 |
break;
|
| 126 |
|
| 127 |
case 'flag-name':
|
| 128 |
$title = theme('translatable_flag', $lang, $langname, $block) . $langname;
|
| 129 |
break;
|
| 130 |
}
|
| 131 |
$attribs = array('title' => $langname, 'class' => 'language');
|
| 132 |
$absolute = FALSE;
|
| 133 |
|
| 134 |
// Determine redirect destination for links in translation language block
|
| 135 |
// or both blocks, if user does not prefer separate languages.
|
| 136 |
// @todo Only for enabled content types!
|
| 137 |
$destination = NULL;
|
| 138 |
if (arg(0) == 'node' && ($block == 'translation' || !$user->switch_adminlocale)) {
|
| 139 |
if (arg(1) == 'add') {
|
| 140 |
// When creating a new node, we simply switch the language and reload.
|
| 141 |
$destination = $_GET['q'];
|
| 142 |
}
|
| 143 |
else if (isset($nodes)) {
|
| 144 |
// When editing a node, we need to search all translations for the
|
| 145 |
// current parent id to build corresponding links.
|
| 146 |
$node = FALSE;
|
| 147 |
foreach ($nodes as $tnode) {
|
| 148 |
if ($tnode['language'] == $lang) {
|
| 149 |
$node = $tnode;
|
| 150 |
break;
|
| 151 |
}
|
| 152 |
}
|
| 153 |
if ($node) {
|
| 154 |
// A translation exists; redirect to regular node edit page.
|
| 155 |
$destination = 'node/'. $node['nid'] .'/edit';
|
| 156 |
}
|
| 157 |
else {
|
| 158 |
// No translation exists; redirect to our callback.
|
| 159 |
$destination = 'node/'. $tset['tnid'] .'/edit/translation/'. $lang;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
}
|
| 163 |
|
| 164 |
switch ($block) {
|
| 165 |
case 'ui':
|
| 166 |
if (!isset($destination)) {
|
| 167 |
$destination = translatable_get_destination($lang);
|
| 168 |
if ($destination == '') {
|
| 169 |
$destination = variable_get('site_frontpage', 'node');
|
| 170 |
}
|
| 171 |
if (strpos($destination, 'destination=') !== FALSE) {
|
| 172 |
$destination = $_GET['q'];
|
| 173 |
}
|
| 174 |
}
|
| 175 |
if (variable_get('translatable_switch_byhostname', FALSE)) {
|
| 176 |
$absolute = TRUE;
|
| 177 |
$host = variable_get('translatable_switch_hostname_'. $lang, $lang .'.'. translatable_get_domain());
|
| 178 |
$url = 'http://'. $host . $GLOBALS['base_path'];
|
| 179 |
if (variable_get('clean_url', 0)) {
|
| 180 |
$url .= $destination;
|
| 181 |
$query = NULL;
|
| 182 |
}
|
| 183 |
else {
|
| 184 |
$query = 'q='. $destination;
|
| 185 |
}
|
| 186 |
}
|
| 187 |
else {
|
| 188 |
$url = 'switchuilocale/'. $lang;
|
| 189 |
$query = 'destination='. drupal_urlencode($destination);
|
| 190 |
}
|
| 191 |
$attribs['class'] .= ($lang == translatable_get_locale() ? ' active' : '');
|
| 192 |
break;
|
| 193 |
|
| 194 |
case 'translation':
|
| 195 |
if (isset($destination)) {
|
| 196 |
$attribs['class'] .= ($lang == $form['language']['#default_value'] || $lang == translatable_get_adminlocale() ? ' active' : '');
|
| 197 |
}
|
| 198 |
else {
|
| 199 |
// Simple translation form.
|
| 200 |
$destination = $_GET['q'];
|
| 201 |
$attribs['class'] .= ($lang == translatable_get_adminlocale() ? ' active' : '');
|
| 202 |
}
|
| 203 |
if (isset($translation_form)) {
|
| 204 |
$message = t('Please wait...');
|
| 205 |
$attribs['onclick'] = "$('#". str_replace('_', '-', $translation_form['#id']) ."').block('$message');";
|
| 206 |
}
|
| 207 |
$url = 'switchadminlocale/'. $lang;
|
| 208 |
$query = 'destination='. drupal_urlencode($destination);
|
| 209 |
break;
|
| 210 |
}
|
| 211 |
$links[$lang] = array('title' => $title, 'path' => $url, 'attribs' => $attribs, 'query' => $query, 'fragment' => NULL, 'absolute' => $absolute, 'html' => TRUE);
|
| 212 |
}
|
| 213 |
return $links;
|
| 214 |
}
|
| 215 |
|
| 216 |
/**
|
| 217 |
* Render the links for the language switcher block.
|
| 218 |
*
|
| 219 |
* This stub function is needed to allow other modules to alter the links
|
| 220 |
* in front of output.
|
| 221 |
*
|
| 222 |
* @param $languages
|
| 223 |
* An array of languages, keyed by language code, as returned by
|
| 224 |
* theme_translatable_languages().
|
| 225 |
*
|
| 226 |
* @see theme_translatable_languages()
|
| 227 |
*/
|
| 228 |
function theme_translatable_language_link($languages = array()) {
|
| 229 |
foreach ($languages as $lang => $link) {
|
| 230 |
$languages[$lang] = l($link['title'], $link['path'], $link['attribs'], $link['query'], $link['fragment'], $link['absolute'], $link['html']);
|
| 231 |
}
|
| 232 |
return $languages;
|
| 233 |
}
|
| 234 |
|
| 235 |
/**
|
| 236 |
* Switch the locale of an user, if enabled.
|
| 237 |
*
|
| 238 |
* @param $type
|
| 239 |
* Which locale to switch; 'locale' or 'adminlocale'.
|
| 240 |
* @param $locale
|
| 241 |
* A language to switch to; already validated.
|
| 242 |
*/
|
| 243 |
function translatable_user_locale($type, $locale) {
|
| 244 |
global $user;
|
| 245 |
|
| 246 |
// Only switch if the user does not prefer separate locales.
|
| 247 |
if (!$user->switch_adminlocale) {
|
| 248 |
$function = 'translatable_set_'. $type;
|
| 249 |
$function($locale, FALSE);
|
| 250 |
}
|
| 251 |
}
|
| 252 |
|
| 253 |
/**
|
| 254 |
* Implementation of hook_admin_menu().
|
| 255 |
*/
|
| 256 |
function translatable_admin_menu(&$admin_menu, $may_cache) {
|
| 257 |
if (!user_access('access translatable')) {
|
| 258 |
return;
|
| 259 |
}
|
| 260 |
if (!$may_cache) {
|
| 261 |
// Display current translation language first and prefix it with a
|
| 262 |
// meaningful title.
|
| 263 |
$languages = translatable_adminlocale_formselect(NULL, NULL);
|
| 264 |
$adminlocale = translatable_get_adminlocale();
|
| 265 |
$languages[$adminlocale]['title'] = t('Language') .': '. $languages[$adminlocale]['title'];
|
| 266 |
|
| 267 |
$mid_admin = $admin_menu['index']['admin'];
|
| 268 |
$mid_language = admin_menu_add_item($admin_menu, $mid_admin, array('title' => $languages[$adminlocale]['title'], 'description' => $languages[$adminlocale]['attribs']['title'], 'path' => $languages[$adminlocale]['path'], 'query' => $languages[$adminlocale]['query'], 'weight' => 10));
|
| 269 |
unset($languages[$adminlocale]);
|
| 270 |
|
| 271 |
foreach ($languages as $link) {
|
| 272 |
admin_menu_add_item($admin_menu, $mid_language, array('title' => $link['title'], 'description' => $link['attribs']['title'], 'path' => $link['path'], 'query' => $link['query'], 'attributes' => $link['attribs']));
|
| 273 |
}
|
| 274 |
}
|
| 275 |
}
|
| 276 |
|