There is a snippet that can be used for language switching instead of the standard language switching block (see an example of page template code appended) - the point is (1) being more compact (e.g. you can insert the switcher into the page header next to the site name, slogan, or search box), and (2) hiding the current language (see http://drupal.org/node/226959).
It works fine for Drupal 5 + i18n - however, it does not work for Drupal 6 + i18n because of i18n v.6 functions being different. Does anybody know how to change this snippet to make it work for D6+i18n?
Thank you!
<div id="langswitch" style="float: right">
-> |<?php foreach (excl_translation_get_links($_GET['q']) as $link): ?>
<?php print $link?> |
<?php endforeach; ?>
<?php
// modified function
function excl_translation_get_links($path = '', $query = NULL, $names = NULL) {
$current = i18n_get_lang();
$names = $names ? $names : i18n_languages('native');
foreach (array_keys(i18n_supported_languages()) as $lang){
if ($lang != $current) {
$url = translation_url($path, $lang);
$links[]= theme('i18n_link', $names[$lang], i18n_path($url, $lang) , $lang, $query);
}}
return $links;
}
?>
</div><!-- /end langswitch -->
Comments
Comment #1
Anonymous (not verified) commentedWould be interested in that, too. I tried to dig through the module, but it seems the according stuff has changed completely so I failed :-(
Comment #2
Anonymous (not verified) commentedOk, I got it working. The following is a mix of code snippets from locale.module and i18n languageicons.module. Someone with better php and i18n knowledge should review that to see if I'm doing nasty things here.
There is one problem with the drupal_alter call, see comments in the code, but I could work around this.
I hope this helps!
Comment #3
GN commentedThanks a lot Frank - it seems to work!
(If flag icons are disabled, the
languageicons_link_add($links[$language->language]);line should be removed as the function is not defined).
Comment #4
Anonymous (not verified) commentedAh, ok, that should be possible to check with
if (function_exists('languageicons_link_add'))...
Comment #5
GN commentedHmmmm...
I'm afraid it actually does not do what the regular i18n language switcher block does.
The i18n language switching block switches the site from /lang1/node/n1 to /lang2/node/n2, where node/n2 is the translation of node/n1 from lang1 to lang2 - while this snippet just switches it from /lang1/node/n1 to /lang2/node/n1.
It's OK for the front page, but it does not work well for content nodes.
Comment #6
Anonymous (not verified) commentedYou are right, I was missing a piece. Here's a new version that should run fine. I just goes through the hole link_alter chain step by step.
Comment #7
Anonymous (not verified) commentedFor those who are interested: At http://drupal.org/node/250124 you can find a little extension that tries to solve the problem with klicking the language switcher on a non-language-neutral page that has no translation.
Comment #8
BenK2 commentedFrank,
I have seen your many posts on Internationaisation. I am a Newbie struggling with a presentation problem - how to turn the Language Switcher Block into a drop down list which, preferably, can be added to a menu or , remain as a block. I'm fairly sure your code snippets here provide the answer ! .. but although although I've figured out the basis of theme CSS in v6 and how to make changes, I cannot see what to do with / or where to put the code snippet ! I have been searching and searching .... could I trouble you to provide a quick explanation please...
Thanks
(by way of background I am running the latest release, have chosen 'path prefix with language fallback' for negotiation, will not be using language icons, expect to have 3 languages to begin and more later ..)
15/05
Just to add my thanks, to GN as well, for the posts below in response to my query.
Comment #9
Anonymous (not verified) commentedJust put it in a page or block body. On my site I've created a new block with input format "php". In the body of the block just insert the snippet and save the block. Then make sure that on the block configuration page you put the block somewhere (in the top or the right column) and there you go. No need to work with css or theming etc.
Comment #10
GN commentedThanks a lot Frank, this version seems to work fine indeed!
Comment #11
GN commentedBenK2,
Usually I try to make the language switcher as compact as possible. Instead of creating a separate block for it in a sidebar area, I'm placing it at the same level as site name and slogan (if any) - e.g. right-justified under the search box - by inserting it in the page.tpl.php after the search box:
<div id="langswitch" style="float: right">(Frank's snippet code goes here)</div>Then, I add to the theme's style.css file a "langswitch" section and format it as I like - usually I start from copying there the format settings from the CSS section describing page elements looking more or less like I want the language switcher to look.
So, if you have in your theme a css-based drop-down menu, you can copy its formatting to the "langswitch" css section.
(Also, you can see some examples of css for drop-down menus e.g. on http://www.cssplay.co.uk/menus/).
Comment #12
Anonymous (not verified) commentedComment #13
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #14
userid commentedJust the ticket, thanks very much. May I point out that the php filter module must be enabled so as to permit the php input format for blocks.
Hm, for some reason it still shows the language name after language is switched?
Comment #15
fletchgqc commentedSee also http://drupal.org/node/261059#comment-965314
Comment #16
jose reyero commentedAdded to the handbook, http://drupal.org/node/313813
Comment #17
RussianManFromMoscow commentedHow do I get in the choice of language through block "switch language" URL type "http://example.com/ru/node/" looks like "http://example.com/ru/"?
Comment #18
aaronwang commentedthanks!you did it
Comment #19
jose reyero commentedThat's a different question.
Comment #20
ThePrince commentedThe only issue I have with this is that when on the front page, it links to the url of the page I'm using for the front.
For example, I created a page called "home" and that's the one I put into the /admin/settings/site-information "Default front page".
The problem is that when I'm at the roo of my site i.e. example.com or example.com/fr the switcher links to example.com/home and example.com/fr/home and not just example.com and example.com/fr as it is when I click on the logo for example.
Can anyone help?
Thanks!
Comment #21
ThePrince commentedResolved my problem with global redirect module.
Comment #22
momper commentedthe same situation like theprince: is it possible to solve this without "global redirect"?
thanks and greetings momper
Comment #23
prosiktuno commentedI have a same task that BenK2.
I need to make language switcher as drop-down menu.
I totally new in site construction. I'm spent more one week to learn HTML, CSS, and now I start to learn PHP. But I still don't know how to get it.
I tried Frank code with CSS modification as told GN. No good success.
I tried code from here - http://drupal.org/node/261059#comment-1032902 and still don't have what I want.
Could anybody advice me how to get language switcher as drop-down menu on D6?
Thanks.
Comment #24
rout commentedI would also find it very useful if this were made into a drop-down menu.
Can anyone advise how this could be made into a dropdown menu?
Comment #25
mafioso commentedbased on http://drupal.org/node/261059#comment-1032902 snippet, i made some changes, and put following code into page.tpl.php:
rgds,
Comment #26
Bilmar commentedsubscribing
Comment #27
mafioso commentedSo sorry folks, my snippets from #25 didn't handle non-node pages (e.g. views page) well.
With my limited php knowledge, i modified the code as below:
rgds,
Comment #28
Josie_h commentedI'll start by apologising, but could someone please help.
I have chased this issue from thread to thread for many hours. I have also looked in all my CSS files to try to figure it myself, but I'm totally lost and due to my own admitted inabilities I'm completely stumped on something I assume is simple. Any help VERY much appreciated.
I have created a block and the snippet is working for me, now all I need to do is change the flag icons from vertical to horizontal alignment and get rid of the bullets. I think that I am supposed to put something (is it a 'class'?) between the middle brackets after the 'array' in the last line... 'echo theme('links', $links, array(?));'
If it isn't too much trouble or a silly question, could someone please explain (simply) where/what I need to change or add to my existing CSS file(s), or do I need to create a new CSS file and in either case what is the code I need to add/modify/create and then what I would actually put between the array brackets?
If relevant, I'm using the latest D6 with the Simply Modern theme and I'm putting the flag icons in either the header or a sidebar.
Many thanks for any help.
Comment #29
Josie_h commentedVery many thanks and credit to forum member Bruce Pearson who resolved this problem for me.
Comment #30
rburgundy commented@Josie_h - can you please share what you had to do to resolve the problem? Many thanks
Comment #31
brucepearson commentedIf you want the flag icons to be shown horizontally then change:
echo theme('links', $links, array());
to:
echo theme('links', $links, array('class' => 'links inline'));
Comment #32
jaochoo commentedHow can I output standard links like "Your Settings" or "Logout" correctly in my theme? E.g.:
- When on English language: Output: "Your Settings", Links to: /user/id/edit
- When on German language: Output: "Deine Einstellungen", Links to: /de/user/id/edit
My current code is as follows, but a) it does not translate "Your Settings" into the correct language (same for "Logout" and other standard links) and b) it always redirects to /user/id/edit, instead of /language-code/user/id/edit:
Comment #33
adrianmak commentedwhere should I put the code posted on #6 ?
Directly on page.tpl.php ? or paste in a new block and assign to a region. ?
Comment #34
jose reyero commentedAn alternate language switcher block would be welcomed for this module.
Comment #35
anton99 commentedHello! I bought theme and can not solve the problem simply CSS rule display: none for a.active, although Garland theme that works. Been looking on the internet solution to this problem and as a result I found this snippet (# 6). Good for me, that does not show link (box) for the current language, but not when switching to the current language, it would be better to show a link to the language before, and, accordingly, when it is clicked a link was inversely proportional to the current language. I'm not good at PHP, but a little versed in it and looking at the code seen that somewhere near the truth, but could not find her. If anyone knows how to change this snippet (# 6) I would be very grateful if he would rather brash to share ideas. Always waiting for an answer, because It is very important to get an answer as soon as possible. Thank you very much.