If you are trying to load WordPress in a specific language with a URL parameter you might find out it is not working properly even if you followed official documentation:
https://codex.wordpress.org/Function_Reference/load_theme_textdomain
Example 2 in codex mentiones, that a locale change should be called before load_theme_textdomain(), however if you add it it functions.php as is – it won’t work properly.
Adding the function into after_setup_theme action didn’t work for me either.
How to load wordpress in a different language with a URL parameter
It turned out to be fixed with a really simple solution: both change locale filter and load_text_domain should be used in a plugin, in order to make it work. Create a new plugin directory in /wp-content/plugins/ and add the following to it’s php file:
/*
Plugin Name: LOAD TEXTDOMAINER
Plugin URI: https://windowspros.ru
Description: Load text domain from URL parameter
Author: Sam Tyurenkov
Version: 1
Author URI: https://windowspros.ru
Tags: language
Text Domain: load-textdomainer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
};
add_filter( 'locale', 'my_theme_localized',10 );
function my_theme_localized( $locale )
{
if ( isset( $_GET['l'] ) )
return esc_attr( $_GET['l'] );
return $locale;
}
add_action( 'after_setup_theme', 'my_pbg_lang');
function my_pbg_lang(){
load_theme_textdomain( 'pbg', get_template_directory() . '/languages' );
}
If you did everything else correctly – you have a .pot file, .mo file for the requested language, and you use localizable strings in wordpress files – you will be able to load a localized version with a URL parameter, e.g. YOURDOMAIN.COM/?l=ru_RU