For a single site wordpress, the language must be set from wp-config.php‘s WPLANG, but is it possible to set from my plugin which override the default value?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
In wp-includes/l10n.php you will find the function get_locale(). It offers a filter; you can set the language and ignore the constant:
function get_locale() {
global $locale;
if ( isset( $locale ) )
return apply_filters( 'locale', $locale );
// WPLANG is defined in wp-config.
if ( defined( 'WPLANG' ) )
$locale = WPLANG;
// If multisite, check options.
if ( is_multisite() ) {
// Don't check blog option when installing.
if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) )
$ms_locale = get_site_option('WPLANG');
if ( $ms_locale !== false )
$locale = $ms_locale;
}
if ( empty( $locale ) )
$locale = 'en_US';
return apply_filters( 'locale', $locale );
}
To change it per plugin use the filter 'locale'. Example:
add_filter( 'locale', 'wpse_52419_change_language' );
function wpse_52419_change_language( $locale )
{
return 'de_DE';
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0