0

I need to change the wordpress language conditionally. So I use the code:

//Change Site Locale
function wpsx89972_redefine_locale($locale) {
if .....my condition exists... {
    $locale = 'en_US';
} else { 
   $locale = 'fr_FR'; 
}
return $locale;

}
add_filter('locale','wpsx89972_redefine_locale',10);

This way I can use my translations but the core's and the theme's language does not change. For example "Leave a Comment" or the month of the published date remain in English.

The solution used to be to call conditionally the function

define ('WPLANG', 'fr_FR');

but now that is deprecated how could I change the core's translation?

  • As far as I can tell, that filter's correct, but are you sure that your theme has a translation in the language you want? – Jacob Peattie Jul 8 '18 at 8:34
  • The filter is correct and is working but define ('WPLANG', 'fr_FR'); is deprecated. It does not work anymore... So the core translations do not change. Yes my theme has the translation in the language I need – Ioannis Jul 8 '18 at 10:54
0

You must define this filter as early as you can, before it is used anywhere. Best way to do it is to create must-use plugin.

I find the concept incredibly usefull since this scripts don't require any setup like plugins do, and can stage environment for both your plugins and theme. Also they can't be deactivated

  • define ('WPLANG', 'fr_FR'); is deprecated so it will not work in a must-use-plugin. It has been replaced by an option called WPLANG stored in the table <TablePrefix>_options Do you know how could I use it with php in my conditional filter so the core language changes too when specific conditions exist? – Ioannis Jul 8 '18 at 11:00
  • You are right! Finally I found out that the WPLANG is not needed, I just had to place my code in a mu-plugin. It is working like a charm now! Thank you for pointing me to the right direction!!! – Ioannis Jul 8 '18 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.