Language Switcher

PerfLocale provides multiple ways to display a language switcher on your site.

Gutenberg Block

Add the Language Switcher block from the block inserter (search "Language Switcher" or find it under Widgets).

Block Settings

SettingOptionsDefault
StyleFlags + Names, Flags Only, Names Only, DropdownFlags + Names
LayoutHorizontal, VerticalHorizontal
Name FormatNative, English, Both - English (Native), CodeNative
Show flagsOn/OffOn
Show namesOn/OffOn
Hide currentOn/OffOff
Show untranslatedOn/OffOff
Font size10–24 px14
Flag size14–40 px20
Gap0–24 px8

The block uses server-side rendering, so you see a live preview in the editor.

Shortcode: [perflocale_switcher]

Place this shortcode in any post, page, or widget area.

Attributes

AttributeValuesDefaultDescription
styleflags_names, flags_only, names_only, dropdownFrom settingsDisplay style
layouthorizontal, verticalhorizontalList direction
name_formatnative, english, both, slugnativeHow to display language names
show_flags1 / 01Show flag emoji
show_names1 / 01Show language names
hide_current1 / 0From settingsHide the active language
show_untranslated1 / 0From settingsShow languages without a translation
classCSS class name(empty)Custom CSS class

Examples

[perflocale_switcher]

[perflocale_switcher style="dropdown"]

[perflocale_switcher style="flags_only" hide_current="1"]

[perflocale_switcher style="names_only" name_format="english"]

[perflocale_switcher layout="vertical" show_flags="0"]

[perflocale_switcher style="flags_names" name_format="both" class="my-switcher"]

Widget

Go to Appearance > Widgets and add the PerfLocale Language Switcher widget. Configure the title and display style.

PHP Template Tag

In your theme templates:

<?php perflocale_language_switcher(); ?>

<?php perflocale_language_switcher( [
	'template'    => 'flags_names',
	'show_flags'  => true,
	'show_names'  => true,
	'show_native' => true,
	'hide_current' => false,
	'class'       => 'my-custom-switcher',
] ); ?>

Theme Template Override

To customize the switcher HTML, copy any template from perflocale/templates/frontend/ to your theme's perflocale/ directory:

your-theme/perflocale/switcher-list.php
your-theme/perflocale/switcher-dropdown.php
your-theme/perflocale/switcher-flags.php

Available variables in templates

VariableTypeDescription
$languagesarrayActive language objects
$current_slugstringCurrent language slug
$urlsarray[slug => url] map of translation URLs
$argsarrayAll display arguments

Language object properties

PropertyExampleDescription
slugfr2-3 letter language code
localefr_FRFull WP locale
nameFrenchEnglish name
native_nameFrançaisNative name
flag🇫🇷Flag emoji
text_directionltrText direction (ltr or rtl)

Filters

// Filter which languages appear in the switcher.
add_filter( 'perflocale/switcher/languages', function( $languages ) {
	// Remove a specific language.
	return array_filter( $languages, fn( $lang ) => $lang->slug !== 'de' );
} );

// Override the template path.
add_filter( 'perflocale/switcher/template', function( $path, $args ) {
	return get_stylesheet_directory() . '/my-custom-switcher.php';
}, 10, 2 );

// Modify the final HTML.
add_filter( 'perflocale/switcher/output', function( $html, $args ) {
	return '<div class="wrapper">' . $html . '</div>';
}, 10, 2 );