1 (edited by nikos 2012-03-05 13:18)

Topic: [Modification] Dynamic change default language for forum guests

I have a multilingual website and i use punbb.
My website is in Greek and English language and all i wanted to achieve was to send the user in the specific language of the forum.
So i made some coding to do that!

This modification sets the language by the url.
So, if you will send the user to yourdomain.com/forums/?lang=Greek the default language of the forum will be Greek.
If you will send the user to yourdomain.com/forums/?lang=English the default language of the forum will be English, and so on...

When the user goes into a specific url of the forum with the lang on it, it creates a cookie to remember it so the language will remain as default as the user surfs in the forum.

If the user is registered and login's into the forum, it ignores this new cookie and shows the forum in the language that the user saved when registered!

So all you need to do is:
1. Open the file include/functions.php
2. Find the line 1558

$forum_user['language'] = $forum_config['o_default_lang'];

3. Replace the line 1558 with the following code

if($_GET['lang']) {
        if (file_exists('lang/'.$_GET["lang"].'/common.php')) {
            setcookie("thedefaultlang", $_GET['lang'], time()+3600);
            $TheDefaultLang = $_GET['lang'];
        } else {
            $TheDefaultLang = $forum_config['o_default_lang'];
        }
    } else { 
        if(isset($_COOKIE["thedefaultlang"])) {
            $TheDefaultLang = $_COOKIE["thedefaultlang"];
        } else {
            $TheDefaultLang = $forum_config['o_default_lang'];
        }
    }
    $forum_user['language'] = $TheDefaultLang;

I hope this will help many others who have multilingual websites like me! smile

Re: [Modification] Dynamic change default language for forum guests

Hello,

your script don't work in ma website sad

3

Re: [Modification] Dynamic change default language for forum guests

Which version of PunBB you use and what languages you have?
If you want, post the functions.php and i will change it for you.

Re: [Modification] Dynamic change default language for forum guests

Great Modification ! Work fine, thanks.

But maybe don't forget to test the cookie too, so the user cannot modify it in order to inject something etc :

if($_GET['lang']) {
        if (file_exists('lang/'.$_GET["lang"].'/common.php')) {
            setcookie("thedefaultlang", $_GET['lang'], time()+3600);
            $TheDefaultLang = $_GET['lang'];
        } else {
            $TheDefaultLang = $forum_config['o_default_lang'];
        }
    } else { 
        if(isset($_COOKIE["thedefaultlang"]) 
           && file_exists('lang/'.$_COOKIE["thedefaultlang"].'/common.php')) {
            $TheDefaultLang = $_COOKIE["thedefaultlang"];
        } else {
            $TheDefaultLang = $forum_config['o_default_lang'];
        }
    }
    $forum_user['language'] = $TheDefaultLang;

Re: [Modification] Dynamic change default language for forum guests

reigyx_x wrote:

please tell me, where the example...?

The example of... ?

Re: [Modification] Dynamic change default language for forum guests

ur link web i mean T__T

sorry my BAD english T___T
Have a nice day >.<
(^____^)v

7

Re: [Modification] Dynamic change default language for forum guests

fristaille wrote:

Great Modification ! Work fine, thanks.

But maybe don't forget to test the cookie too, so the user cannot modify it in order to inject something etc :

if($_GET['lang']) {
        if (file_exists('lang/'.$_GET["lang"].'/common.php')) {
            setcookie("thedefaultlang", $_GET['lang'], time()+3600);
            $TheDefaultLang = $_GET['lang'];
        } else {
            $TheDefaultLang = $forum_config['o_default_lang'];
        }
    } else { 
        if(isset($_COOKIE["thedefaultlang"]) 
           && file_exists('lang/'.$_COOKIE["thedefaultlang"].'/common.php')) {
            $TheDefaultLang = $_COOKIE["thedefaultlang"];
        } else {
            $TheDefaultLang = $forum_config['o_default_lang'];
        }
    }
    $forum_user['language'] = $TheDefaultLang;

Nice addition mate! wink