Language switcher in my header?
Eviltomh89 - May 6, 2009 - 11:24
HI all,
I was wondering whether it is possible to place a language switcher in the header of my website.
Already i have found the page.tpl.php file and tried to change it, but simply putting the code of the language module i have installed in the page.tpl.php isn't working
here is the code i made up
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="<?
php echo get_page_language($language); ?>"
xml:lang="<?php echo get_page_language($language);
?>">
<head>
<title><?php if (isset($head_title )) { echo $head_title;
} ?></title>
<meta http-equiv="X-UA-Compatible"
content="IE=EmulateIE7" />
<?php echo $head; ?>
<?php echo $styles ?>
<?php echo $scripts ?>
<!--[if IE 6]><link rel="stylesheet" href="<?php echo
$base_path . $directory; ?>/style.ie6.css" type="text/css"
/><![endif]-->
<!--[if IE 7]><link rel="stylesheet" href="<?php echo
$base_path . $directory; ?>/style.ie7.css" type="text/css"
media="screen" /><![endif]-->
<script type="text/javascript"><?php /* Needed to
avoid Flash of Unstyle Content in IE */ ?> </script>
</head>
<body>
<div class="PageBackgroundSimpleGradient">
</div>
<div class="Main">
<div class="Sheet">
<div class="Sheet-tl"></div>
<div class="Sheet-tr"></div>
<div class="Sheet-bl"></div>
<div class="Sheet-br"></div>
<div class="Sheet-tc"></div>
<div class="Sheet-bc"></div>
<div class="Sheet-cl"></div>
<div class="Sheet-cr"></div>
<div class="Sheet-cc"></div>
<div class="Sheet-body">
<div class="Header">
<div class="Header-jpeg"></div>
<div class="logo">
<?php
if (!empty($site_name)) { echo '<h1 class="logo-
name"><a href="'.check_url($base_path).'" title = "'.
$site_name.'">'.$site_name.'</a></h1>'; }
if (!empty($site_slogan)) { echo '<div class="logo-
text">'.$site_slogan.'</div>'; }
if (!empty($logo)) { echo '<div class="logo-
image"><img src="'. check_url($logo) .'" alt="'. $site_title
.'" id="logo-image" /></div>'; }
?>
</div>
// Here is where i have copied the whole languageinterface module
<?php
// $Id: languageinterface.module,v 1.14 2009/02/19
21:55:36 aaronhawkins Exp $
/**
* @file
* Creates a language switcher for Drupal 6
*/
require_once('li_helperfunctions.inc');
function languageinterface_init() {
if (variable_get('language_redirect', 0) == 1) {
$referrer = $_SERVER["HTTP_REFERER"];
$lastlanguage = '';
global $language;
global $base_url;
global $user;
$fullurl = $_SERVER['QUERY_STRING'];
$fullurl = str_replace('q=', '', $fullurl);
if (!empty($_SESSION['last_viewed_source'])) {
$lastviewed = $_SESSION['last_viewed_source'];
}
else {
$lastviewed = $_COOKIE['last_viewed_source'];
}
$internal = strpos($referrer, $base_url);
if ($lastviewed == 'goto') {
$internal = 20;
}
if(!is_numeric($internal)) {
// not from internal link
$arg_1 = arg(1);
if (arg(0) == 'user' && empty($arg_1)) {
$lastlanguage = $_COOKIE['last_viewed_lang'];
}
else {
$lastlanguage = $_SESSION['lastlanguage'];
}
if (!empty($lastlanguage) && $lastviewed != 'goto') {
$languagepath = languagepath($lastlanguage);
$_SESSION['last_viewed_source'] = array();
$_SESSION['last_viewed_source'] = 'goto';
setcookie("last_viewed_source", 'goto', time
()+360000, '/');
drupal_goto($languagepath);
}
}
if (is_numeric($internal)) {
}
if (is_numeric($internal)) {
$_SESSION['lastlanguage'] = array();
$_SESSION['lastlanguage'] = $language->language;
$_SESSION['last_viewed_source'] = array();
$_SESSION['last_viewed_source'] = 'int';
setcookie("last_viewed_lang", $language->language,
time()+360000, '/');
setcookie("last_viewed_source", 'int', time()+360000,
'/');
}
}
}
/**
* Implementation of hook_menu().
*/
function languageinterface_menu() {
$items['admin/settings/language-interface'] = array(
'title' => t('Consistent Language Interface Options'),
'description' => t('Language Interface Options'),
'page callback' => 'drupal_get_form',
'page arguments' => array
('languageinterface_options'),
'access arguments' => array('administer languages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of hook_block().
*/
function languageinterface_block($op = 'list', $delta = 0,
$edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Consistent Language Interface
block');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;
case 'view':
$block['subject'] = t('');
$block['content'] = language_link_creation();
}
//end switch ($op)
return $block;
}
//End function (hook_block)
function languagepath($lang) {
//We get the full url of the page.
global $base_url;
$fullurl = $_SERVER['QUERY_STRING'];
$fullurl = str_replace('q=', '', $fullurl);
// We convert the url into an array.
$urlarray = explode('/', $fullurl);
// We check to see if there is a language string in the
url and remove it if so.
global $language;
if ($urlarray[0] == "") {
array_shift($urlarray);
}
if ($urlarray[0] == $language->language) {
array_shift($urlarray);
}
$prefix = $lang.'/';
$default_language = language_default();
if ($lang == $default_language->language) {
$prefix = '';
}
// We determine if we are looking at a node or some
other kind of page.
if ((arg(0) == 'node') && (is_numeric(arg(1)))) {
$areatype = 'node';
$node_nid = arg(1);
array_shift($urlarray);
array_shift($urlarray);
/*
Since the page is a node we remove two more items
from the array
so that the "node/" and the nid are left out.
*/
// Since we have determined this to be a node we get
the tnid to work with later.
$tnid = db_result(db_query("SELECT tnid FROM
{node} WHERE nid = %d", $node_nid));
if ($tnid) {
if (specific_translation_exist($tnid, $lang)) {
$destination_nid = specific_translation_exist($tnid,
$lang);
}
else {$destination_nid = $tnid;}
}
// End if ($tnid).
else {
$destination_nid = $node_nid;
}
if (arg(2)) {
$destination_nid = arg(1);
$remaining = '/'.implode("/",$urlarray);
}
$path = rootpath() .$prefix .'node/'. $destination_nid .
$remaining;
// Endif (current page is node)
}
else {
$remaining = implode("/", $urlarray);
$path = rootpath() . $prefix . $remaining ;
}
return $path;
}
function language_link_creation() {
$customdimensions = variable_get
('custom_flag_dimensions', '16x12');
$customdimensions = explode('x', $customdimensions);
$flagwidth = $customdimensions[0];
$flagheight = $customdimensions[1];
$defaultflagpath = drupal_get_path('module',
'languageinterface') .'/flags/*.png' ;
$flagpath = variable_get('custom_flag_path',
$defaultflagpath);
$textoptions = variable_get('languageinterface_text',
'post');
$languages = get_enabled_languages();
$languagetext = variable_get('tl', '');
$tbl = variable_get('tbl', '');
$tal = variable_get('tal', '');
foreach ($languages as $lang) {
$path = languagepath($lang);
if ($lang == currentlanguage()) {
$activeclass = TRUE;
}
else {
$activeclass = FALSE;
}
$src = rootpath() . str_replace('*', $lang, $flagpath);
$text = '';
$defaulttext = db_result(db_query('SELECT native
FROM {languages} WHERE language = "%s"', $lang));
if (!empty($languagetext[$lang])) {
$ltext = $languagetext[$lang];
}
else {
$ltext = $defaulttext;
}
if (!empty($tbl[$lang]) && empty($tal[$lang])) {
$text = $tbl[$lang] .' '. $ltext;
}
if (!empty($tal[$lang]) && empty($tbl[$lang])) {
$text = $ltext .' '. $tal[$lang];
}
if (!empty($tal[$lang]) && !empty($tbl[$lang])) {
$text = $tbl[$lang] .' '. $ltext .' '. $tal[$lang];
}
if (empty($tal[$lang]) && empty($tbl[$lang])) {
$text = $ltext;
}
$links[$lang]['text'] = $text;
$links[$lang]['src'] = $src;
$links[$lang]['flagheight'] = $flagheight;
$links[$lang]['flagwidth'] = $flagwidth;
$links[$lang]['position'] = $textoptions;
$links[$lang]['path'] = $path;
$links[$lang]['active'] = $activeclass;
$links[$lang]['language'] = $lang;
}
//End foreach.
return theme('languageswitcher', $links);
}
/**
* Implementation of hook_theme().
*/
function languageinterface_theme() {
return array(
'languageswitcher' => array(
'arguments' => array('links' => NULL ),
),
);
}
/**
* Create a themable language switcher
*/
function theme_languageswitcher($links) {
/*
Here we are manually creating a unordered list due to
the fact that
theme('item_list') does not assign language specific
active classes correctly
*/
$output = '<ul id="language-interface">';
foreach ($links as $link) {
$classes = '';
if ($link['active'] == TRUE) {
$classes = 'active'.' '. $link['language'] ;
}
else {
$classes = $link['language'] ;
}
if ($link['position'] != 'no_image') {
$flag = '<img src="'. $link['src'] .'" width = "'. $link
['flagwidth'] .'" height = "'. $link['flagheight'] .'" class="'.
$classes .'" alt="'. $link['text'] .'"/>';
}
if ($link['position'] != 'no_text') {
$text = $link['text'];
}
switch ($link['position']) {
case 'pre':
$display = $text.' '.$flag;
break;
case 'post';
$display = $flag.' '.$text;
break;
case 'no_text';
$display = $flag;
break;
case 'no_image';
$display = $text;
break;
}
// end switch
// Here we are creating a link manually due to the fact
that the l() function is language unaware.
// When the l() issue is fixed in core this line will be
changed to l($display, $link['path'])
//In the meantime to secure against xss attacks we
run the links through filter_xss().
$prefiltered = sprintf('<li class="%s"><a href="%s"
class = "%s">%s</a></li>', $classes, $link['path'],
$classes, $display );
$output .= filter_xss($prefiltered, array('li', 'a', 'img'));
}
$output .= '</ul>';
return $output;
}
function languageinterface_options() {
$customdimensions = variable_get
('custom_flag_dimensions', '16x12');
$customdimensions = explode('x', $customdimensions);
$flagwidth = $customdimensions[0];
$flagheight = $customdimensions[1];
$langsettings = variable_get('language_negotiation', '0');
if ($langsettings == '0' || $langsettings == '3') {
drupal_set_message(t('Language Negotiation must be
set to "Path Prefix" or "Path Prefix with language fallback"
for the language swithcher to work. <br /> To change
this setting please go to: ').l
('admin/settings/language/configure',
'admin/settings/language/configure'), $type = 'error',
$repeat = FALSE);
}
$form['flag_text_config'] = array(
'#type' => 'radios',
'#title' => t('Text Options'),
'#options' => array('pre' => t('Text Before Flag'), 'post'
=> t('Text After Flag'), 'no_text' => t('Flag only (No
text)'), 'no_image' => t('Text only (No flag)')),
'#default_value' => variable_get
('languageinterface_text', 'post'),
'#description' => t('Choose how you want the
language text and flags to display.'),
);
$form['language_order'] = array(
'#type' => 'fieldset',
'#title' => t('Language weights'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#description' => t('Choose the order in which the
language switching links will appear.'),
);
$form['language_text'] = array(
'#type' => 'fieldset',
'#title' => t('Pre and post language text'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#description' => t('Optional text before or after
language name.'),
);
$enabled = get_enabled_languages();
$form['language_order']['lang_weight'] = array('#tree'
=> TRUE);
$form['language_text']['pretext'] = array('#tree' =>
TRUE);
$form['language_text']['posttext'] = array('#tree' =>
TRUE);
$form['language_text']['text'] = array('#tree' => TRUE);
$tbl = variable_get('tbl', '');
$tl = variable_get('tl', '');
foreach ($enabled as $lang) {
$text = db_result(db_query('SELECT native FROM
{languages} WHERE language = "%s"', $lang));
if (!empty($tl[$lang])) {
$tldef = $tl[$lang];
}
else {
$tldef = $text;
}
$currentweight = db_result(db_query('SELECT weight
FROM {languages} WHERE language = "%s"', $lang));
$form['language_order']['lang_weight'][$lang] = array(
'#type' => 'weight',
'#title' => $text,
'#delta' => 10,
'#default_value' => $currentweight,
);
$form['language_text']['text'][$lang] = array(
'#type' => 'textfield',
'#title' => t('Language display for') .' '. $text,
'#default_value' => $tldef,
'#size' => 80,
'#description' => t('How the language will display in
the link. Useful in situations where the language name
should change in the context of the text before it or after
it. Example: "Version Française" where the "e" is
needed because "version" is feminine.'),
);
$form['language_text']['pretext'][$lang] = array(
'#type' => 'textfield',
'#title' => t('Text before language for') .' '. $text,
'#default_value' => $tbl[$lang],
'#size' => 80,
'#description' => t('Text before language. Example:
to show "View page in English" Rather than just "English"
enter "View page in".'),
);
$form['language_text']['posttext'][$lang] = array(
'#type' => 'textfield',
'#title' => t('Text after language for') .' '. $text,
'#default_value' => $tal[$lang],
'#size' => 80,
'#description' => t('Text after language. Example: to
show "English version" Rather than just "English" enter
"version"'),
);
}
// end foreach
$form['custom_flag_path'] = array(
'#type' => 'textfield',
'#title' => t('Custom path to flag icons'),
'#default_value' => variable_get('custom_flag_path',
drupal_get_path('module', 'languageicons').'/flags/*.png'),
'#size' => 80,
'#maxlength' => 180,
'#description' => t('Path for language flags, relative to
Drupal installation. \'*\' is a placeholder for language
code. If you wish to change the image type for the flag
be sure to replace the png with the correct file type.'),
);
$form['custom_dimensions'] = array(
'#type' => 'fieldset',
'#title' => t('Custom Flag Dimensions'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#description' => t('Choose the width and height of
the flag icons.'),
);
$form['custom_dimensions']['flagwidth'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#tree' => TRUE,
'#default_value' => $flagwidth,
);
$form['custom_dimensions']['flagheight'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#tree' => TRUE,
'#default_value' => $flagheight,
);
$form['redirect'] = array(
'#type' => 'checkbox',
'#title' => t('Redirect returning visitors to last viewed
language'),
'#default_value' => variable_get('language_redirect',
0),
);
$form['reset_all'] = array(
'#type' => 'checkbox',
'#title' => t('Reset configuration'),
'#default_value' => 0,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Settings'),
);
return $form;
}
function languageinterface_options_submit($form, &
$form_state) {
if ($form_state['values']['reset_all'] == 0) {
variable_set('language_redirect', $form_state['values']
['redirect']);
$enabledlanguages = get_enabled_languages();
foreach($enabledlanguages as $lang) {
$pretext[$lang] = $form_state['values']
['language_text']['pretext'][$lang];
$text[$lang] = $form_state['values']['language_text']
['text'][$lang];
$posttext[$lang] = $form_state['values']
['language_text']['posttext'][$lang];
$weight = $form_state['values']['language_order']
['lang_weight'][$lang];
db_query('UPDATE {languages} SET weight = %d
WHERE language = "%s"', $weight, $lang);
}
if (!empty($pretext)) {
variable_set('tbl', $pretext);
}
else {
variable_del('tbl');
}
if (!empty($text)) {
variable_set('tl', $text);
}
else {
variable_del('tl');
}
if (!empty($posttext)) {
variable_set('tal', $posttext);
}
else {
variable_del('tal');
}
if (!empty($form_state['values']['custom_dimensions']
['flagwidth']) && !empty($form_state['values']
['custom_dimensions']['flagheight'])) {
$dimensions = check_plain($form_state['values']
['custom_dimensions']['flagwidth']) .'x'. check_plain
($form_state['values']['custom_dimensions']['flagheight']) ;
variable_set('custom_flag_dimensions', $dimensions);
}
variable_set('languageinterface_text',$form_state
['values']['flag_text_config']);
variable_set('custom_flag_path', check_plain
($form_state['values']['custom_flag_path']));
}
if ($form_state['values']['reset_all'] == 1) {
variable_set('languageinterface_text', 'post');
variable_set('custom_flag_path', drupal_get_path
('module', 'languageinterface') .'/flags/*.png');
variable_set('custom_flag_dimensions', '16x12');
variable_del('tbl');
variable_del('tal');
variable_del('tl');
}
drupal_set_message(t('Your configuration settings have
been saved'), $type = 'status', $repeat = FALSE);
}
// here is the end of the language interface module, and after this is the rest of the page.tpl.php file
</div>Can anyone tell me what to do?
Help is much appreciated!!
Thnx in advance
Tom

create a region in header and
create a region in header and place that languages block in that region
add a region in your theme .info file
add css in your theme css file
and print that region in page.tpl.php
thats it
http://drupal.org/node/171224
velmula
Hi manasapenchala, thanks for
Hi manasapenchala,
thanks for your explanation,
But the first thing you said, doesn't make any sense to me :(
How do i place the language block in the header region?
And how do i add the header as a block, so that i can place also other stuff in the header.
Regards,
Tom