3

How can I use a custom logo image in Drupal 8 that is language dependent? There does not seem to be an appropriate setting in the Content and Language Translation configuration page.

5

Langauge is a visibility condition for blocks, so you could use multiple branding blocks and hard code the paths.

Setting it automatically is a little harder, language is not available in $variables in preprocess so you'd need to add it - you could add these to all preprocess functions and templates:

/**
 * Preprocess variables for all templates.
 * @param $variables
 */
function HOOK_preprocess(&$variables) {
  // Inject the current language into every template to ease theme development.
  $variables['current_language'] = \Drupal::languageManager()->getCurrentLanguage();

  // Inject a usable directory path.
  $variables['theme_path'] = base_path() . $variables['directory'];
}

You can use these in the branding block or page template to set a path to a language specific logo, e.g.:

<img src="{{ theme_path ~ '/logo-' ~ current_language.getID() ~ '.svg' }}" alt="{{ 'Home'|t }}" />

So you'd have logo-en.svg for english, logo-sv.svg for Swedish etc in your theme root, getID() just returns the two character language code.

You could go further and just build the whole thing in preprocess, just for the template you are using (either page or the branding block).

1

I like Jeff Burnz idea. If you want to have a more "robust" solution, you could make your own custom block type (hardcode) and put the logic there, so it could have also a fallback, if a new language logo is not available for some reason.

My second reason is: I don't like to put ANY kind of logic into presentation layer.

  • This is the solution I ended up implementing. Simple and flexible enough. Thanks to all for your suggestions. – Tony Patrinos Dec 29 '16 at 11:50
1

Found an easier hack for the lazier of us that doesn't involve any programming at all - just add the following to your block--system-branding-block.html.twig:

<img src= "{{ path('<front>') ~ 'logo.png' }}" alt="{{ 'Home'|t }}" />

This would look for logo.png for your site default language (usually English), rulogo.png for Russian etc.

0
function MYTHEME_preprocess_block(&$variables) {
  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $variables['language'] = $language;
  switch ($variables['base_plugin_id']) {

  case 'system_branding_block':
    if ($variables['content']['site_logo']['#access'] && $variables['content']['site_logo']['#uri'] && $variables['language']=='fr') {
      $variables['site_logo'] = str_replace('.svg', '-fr.svg', $variables['content']['site_logo']['#uri']);
    }
    break;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.