PHP FUNCTION TO SWITCH LANGUAGE
#1
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.
Reply
#2
buuump
Reply
#3
You could update the language field in the mybb_users table?
RedHat Certified Systems Administrator
Reply
#4
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 ?
Reply
#5
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
Reply
#6
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.
Reply
#7
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
Reply
#8
(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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)