Jak Na Language Switcher V Bootstrapu

This article shows you, how to create reasonably good-looking language switcher for Bootstrap-based Drupal site, which includes corresponding icons for easier navigation. It assumes, that you have enbled Locale module (which is in core), so you can enable default language switcher.

In block management interface, add language switcher to Navigation region.
In your subtheme folder/templates:

  1. Create region—navigation.tpl.php file and insert into it following:
<?php if ($content): ?>
    <?php print $content; ?>
<?php endif; ?>
  1. Create block—navigation.tpl.php file with content:
<?php print $content ?>

Insert following snippet to your theme's template.php file:

<?php
function <your_theme_name>_links__locale_block(&$variables) {
  // the global $language variable tells you what the current language is
  global $language;
  // an array of list items
  $items = array();
  foreach($variables['links'] as $lang => $info) {
      $code     = $info['language']->language;      
      $name     = "<span class='lang-sm' lang='" . $code . "'></span> " . $info['language']->native;
      $href     = isset($info['href']) ? $info['href'] : '';
      $li_classes   = array('lang_'.$code);
      // if the global language is that of this item's language, add the active class
      if($lang === $language->language){
            $li_classes[] = 'active';
      }
      $link_classes = array();
      $options = array('attributes' => array('class'    => $link_classes),
                                             'language' => $info['language'],
                                             'html'     => true
                                             );
      $link = l($name, $href, $options);
      // display only translated links
      if ($href) $items[] = array('data' => $link, 'class' => $li_classes);
    }
  // output
  $attributes = array('class' => array('nav','navbar-nav','navbar-right'));
  $output = theme_item_list(array('items' => $items,
                                  'title' => '',
                                  'type'  => 'ul',
                                  'attributes' => $attributes
                                  ));
  return $output;
}

Then install libraries api module and download repository https://github.com/usrz/bootstrap-languages to sites/all/libraries/bootstrap-languages

Then create file sites/all/libraries/bootstrap-languages.libraries.info with contents:

name = Bootstrap Languages
machine name = bootstrap-languages
description = Provides language icons for Bootstrap-based theme
version = 1
files[css][] = languages.min.css

Then add following snippet to template.php file:

<?php
/**
 * Implements hook_preprocess_html().
 */
function zastreseni_bazenu_bootstrap_preprocess_html(&$variables) {
  libraries_load('bootstrap-languages');    
}
Není-li uvedeno jinak, obsah této stránky je pod licencí Creative Commons Attribution-ShareAlike 3.0 License