I’m developing a multi-language web portal for my client – in English and Bengali. Instead of using multisite, I installed Polylang plugin. It’s working fine through the website.
For different CSS styles, and different font-sizes I’m loading a separate stylesheet with:
function styles_and_scripts(){
wp_register_style( 'main-style-bangla', get_template_directory_uri() . '/style-bangla.css', '', '', 'screen' );
// load seperate stylesheet for Bangla
if( get_locale() == 'bn_BD' ) {
wp_enqueue_style( 'main-style-bangla');
}
}
add_action( 'wp_enqueue_scripts', 'styles_and_scripts' );
It’s working just fine, and loading different CSS when I’m switching to Bengali.
I’m trying the same method for applying filters for Bengali Numerals and Strings etc. with these functions and filters. So I’m doing something like this in my functions.php
:
if( get_locale() == 'bn_BD' && !is_admin() ) {
add_filter( 'get_the_time', 'make_bangla_number' );
add_filter( 'the_date', 'make_bangla_number' );
add_filter( 'the_time', 'make_bangla_number' );
add_filter( 'get_the_date', 'make_bangla_number' );
add_filter( 'get_the_time', 'make_bangla_months' );
add_filter( 'the_date', 'make_bangla_months' );
add_filter( 'the_time', 'make_bangla_months' );
add_filter( 'get_the_date', 'make_bangla_months' );
}
But it’s not working. I tried to figure out why?
$my_locale = get_locale();
var_dump( $my_locale );
It’s returning “en_US
” in both the languages. You can see the image below for proof:
Why the same bit of code in the same functions.php
file is behaving strange in different cases? I then changed the placement of the code just after the enqueue portion, but it’s still not working. 🙁
UPDATE
I found a function from Polylang documentation saying:
<?php pll_current_language($value); ?>
(Source^) will retrieve the current language.
I tried var_dump(pll_current_language());
it returns nothing.
Then I managed to find the following code:
function site_current_language() {
global $polylang;
echo $currentLang = pll_current_language('locale');
return $currentLang;
}
//add_action( 'init', 'site_current_language' ); // it does not print
add_action( 'wp', 'site_current_language' ); // it prints
It’s working fine, it’s echoing the current $locale
.
But instead of echoing this, if I tried returning, it’s returning boolean false
.
I tried global $polylang; var_dump($polylang);
without any function or hook, it’s showing that it is failing to find the curlang
within. But if I do the same inside my style/script enqueue fuction, it’s getting the current language.
So I suspect there’s something related with the proper hook. Note, the hook init
is failing to echo, but wp
is echoing. I think we should find a proper hook to get it work.
But how, I don’t know.
You try this example