@@ -283,27 +283,40 @@ function init_language() {
283283
284284 $ installed_language = get_installed_language ();
285285 $ installed_language = array_flip ( $ installed_language );
286+
287+ $ priority_lang = array ();
286288
287- // get the language
288- if ( get ('set_lang_id ' ))
289- $ lang_id = get ( ' set_lang_id ' );
290- elseif ( isset ( $ _SESSION [ ' lang_id ' ]))
291- $ lang_id = $ _SESSION [ ' lang_id ' ];
292- else
293- $ lang_id = get_setting ( 'lang_id ' ) ;
289+ // get the languages
290+ $ requested_lang = get ('set_lang_id ' );
291+ if ( $ requested_lang ) // the first in the priority list is the GET query like ? set_lang_id=en
292+ $ priority_lang [] = $ requested_lang ;
293+
294+ if ( isset ( $ _SESSION [ ' lang_id ' ])) // the second on the priority list is the previously established
295+ $ priority_lang [] = $ _SESSION [ 'lang_id ' ] ;
294296
295- // language not found, load the default language
296- if (!isset ($ installed_language [$ lang_id ])) {
297- $ default_language = array_pop ($ installed_language );
298- $ lang_id = $ default_language ['lang_id ' ];
297+ if (isset ($ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ])) { // the third is the sent by the browser
298+
299+ foreach (explode (', ' , $ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ]) as $ part ) {
300+ $ priority_lang [] = strtolower (substr ($ part , 0 , 2 ));
301+ }
299302 }
300303
301- // set the language in session
302- $ _SESSION ['lang_id ' ] = $ lang_id ;
304+ $ priority_lang [] = get_setting ('lang_id ' );// the fourth is the system
303305
304- // define the constant
305- define ("LANG_ID " , $ lang_id );
306+ $ priority_lang = array_unique ( $ priority_lang );
306307
308+ // through the list of langs to see which is the best to use
309+ while ((list (,$ lang ) = each ($ priority_lang )) && !defined ("LANG_ID " )) {
310+ if (isset ($ installed_language [ $ lang ]))
311+ define ("LANG_ID " , $ lang );
312+ }
313+
314+ if (!defined ("LANG_ID " ))// whether the languages listed is not available
315+ throw new Exception ("Can not find the language file " );
316+
317+ // set the language in session
318+ $ _SESSION ['lang_id ' ] = LANG_ID ;
319+
307320 // load the dictionaries
308321 load_lang ('generic ' );
309322
@@ -546,4 +559,4 @@ protected function _get_load_area( ){
546559 protected function __construct () {}
547560
548561
549- }
562+ }
0 commit comments