Hi

In Language Switcher block I'd like to show iso code of language instead of native language name. For example

  • English
  • Polski

should become

  • en
  • pl

Is there any module available which does exactly what I need, or perhaps which hook should I use ?

In addition of core modules I have got also i18n package installed.

Thanks
Marcin

Comments

misqu23’s picture

Please help.

Does anybody knows how to achieve this.

Martin

misqu23’s picture

OK. I found this. Module Consistent Language Interface does the thing.

eugene_se’s picture

I solved it this way:

Go to Administer > Site configuration > Languages

Then edit each language and you can enter language codes in the first two fields instead of the descriptive names that are there by default.

So no extra modules are needed.

AlanAtLarge’s picture

This was driving me crazy too....theme overrides, etc....

I only needed to change the native language box....

ezoulou’s picture

I didn't find much about this so I post my solution in case it helps someone.

Situation
I want my language switcher block to display the iso code (a 2 letters string) in place of the full language title. For example :

I want
FR - EN - NL
In place of
Français - English - Nederlands

Solution
Place this in YOURTHEME/template.php :

function YOURTHEME_links__locale_block(&$vars) {
  foreach($vars['links'] as $language => $langInfo) {
		$vars['links'][$language]['title'] = $vars['links'][$language]['language']->language;
  }
  $content = theme_links($vars);
  return $content;
}
stolzenhain’s picture

Thanks for the legwork on this little snippet — I always went with the first solution though it doesn't always seem ideal (e.g. when offering the language selection on the node edit page).

It should be noted, that a common best-practice approach on rewriting strings (the String Override module) doesn't work for this usecase – there seems to be something hardcoded going on.

detectivepixel’s picture

Separate language codes with " | " and makes them uppercase

function YOURTHEME_links__locale_block(&$variables) {

  $count = count($variables['links']);
  $i = 0;
  foreach($variables['links'] as $language => $langInfo) {
        $i++;
        $variables['links'][$language]['title'] = strtoupper($variables['links'][$language]['language']->language);

        if($i != $count) {
          $variables['links'][$language]['title'] .= '<span class="language-switcher-separator"> | </span>';
        }

        $variables['links'][$language]['html'] = TRUE;
  }
  $content = theme_links($variables);
  return $content;

}
seeem’s picture

Thanks dude for this (Y)

andreasTypo’s picture

You can also just go to;

admin/config/regional/language

There you find active languages.

for your languages English and Polski just press edit and change 'the name of the language in your own language'-or something from English to EN and from Polski to PL
that's the fix without changing code

teefars’s picture

Hey thanks! It worked!
Any setbacks?

imen ch’s picture

to go to admin/config/regional/language and rename language doesn't work on drupal 8.3 version

i have do this and it works on mytheme.module (mytheme: name of your theme)

mytheme_links__language_block(&$variables) {
$variables['links']['en']['link']['#title'] = 'EN';
$variables['links']['fr']['link']['#title'] = 'FR';
}

gagarine’s picture

A generic solution for D8. Add this code in mytheme.theme.

/**
 * Use language code for the language switcher
 *
 * @param $variables
 */
function MYTHEME_preprocess_links__language_block(&$variables) {

  foreach ($variables['links'] as $i => $link) {
    /** @var \Drupal\language\Entity\ConfigurableLanguage $linkLanguage */
    $linkLanguage = $link['link']['#options']['language'];
    $variables['links'][$i]['link']['#title'] = $linkLanguage->get('id');
  }
}

https://interface-network.com - Interface Network is an action and research technology governance agency.

stebentje’s picture

With this I get: Error: Call to a member function get() on null in MYTHEME_preprocess_links__language_block() 

Some idea about this error?

quotientix’s picture

You will need to rename "MYTHEME" to your theme name