Helper API
PerfLocale provides a fluent helper API via the perflocale() function. This is the recommended way for theme and plugin developers to interact with PerfLocale.
Quick Start
// Get the helper.
$pl = perflocale();
// Current language info.
echo $pl->slug(); // "fr"
echo $pl->locale(); // "fr_FR"
echo $pl->name(); // "French"
echo $pl->native_name(); // "Français"
echo $pl->display_name(); // "French (Français)"
echo $pl->flag(); // "🇫🇷"
Method Reference
Language Information
perflocale()->slug(): string
Returns the current language slug (e.g. "fr"). Empty string if no language detected.
perflocale()->locale(): string
Returns the full locale (e.g. "fr_FR"). Falls back to get_locale().
perflocale()->name(): string
Returns the English name of the current language (e.g. "French").
perflocale()->native_name(): string
Returns the native name (e.g. "Français"). Falls back to the English name.
perflocale()->display_name(): string
Returns a combined display string: "French (Français)". If English and native names are the same, returns just the name.
perflocale()->flag(): string
Returns the flag emoji or code (e.g. "🇫🇷").
perflocale()->current_language(): ?object
Returns the full language object with all properties, or null if not detected.
Language object properties:
| Property | Type | Example |
|---|---|---|
slug | string | "fr" |
locale | string | "fr_FR" |
name | string | "French" |
native_name | string | "Français" |
flag | string | "🇫🇷" |
text_direction | string | "ltr" |
is_default | int | 0 |
is_active | int | 1 |
Language Checks
perflocale()->is_default(): bool
Returns true if the current language is the default language.
if ( ! perflocale()->is_default() ) {
// Show "back to English" link.
}
perflocale()->is_rtl(): bool
Returns true if the current language is right-to-left (Arabic, Hebrew, etc.).
$dir = perflocale()->is_rtl() ? 'rtl' : 'ltr';
echo '<div dir="' . $dir . '">';
perflocale()->is_language( string $slug ): bool
Check if the current language matches a specific slug.
if ( perflocale()->is_language( 'fr' ) ) {
echo 'Bienvenue!';
} elseif ( perflocale()->is_language( 'de' ) ) {
echo 'Willkommen!';
}
Language Data
perflocale()->default_language(): ?object
Returns the default language object.
$default = perflocale()->default_language();
echo $default->name; // "English"
perflocale()->languages(): array
Returns all active languages as an array of objects.
foreach ( perflocale()->languages() as $lang ) {
echo $lang->slug . ': ' . $lang->native_name . "\n";
}
// en: English
// fr: Français
// de: Deutsch
URLs
perflocale()->permalink( int $post_id, string $lang_slug ): string
Get the translated permalink for a post in a specific language.
$de_url = perflocale()->permalink( 42, 'de' );
// https://example.com/de/about/
perflocale()->term_link( int $term_id, string $lang_slug ): string
Get the translated term archive link.
$de_cat = perflocale()->term_link( 5, 'de' );
// https://example.com/de/category/news/
perflocale()->home_url( string $lang_slug = '', string $path = '' ): string
Get the home URL for a specific language.
perflocale()->home_url( 'de' );
// https://example.com/de/
perflocale()->home_url( 'de', '/contact/' );
// https://example.com/de/contact/
perflocale()->home_url();
// Current language home URL.
Translations
perflocale()->translations( int $post_id ): array
Get all translations of a post as a [slug => post_id] map.
$translations = perflocale()->translations( 42 );
// ['en' => 42, 'fr' => 56, 'de' => 78]
foreach ( $translations as $slug => $id ) {
echo $slug . ': ' . get_the_title( $id ) . "\n";
}
Switcher
perflocale()->switcher( array $args = [] ): string
Returns the language switcher HTML string.
echo perflocale()->switcher();
echo perflocale()->switcher( [
'template' => 'dropdown',
'hide_current' => true,
] );
See Language Switcher for all available arguments.
Service Container Access
For advanced use cases, the PerfLocale() function (uppercase) returns the service container:
$plugin = PerfLocale();
$router = $plugin->get( 'router' );
$settings = $plugin->get( 'settings' );
$cache = $plugin->get( 'cache' );
$converter = $plugin->get( 'url_converter' );
Template Tags
These standalone functions are available for backward compatibility:
| Function | Description |
|---|---|
perflocale_language_switcher( $args ) | Echo the language switcher |
perflocale_current_language() | Get current language object |
perflocale_get_languages() | Get all active languages |
perflocale_get_permalink( $id, $slug ) | Get translated post URL |
perflocale_get_term_link( $id, $slug ) | Get translated term URL |
perflocale_home_url( $slug, $path ) | Get language home URL |
perflocale_is_rtl() | Check if current language is RTL |
perflocale_hreflang_tags() | Output hreflang tags manually |
Examples
Conditional content by language
if ( perflocale()->is_language( 'fr' ) ) {
get_template_part( 'partials/hero', 'fr' );
} else {
get_template_part( 'partials/hero' );
}
Custom language menu
<ul class="lang-menu">
<?php foreach ( perflocale()->languages() as $lang ) : ?>
<li class="<?php echo perflocale()->is_language( $lang->slug ) ? 'active' : ''; ?>">
<a href="/<?php echo esc_url( perflocale()->home_url( $lang->slug ) ); ?>">
<?php echo esc_html( $lang->native_name ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
Show translation links for current post
<?php
$translations = perflocale()->translations( get_the_ID() );
foreach ( $translations as $slug => $post_id ) :
if ( $post_id === get_the_ID() ) continue;
?>
<a href="/<?php echo esc_url( get_permalink( $post_id ) ); ?>">
Read in <?php echo esc_html( strtoupper( $slug ) ); ?>
</a>
<?php endforeach; ?>