Support Log in

2. Language Detection Functions

Developer Guide
Prefer video? Watch the Lang Forge tutorials 9 short guides covering every feature Watch

These functions determine which language is currently active, what the default language is, and which languages are configured on the site. They are defined in langforge.php and available globally once the plugin is active.

lf_get_current_language()

Returns the currently active language code.

php
/**
 * Get the current language code.
 *
 * Detection order:
 *   1. URL prefix / parameter / subdomain / domain mapping
 *   2. Site default language
 *
 * Note: when `lf_auto_detect_language` is enabled, the Accept-Language
 * header is consulted ONCE per browser at front-controller time. If a
 * non-default language is preferred, the request is redirected to the
 * `/{lang}/` URL before `template_redirect` runs — so by the time any
 * template code calls `lf_get_current_language()`, the language has
 * been "chosen" by the URL. The function itself never returns a
 * browser-detected language in place of a URL-derived one.
 *
 * @return string Language code (e.g., 'en', 'ru', 'de')
 */
$lang = lf_get_current_language();
// Returns: 'en'
ParameterTypeDescription
(none)This function takes no parameters
Return TypeDescription
stringTwo-letter language code. Never returns empty; falls back to default language.

The current language is detected from the URL (directory prefix, query parameter, subdomain, or domain mapping), then falls back to the site default language. Cookie-based detection is intentionally avoided for clean default-language URLs. If browser auto-detection is enabled (lf_auto_detect_language), it is applied via a 302 redirect to /{lang}/ on the visitor’s first page load rather than by silently switching language at runtime — this keeps hreflang, canonical URLs, and crawler indexing clean. The redirect sets a long-lived lf_autodetect_done cookie so it runs at most once per browser.

Example 1: Language-conditional logic
php
// Load language-specific stylesheets
$lang = lf_get_current_language();

if ($lang === 'ar' || $lang === 'he') {
    // Load RTL stylesheet for Arabic and Hebrew
    wp_enqueue_style('theme-rtl', get_template_directory_uri() . '/css/rtl.css');
}

if ($lang === 'ru') {
    // Load Russian-specific typography adjustments
    wp_enqueue_style('theme-ru', get_template_directory_uri() . '/css/ru.css');
}
Example 2: Conditional body class
php
// functions.php - Add language as a body class
add_filter('body_class', function ($classes) {
    $classes[] = 'lang-' . lf_get_current_language();
    return $classes;
});
// Output: <body class="... lang-en">
Example 3: Language-specific widget area
php
// sidebar.php
$lang = lf_get_current_language();

if (is_active_sidebar('sidebar-' . $lang)) {
    dynamic_sidebar('sidebar-' . $lang);
} else {
    dynamic_sidebar('sidebar-default');
}

lf_get_default_language()

Returns the site’s default language code as configured in Lang Forge > Settings.

php
/**
 * Get the site's default language code.
 *
 * @return string Default language code (e.g., 'ru')
 */
$default = lf_get_default_language();
// Returns: 'ru'
ParameterTypeDescription
(none)This function takes no parameters
Return TypeDescription
stringThe default language code configured in settings
Example 1: Check if viewing a non-default language
php
$current = lf_get_current_language();
$default = lf_get_default_language();

if ($current !== $default) {
    // Show a "View original" link
    $default_langs = lf_get_languages();
    $default_name = $default_langs[$default]['native_name'] ?? $default;
    echo '<a href="' . esc_url(home_url('/')) . '">';
    echo esc_html(sprintf('View in %s', $default_name));
    echo '</a>';
}
Example 2: Apply default language as the HTML lang attribute fallback
php
// functions.php
add_filter('language_attributes', function ($output) {
    $lang = lf_get_current_language();
    $languages = lf_get_languages();
    $locale = $languages[$lang]['locale'] ?? $lang;
    return 'lang="' . esc_attr(str_replace('_', '-', $locale)) . '"';
});
Example 3: Redirect to default language on 404
php
add_action('template_redirect', function () {
    if (is_404()) {
        $default = lf_get_default_language();
        $current = lf_get_current_language();
        if ($current !== $default) {
            wp_safe_redirect(home_url('/'));
            exit;
        }
    }
}, 99);

lf_get_languages()

Returns an associative array of all active languages configured in the site.

php
/**
 * Get all active languages.
 *
 * @return array Associative array keyed by language code.
 *               Each entry contains: name, native_name, flag, locale
 */
$languages = lf_get_languages();
ParameterTypeDescription
(none)This function takes no parameters
Return TypeDescription
arrayAssociative array keyed by language code
Return format:
php
[
    'ru' => [
        'name'        => 'Russian',
        'native_name' => 'Русский',
        'flag'        => 'ru',
        'locale'      => 'ru_RU',
    ],
    'en' => [
        'name'        => 'English',
        'native_name' => 'English',
        'flag'        => 'gb',
        'locale'      => 'en_US',
    ],
]
Example 1: Build a custom language list with flags
php
$languages = lf_get_languages();
$current   = lf_get_current_language();
$core      = LF_Core::instance();

echo '<ul class="custom-lang-list">';
foreach ($languages as $code => $lang) {
    $active_class = ($code === $current) ? ' class="active"' : '';
    $url = esc_url($core->get_language_url($code));
    $flag_url = plugins_url('langforge/assets/flags/' . $lang['flag'] . '.svg');

    echo '<li' . $active_class . '>';
    echo '<a href="' . $url . '">';
    echo '<img src="' . esc_url($flag_url) . '" alt="" width="20" height="14"> ';
    echo esc_html($lang['native_name']);
    echo '</a>';
    echo '</li>';
}
echo '</ul>';
Example 2: Generate JSON for a JavaScript language picker
php
// Enqueue language data for a frontend JS component
add_action('wp_enqueue_scripts', function () {
    $languages = lf_get_languages();
    $current = lf_get_current_language();
    $core = LF_Core::instance();

    $lang_data = [];
    foreach ($languages as $code => $lang) {
        $lang_data[] = [
            'code'       => $code,
            'name'       => $lang['name'],
            'nativeName' => $lang['native_name'],
            'flag'       => $lang['flag'],
            'url'        => $core->get_language_url($code),
            'isCurrent'  => ($code === $current),
        ];
    }

    wp_localize_script('theme-main', 'lfLanguages', $lang_data);
});
Example 3: Language count for conditional logic
php
$languages = lf_get_languages();

// Only show language switcher if more than one language is active
if (count($languages) > 1) {
    echo lf_language_switcher(['style' => 'dropdown']);
}

Forge AI Assistant Online

Hi! I'm the Lang Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs