I have a user based website with WordPress and from their profile settings they are able to select the language, this info and other settings are set for every user in user_meta.
I know how to translate but, is there a way to set the theme language programmatically?
Thank you!
Edit: No plugins please, I need to do this as simple as possible.
Since WP 4.7 you can use:
Reference: https://developer.wordpress.org/reference/functions/switch_to_locale/
I found a different solution:
Via: http://codex.wordpress.org/Function_Reference/load_theme_textdomain
I came up with following solution as I needed to generate invoices from a plugin in different languages in the scope of the same request:
I guess you’re looking for the
override_load_textdomain
filter, called just in the beginning of aload_textdomain
function call.That would be something like:
Code from brain to keyboard, not tested. You should do some more validations and so.
For me only these two solutions together worked.
A had same question, I resolved it this way:
fist step you have to create a .mo with a text domain. You can use loco translate plugin: https://br.wordpress.org/plugins/loco-translate/
Load your text domain linking .mo file:
Change wordpress location via hook. Create a function wich returns wanted location. Use WordPress Locale Id ever, you can see all locales Id here: https://wpastra.com/docs/complete-list-wordpress-locale-codes/
Lets write the function:
You can change by user location. This way wordpress sets the same language was be set by user in its control panel:
Obs: My wordpress version is 5.7. This way can no work in another versions. Good look!
I had a similar problem and solved it like this:
In my case I wanted to retrieve the locale by using the user locale:
$userLocale = get_user_locale($userObject->ID);
I created a custom function to load the correct theme_textdomain with dynamic locale. It’s almost the same as the WP function, but you can add a locale variable:
Based on:
http://queryposts.com/function/load_theme_textdomain/
actually switch_to_locale not working for me, and during debug I have find out
load_textdomain
make out of memory fatal error :/