MyBB Community Forums

Full Version: PHP FUNCTION TO SWITCH LANGUAGE
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

What is the PHP function to switch the language of a user only ? I want to make a script if a player is english, forum is in english too else if is in french, forum is too.
buuump
You could update the language field in the mybb_users table?
I can't because I wan't to change language of guests members too.

I want the same function of : <select name="language" onchange="MyBB.changeLanguage();" style="width: 100%;"> in javascript but in PHP ?
There's no function. You could do something similar to this (it sets language if user didn't choose any): https://github.com/Destroy666x/MyBB-Brow...#L55-#L132
You may use the HTTP_ACCEPT_LANGUAGE header sent by the user.

Change line 94 in global.php from:
$lang->set_language($mybb->settings['bblanguage']);
to:
// Fallback (board default) language
$chosen_lang = $mybb->settings['bblanguage'];

// Add a new case for each language you have installed in ./inc/languages/
switch(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)) {
	case 'en':
		$chosen_lang = 'english';
		break;
	case 'fr':
		$chosen_lang = 'french';
		break;
}

$lang->set_language($chosen_lang);

You may want to consider using the Patches plugin so that you can apply the change again when you upgrade your forum.
The code above is not fully correct though considering they may be e.g. different en versions, some country codes also have 3 characters. A regex is better: https://github.com/Destroy666x/MyBB-Brow...e.php#L145
(2016-04-10, 05:31 PM)Destroy666 Wrote: [ -> ]The code above is not fully correct though considering they may be e.g. different en versions, some country codes also have 3 characters. A regex is better: https://github.com/Destroy666x/MyBB-Brow...e.php#L145

I completely agree, I was just suggesting a simple method to solve the problem. You could always edit the switch statement to chose how many characters you look at and add cases for each language you support. Smile