Using WordPress ‘load_default_textdomain()’ PHP function

The load_default_textdomain() WordPress PHP function loads the default translated strings based on the locale.

Usage

load_default_textdomain( $locale );

Example:

load_default_textdomain( 'fr_FR' );

This will load the French translation file.

Parameters

  • $locale (string) Optional: Locale to load. Default is the value of get_locale(). Default: null.

More information

See WordPress Developer Resources: load_default_textdomain()

Examples

Loading the Default Locale

This example loads the default locale based on the site’s language setting.

load_default_textdomain();

Loading a Specific Locale

This example loads the German translation file.

load_default_textdomain( 'de_DE' );

Loading a Specific Locale with a Filter

This example loads a specific locale using the locale filter.

function my_custom_locale( $locale ) {
    return 'es_ES';
}
add_filter( 'locale', 'my_custom_locale' );

load_default_textdomain();

Loading the Locale Based on User Preference

This example loads the locale based on a user’s preference stored in the database.

$user_locale = get_user_meta( get_current_user_id(), 'preferred_locale', true );

if ( ! empty( $user_locale ) ) {
    load_default_textdomain( $user_locale );
}

Loading the Locale Based on Browser Language

This example loads the locale based on the user’s browser language.

function get_browser_language() {
    $languages = explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
    return $languages[0];
}

$browser_locale = get_browser_language();

if ( ! empty( $browser_locale ) ) {
    load_default_textdomain( $browser_locale );
}