I am trying to load localization file depends on users settings. Is this way is correct or there are better ways to implement locale changing depends on user settings?
<?php
add_filter( 'locale', 'theme_localized' );
function theme_localized( )
{
if(is_user_logged_in()) {
$locale = get_user_locale();
return $locale;
}
}
?>
This code is working but I am not sure that this is the conventional way of translating website.
I am trying to load localization file depends on users settings. Is this way is correct or there are better ways to implement locale changing depends on user settings?
<?php
add_filter( 'locale', 'theme_localized' );
function theme_localized( )
{
if(is_user_logged_in()) {
$locale = get_user_locale();
return $locale;
}
}
?>
This code is working but I am not sure that this is the conventional way of translating website.
Share Improve this question asked Nov 5, 2018 at 18:36 Nikolai MaksimovNikolai Maksimov 331 silver badge3 bronze badges1 Answer
Reset to default 0By default WordPress only loads translations according to the user's language when they're viewing admin pages.
You can see that in the code for the load_theme_textdomain function:
$locale = apply_filters( 'theme_locale',
is_admin() ? get_user_locale() : get_locale(),
// ^^^^^^^^
$domain );
So your code is fine if you want to override that behaviour for the front end of your site.
Is it conventional? Well, this is what filters are for. However, I'd say this isn't normally done unless the language of the actual content is also changing.