📜 ⬆️ ⬇️

Drupal and multilingual, how to programmatically add a language switch

When creating the next multilingual site, a task arose, without using the standard language switching block, to add a language switch.

At first glance, nothing complicated.
But after making several attempts, I was faced with the task of supporting pages created through views, selections of announcements on the pages of taxonomy terms and translations associated with the nodes by the Content Translation module. It also turned out to be useful to reflect on the fact that the above pages (node) and taxonomy terms have aliases.

To my surprise, Google did not produce the expected results, and I absolutely do not perceive the tips for sawing the core of drupal (for this there is an API and a little imagination).
')
Having smoked a couple of cups of coffee and having drunk more than one cigarette (humor), he solved the problem and is ready to share the solution with those who still have time to step on such a rake in their life.


So go:

To implement we need two files of our theme ( page.tpl.php and template.php ).

In the file template.php we create a function:

 function __lang_front_lang($foo){ switch($foo) { case 'uk': $output = '/ua'; break; case 'ru': $output = '/'; break; case 'en': $output = '/en'; break; default: $output = '/'; break; } return $output; } function __link_lang($foo, $path, $path_alt, $nid){ if(strlen($path)){ $path = 'node/'.$nid; $path_lang = translation_path_get_translations($path); $path = array( 'ru' => drupal_get_path_alias($path_lang[ru], $path_language = 'ru'), 'uk' => drupal_get_path_alias($path_lang[uk], $path_language = 'uk'), 'en' => drupal_get_path_alias($path_lang[en], $path_language = 'en'), ); } elseif(strlen($path_alt[2])) { $path_tax = $path_alt[0].'/'.$path_alt[1].'/'.$path_alt[2]; }elseif(strlen($path_alt[1])) { $path_tax = $path_alt[0].'/'.$path_alt[1]; } elseif(strlen($path_alt[0])) { $path_tax = $path_alt[0]; } else { $path_tax = ''; } if(!strlen($path) || !strlen($nid)){ } else { if(!strlen($path[uk])){ $path[uk] = 'not_translated_uk.html'; } if(!strlen($path[ru])){ $path[ru] = 'not_translated_ru.html'; } if(!strlen($path[en])){ $path[en] = 'not_translated_en.html'; } } $output = '<li><a href="/ua/'.$path[uk].$path_tax.'">ua</a></li> <li><a href="/'.$path[ru].$path_tax.'">ru</a></li> <li><a href="/en/'.$path[en].$path_tax.'">eng</a></li>'; return $output; } 


Further, in the page.tpl.php file in the place where we need to display our switch, we add:

 <ul class=" img_link_list lang_li"> <?php if($is_front){ $path_alt = ''; } else { $path_alt = array(arg(0), arg(1), arg(2)); } print __link_lang($language->language, $node->path, $path_alt, $node->nid); ?> </ul> 


and in the place where you need to display the logo with a link to the main add:
 <div id="logo"><a href="<?php print __lang_front_lang($language->language); ?>"><img src="<?php echo $base_path . $directory; ?>/images/logo.jpg" width="140" /></a></div> 


The main magic ends here.
Paths will be formed taking into account the pages on which we are located regardless of whether it is a node or a taxonomy term page.

It remains the least to make the taxonomy pages filter the material, and give us announcements that have the language in which we are switched.

There are several ways to do this. I will describe one of the most simple ones (in order to be able to implement this solution to the uninitiated).

Go:
We will use the “Views” module and one file of our theme ( node.tpl.php ).
Go to the “Views” view list page (/ admin / build / views) and enable the view included in the kernel, if not already included, with the name “taxonomy_term (default)”.
Let us ensure that the output of the material in the form of “page (page)” is configured to display material announcements, and not fields.

Then we go to our node.tpl.php file and rule what is between
 <?php if (!$page): ?> 

and
 <?php endif; ?> 


As a result, we should get something like this:
 <div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?> clear-block"> <?php print $picture ?> <?php if (!$page): ?> <?php global $language; ?> <?php if ($language->language == $node->language): ?> <div class="items_page"> <h3><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h3> <?php print $content; ?> </div> <?php endif; ?> <?php endif; ?> <?php if ($page): ?> <div class="meta"> <?php if ($submitted): ?> <span class="submitted"><?php print $submitted ?></span> <?php endif; ?> </div> <div class="content"> <?php print $content ?> </div> <?php print $links; ?> <?php endif; ?> </div> 


At this our magic ends with success.
If we did everything right, then we just enjoy the result and continue to drink coffee.

with their heads held high, as many developers regularly encounter this

multilingualism in Drupal.

Oops:
Do not forget, after any changes in the theme files, resave the list of themes (/ admin / build / themes), themes

by updating the cache in the first place, and of course just cleaning the shared cache (/ admin / settings / performance).

You could! So you are well done!

Source: https://habr.com/ru/post/140758/


All Articles