Enable Browser Language Detection

All new MODs released in our MOD Database will be announced in here. All support for released MODs needs to take place in the Customisations Database.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTICE: This forum is only for the announcement of new releases and/or updates of MODs. Any MOD support should be obtained through the Customisations Database in the support area designated for each MOD.

A direct link to support for each MOD is in the first post of the respective topic.
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hi ScuL,
thanks a lot!
I never thought about this array before. I always looked at the array from the phpBB lang table.

I will make an update to the MOD next days.

Bye Martin
Free tutorial: Installing MODs in phpBB 3.0
Advanced Block MOD - Prevent spam on your phpBB 3.0 board with Stop Forum Spam, BotScout, Akismet, Project Honey Pot and several IP-RBL and Domain-RBL DNS blacklists!
My MODs
Use the official phpBB Ideas to vote missing core features!!!
ScuL
Registered User
Posts: 111
Joined: Tue May 04, 2004 6:01 pm
Location: NZ

Re: Enable Browser Language Detection

Post by ScuL »

Well maybe this way is not the most efficient way. I'm not skilled enough with arrays to do some smart PHP trick. Basically if you can extract the first item in the HTTP_ACCEPT_LANGUAGE array you're safe :)

This mod is very important for my own forum as I'm running on 26 languages.. so that's why I spent the entire afternoon to get it straight :D
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hello ScuL,
at the moment I only have installed two language.

Your line only sets the default value to first value of the array but it still uses all values, right?

I'm not a PHP professional, too! :oops: :?

Bye Martin
Free tutorial: Installing MODs in phpBB 3.0
Advanced Block MOD - Prevent spam on your phpBB 3.0 board with Stop Forum Spam, BotScout, Akismet, Project Honey Pot and several IP-RBL and Domain-RBL DNS blacklists!
My MODs
Use the official phpBB Ideas to vote missing core features!!!
User avatar
GoBieN
Registered User
Posts: 546
Joined: Fri Mar 05, 2004 5:22 pm
Location: Belgium
Contact:

Re: Enable Browser Language Detection

Post by GoBieN »

Martin Truckenbrodt wrote:Hello ScuL,
at the moment I only have installed two language.

Your line only sets the default value to first value of the array but it still uses all values, right?

I'm not a PHP professional, too! :oops: :?

Bye Martin
Because in the browser you can set multiple languages you have an array with multiple values in it.
The problem is you have loop in your code that checks every value that browser has defined against the installed languages. The problem here is that you start with the language thats on top in browerlist, and end with language thats at the bottom of the browser list. And every time your check gets a succes you overwrite the previous selection.

So if i have a board with the following: NL, FR, EN, DE
And a browser with with this: NL, EN (NL being top priority)
It will go like this.

The browser array will have 2 items (NL and EN).
We enter the loop and check the first item:
brower says NL, forum has NL --> succes write selection
We enter the loop again and check the second item:
browser says EN, forum has EN --> succes write selection
Stop loop, because array is at end.

This means i prefer NL and NL is installed, but i end up with EN, because it was the last one to match.

Scul his solution is that he only uses the first item and the browser array and forgets the rest.
This works, but perhaps it would be nicer, to break the loop after the first succes. Because we can then use all the items in the browser array.
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hello GoBieN,
thanks a lot!

So I got it right.

Okay, I will think about it!

Bye Martin
Free tutorial: Installing MODs in phpBB 3.0
Advanced Block MOD - Prevent spam on your phpBB 3.0 board with Stop Forum Spam, BotScout, Akismet, Project Honey Pot and several IP-RBL and Domain-RBL DNS blacklists!
My MODs
Use the official phpBB Ideas to vote missing core features!!!
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hello,
I think I've got it. Please try following code and give me feedback:

Code: Select all

			/**
			* If a guest user is surfing, we try to guess his/her language first by obtaining the browser language
			* If re-enabled we need to make sure only those languages installed are checked
			* Commented out so we do not loose the code.
			* installed language checking added 2007-12-30 by Martin Truckenbrodt
			**/
			if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && request_var('lang', '') == '')
			{
				$sql = 'SELECT lang_iso FROM ' . LANG_TABLE;
				$result = $db->sql_query($sql, 3600);

				while ($row = $db->sql_fetchrow($result))
				{
					$lang_iso_ary[] = $row['lang_iso'];
				}

				$accept_lang_ary = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
				$accept_lang_ary = array_reverse($accept_lang_ary, TRUE);

				foreach ($accept_lang_ary as $accept_lang)
				{
					foreach ($lang_iso_ary as $lang_iso)
					{
						// Set correct format ... guess full xx_YY form
						$accept_lang_xx_yy = basename(substr($accept_lang, 0, 2) . '_' . strtolower(substr($accept_lang, 3, 2)));
						// Set correct format ... guess only xx form
						$accept_lang_xx = basename(substr($accept_lang, 0, 2));

						if (file_exists($phpbb_root_path . 'language/' . $accept_lang_xx_yy . "/common.$phpEx") && $lang_iso == $accept_lang_xx_yy)
						{
							$this->lang_name = $config['default_lang'] = $lang_iso;
							$this->lang_path = $phpbb_root_path . 'language/' . $lang_iso . '/';
						}
						else if (file_exists($phpbb_root_path . 'language/' . $accept_lang_xx . "/common.$phpEx") && $lang_iso == $accept_lang_xx)
						{
							$this->lang_name = $config['default_lang'] = $lang_iso;
							$this->lang_path = $phpbb_root_path . 'language/' . $lang_iso . '/';
						}
					}
				}
				$this->data['user_lang'] = $this->lang_name;
			}
			/*
			*/
Now I'm thinking about if it's possible to add some breaks. But it seems that breaks are not possible. All advices are welcome!

Bye Martin
Free tutorial: Installing MODs in phpBB 3.0
Advanced Block MOD - Prevent spam on your phpBB 3.0 board with Stop Forum Spam, BotScout, Akismet, Project Honey Pot and several IP-RBL and Domain-RBL DNS blacklists!
My MODs
Use the official phpBB Ideas to vote missing core features!!!
ScuL
Registered User
Posts: 111
Joined: Tue May 04, 2004 6:01 pm
Location: NZ

Re: Enable Browser Language Detection

Post by ScuL »

Well maybe i'm missing the point but I don't see why you would need to pass alternative languages in the loop?

phpBB has only 1 default language and that's the one the user selects (or the MOD in this case).
From all languages in the browser it seems most useful to select the language that has the highest priority: the first one. So there is no point in looping through all the other languages.

Your initial mod was fine you just need to find a way to make the loops more efficient so that they will break once the primary language has been determined! :)
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hello ScuL,
what's the matter if the browser default language is not installed at the board?
IMO the other browser languages should have a chance, too.

I know that there are now some loops more. But the loops are not including any SQL actions. And the number of the more loops is related only to the number of languages added to the browser. So IMO the perfomance should be okay. But I will think again about this. I will wait with a new official version of the MOD till we got a really good solution which takes all possible scenarios.

I've tried it yesterday with german langauge Firefox with default settings. There are four languages installed by default!

Bye Martin
Free tutorial: Installing MODs in phpBB 3.0
Advanced Block MOD - Prevent spam on your phpBB 3.0 board with Stop Forum Spam, BotScout, Akismet, Project Honey Pot and several IP-RBL and Domain-RBL DNS blacklists!
My MODs
Use the official phpBB Ideas to vote missing core features!!!
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hello,
the next update see in the following post.

Bye Martin
Last edited by Martin Truckenbrodt on Wed Apr 30, 2008 8:24 pm, edited 1 time in total.
Free tutorial: Installing MODs in phpBB 3.0
Advanced Block MOD - Prevent spam on your phpBB 3.0 board with Stop Forum Spam, BotScout, Akismet, Project Honey Pot and several IP-RBL and Domain-RBL DNS blacklists!
My MODs
Use the official phpBB Ideas to vote missing core features!!!
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hello,
the next try: :)

Code: Select all

			/**
			* If a guest user is surfing, we try to guess his/her language first by obtaining the browser language
			* If re-enabled we need to make sure only those languages installed are checked
			* Commented out so we do not loose the code.
			* installed language checking added 2007-12-30 by Martin Truckenbrodt
			**/
			if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && request_var('lang', '') == '')
			{
				$sql = 'SELECT lang_iso FROM ' . LANG_TABLE;
				$result = $db->sql_query($sql, 3600);

				while ($row = $db->sql_fetchrow($result))
				{
					$lang_iso_ary[] = $row['lang_iso'];
				}

				$accept_lang_ary = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
				$accept_lang_xx_mark = '';

				foreach ($accept_lang_ary as $accept_lang)
				{
					// Set correct format ... guess full xx_YY form
					$accept_lang_xx_yy = basename(substr($accept_lang, 0, 2) . '_' . strtolower(substr($accept_lang, 3, 2)));
					// Set correct format ... guess only xx form
					$accept_lang_xx = basename(substr($accept_lang, 0, 2));

					if (($accept_lang_xx != $accept_lang_xx_mark) && ($accept_lang_xx_mark != ''))
					{
						break;
					}

					foreach ($lang_iso_ary as $lang_iso)
					{
						if (file_exists($phpbb_root_path . 'language/' . $accept_lang_xx_yy . "/common.$phpEx") && $lang_iso == $accept_lang_xx_yy)
						{
							$this->lang_name = $config['default_lang'] = $lang_iso;
							$this->lang_path = $phpbb_root_path . 'language/' . $lang_iso . '/';
							break 2;
						}
						else if (file_exists($phpbb_root_path . 'language/' . $accept_lang_xx . "/common.$phpEx") && $lang_iso == $accept_lang_xx)
						{
							$this->lang_name = $config['default_lang'] = $lang_iso;
							$this->lang_path = $phpbb_root_path . 'language/' . $lang_iso . '/';
							$accept_lang_xx_mark = $accept_lang_xx;
						}
					}
				}
				$this->data['user_lang'] = $this->lang_name;
			}
			/*
			*/
Now the loops are broken again in two ways:
1. A browser language xx_YY is found in the lang_iso table.
2. If the browser gives a language and the lang_iso table only gives a language which contains xx for this,too. Then it is still trying to get a xx_YY till the first browser language gives an other xx part.

Two rules with this MOD:
1. xx_YY still has a higher priority than xx. E.g. browser language array = 1. en, 2. en_US . en_US will be used.
2. The browser language list should have all variations of an xx closed in one line at the priority list. E. g. browser language array = 1. en, 2. fr, 3. en_US . en will be used cause fr breaks the loop.

Please try the code and give me feedback!

Bye Martin
Free tutorial: Installing MODs in phpBB 3.0
Advanced Block MOD - Prevent spam on your phpBB 3.0 board with Stop Forum Spam, BotScout, Akismet, Project Honey Pot and several IP-RBL and Domain-RBL DNS blacklists!
My MODs
Use the official phpBB Ideas to vote missing core features!!!
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hello,
it seems that I have too much time for programming these days. :)

code deleted - see next post

I have deleted both of the rules.
Now the MOD uses this prority list:
1. similar entry in both arrays
2. a xx_YY found in browser array and a xx found in lang_iso table
3. board default language

Now only one scenario is missing:
xx found in browser array but in lang_iso table only xx_YY is found. I'm not sure if it's worth to include this scenario.

Now the loops are broken as soon as possible. Although I fixed a problem then the browser doesn't add
; q=1.0
to the first language. I found this in FireFox.

Bye Martin
Last edited by Martin Truckenbrodt on Thu May 01, 2008 12:31 pm, edited 1 time in total.
Free tutorial: Installing MODs in phpBB 3.0
Advanced Block MOD - Prevent spam on your phpBB 3.0 board with Stop Forum Spam, BotScout, Akismet, Project Honey Pot and several IP-RBL and Domain-RBL DNS blacklists!
My MODs
Use the official phpBB Ideas to vote missing core features!!!
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hello,
the next version:

Code: Select all

			/**
			* If a guest user is surfing, we try to guess his/her language first by obtaining the browser language
			* If re-enabled we need to make sure only those languages installed are checked
			* Commented out so we do not loose the code.
			* installed language checking added 2007-12-30 by Martin Truckenbrodt
			**/
			if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && request_var('lang', '') == '')
			{
				$sql = 'SELECT lang_iso FROM ' . LANG_TABLE;
				$result = $db->sql_query($sql, 3600);

				while ($row = $db->sql_fetchrow($result))
				{
					$lang_iso_ary[] = $row['lang_iso'];
				}

				$accept_lang_ary = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

				foreach ($accept_lang_ary as $accept_lang)
				{
					// Set correct format ... guess full xx_YY form
					$accept_lang_xx_yy = basename(substr($accept_lang, 0, 2) . '_' . strtolower(substr($accept_lang, 3, 2)));
					// Set correct format ... guess only xx form
					$accept_lang_xx = basename(substr($accept_lang, 0, 2));

					foreach ($lang_iso_ary as $lang_iso)
					{
						if (file_exists($phpbb_root_path . 'language/' . $accept_lang_xx_yy . "/common.$phpEx") && $lang_iso == $accept_lang_xx_yy)
						{
							$this->lang_name = $config['default_lang'] = $lang_iso;
							$this->lang_path = $phpbb_root_path . 'language/' . $lang_iso . '/';
							break 2;
						}
						else if (file_exists($phpbb_root_path . 'language/' . $accept_lang_xx . "/common.$phpEx") && $lang_iso == $accept_lang_xx)
						{
							$this->lang_name = $config['default_lang'] = $lang_iso;
							$this->lang_path = $phpbb_root_path . 'language/' . $lang_iso . '/';
							if (substr($accept_lang, 2, 1) == ';' || substr($accept_lang, 2, 1) == '' || strlen($accept_lang) == 2)
							{
								break 2;
							}
						}
						else if (file_exists($phpbb_root_path . 'language/' . $lang_iso . "/common.$phpEx") && substr($lang_iso, 0 , 2) == $accept_lang_xx)
						{
							$this->lang_name = $config['default_lang'] = $lang_iso;
							$this->lang_path = $phpbb_root_path . 'language/' . $lang_iso . '/';
						}
					}
				}
				$this->data['user_lang'] = $this->lang_name;
			}
			/*
			*/
I've added the last scenario.

The topical priority list:
1. similar entry in browser lang array and in lang_iso table => break
2. xx_YY in browser lang array and xx in lang_iso table
3. xx in browser lang array and xx_YY in lang_iso table
4. board default language

Bye Martin
User avatar
A_Jelly_Doughnut
Former Team Member
Posts: 34457
Joined: Sat Jan 18, 2003 1:26 am
Location: Where the Rivers Run
Contact:

Enable Browser Language Detection

Post by A_Jelly_Doughnut »

MOD Updated to version 1.0.6
See first post for Download Link
A Donut's Blog
"Bach's Prelude (Cello Suite No. 1) is driving Indiana country roads in Autumn" - Ann Kish
User avatar
Martin Truckenbrodt
Registered User
Posts: 3045
Joined: Sun Mar 23, 2003 6:22 pm
Location: Franconia
Name: Martin Truckenbrodt
Contact:

Re: Enable Browser Language Detection

Post by Martin Truckenbrodt »

Hello A_Jelly_Doughnut,
thanks a lot for approving the MOD!

http://www.phpbb.com/bugs/phpbb3/ticket ... t_id=27695

Bye Martin
Free tutorial: Installing MODs in phpBB 3.0
Advanced Block MOD - Prevent spam on your phpBB 3.0 board with Stop Forum Spam, BotScout, Akismet, Project Honey Pot and several IP-RBL and Domain-RBL DNS blacklists!
My MODs
Use the official phpBB Ideas to vote missing core features!!!
dbranscombe
Registered User
Posts: 2
Joined: Wed May 28, 2008 6:29 am

Re: Enable Browser Language Detection

Post by dbranscombe »

Martin,
Thanks for all your work on this. You might want to include versioning info in your line: language checking mod v. 2008-05-28 by Martin Truckenbrodt

IMO, in IE7, the second foreach is not breaking because of this line:

Code: Select all

if (substr($accept_lang, 2, 1) == ';' || substr($accept_lang, 2, 1) == '' || strlen($accept_lang) == 2)
                     {
                        break 2;
                     }
$accept_lang (if IE7) will be something like "en_us" or "es_es." Please tell me what you are trying to accomplish by qualifying the break 2 within the above IF statement.

Here is code that works for IE7. Please feel free to use this in your mod.
The design idea: Deliver the user's highest priority code 4 lang requested that we have installed, if we don't have one then deliver the user's highest priority code 2 lang that we have intalled, if not the user recieves the board default
Example code 4 lang would be en_us
Example code 2 lang would be en

This code uses array funcs, some new var names, and includes echos and print_r for troubleshooting.

replace below this line: if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))

Code: Select all

{
				$accept_langs_full = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
				
				// Format browser requested langs ... to match phpBB format, ie. IE7 uses "-"
				foreach ($accept_langs_full as $full)
				{
					$http_accept_langs[] = substr($full, 0, 2) . '_' . strtolower(substr($full, 3, 2));
				}
				//print_r ($http_accept_langs);

				$sql = 'SELECT lang_iso FROM ' . LANG_TABLE;
				$result = $db->sql_query($sql, 3600);

				// get langs installed on board
				while ($row = $db->sql_fetchrow($result))
				{
					$phpBB_installed_langs[] = $row['lang_iso'];
				}
				//print_r ($phpBB_installed_langs);

				// deliver the user's highest priority code 4 lang requested that we have installed, if we don't have one then deliver the user's highest priority code 2 lang that we have intalled, if not the user recieves the board default
				$lang_4_common = array_merge ( array_intersect ( $http_accept_langs, $phpBB_installed_langs));
				if (!empty ($lang_4_common) && file_exists($phpbb_root_path . 'language/' . $lang_4_common[0] . "/common.$phpEx"))
				{
					$this->lang_name = $config['default_lang'] = $lang_4_common[0];
					$this->lang_path = $phpbb_root_path . 'language/' . $lang_4_common[0] . '/';
					//echo ("<br> Choosing language " . $this->lang_name);
				}

				// else let's drop to code 2 langs and deliver the highest priority code 2 we have
				else
				{
					foreach ($http_accept_langs as $lang_4)
					{
						$http_accept_2[] = substr($lang_4, 0, 2);
					}

					$http_accept_langs_2 = array_unique($http_accept_2);	

					$lang_2_common = array_merge ( array_intersect ( $http_accept_langs_2, $phpBB_installed_langs));

					if (!empty ($lang_2_common) && file_exists($phpbb_root_path . 'language/' . $lang_2_common[0] . "/common.$phpEx"))
					{
						$this->lang_name = $config['default_lang'] = $lang_2_common[0];
						$this->lang_path = $phpbb_root_path . 'language/' . $lang_2_common[0] . '/';
						//echo ("<br> Choosing lang_2 " . $this->lang_name);
					}
				}

				// OK to give up, we can't accomodate this user, so let's use the board default lang

				$this->data['user_lang'] = $this->lang_name;
			}	// end of if isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])
		}	// end of else from if ($this->data['user_id'] != ANONYMOUS)
replace above this line: // We include common language file here to not load it every time a custom language file is included
Locked

Return to “[3.0.x] MOD Database Releases”