1 (edited by Razmooze 2004-06-10 09:08)

Topic: Individual language selection

Hi!

Was wondering if there is any mod out there for individual language selection so that different users can select from a list of different languages just like they select a theme?

If not, I might try to do it myself.
I guess such a mod would only affect profile.php and config.php, right?

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

Yes, I have made such a mod:

MultiLanguage Mod
=================

for PunBB v.1.1.x

some considerations:

- language selection will be placed as a primary function in
  the main navigational links, since we want language selection
  to be available to all users, including guests.

- language selection will not be placed on user's profile page,
  since we do not want this functionality to suddenly move there
  when the user decides to register.

- language selection will not be displayed on forum installations
  with only one installed language.

with these objectives in mind, off we go.

first, add a "language" column to the "user" table - on mysql,
this is done with the following statement:

ALTER TABLE `users` ADD `language` VARCHAR( 10 ) DEFAULT NULL;

in "config.php", after setting $language (which now determines
the default language), add an array of installed languages:

$languages = array(
    'en' => 'English',
    'de' => 'German',
    'ru' => 'Russian'
);

defining $languages in "config.php" is of course optional, and
language selection will not be present on the users' profiles
page if only one language is installed.

some sections in "common.php" need to be moved around a bit.
cut the code between the following remark and the next:

    // Check/update/set cookie and fetch user info

paste it before the section that begins with the remark:

    // Enable output buffering

then cut the code section following this remark:

    // Attempt to load the common language file

and paste that before the section with this remark:

    // Enable output buffering

these changes won't immidiately affect anything, they're just
necessary because we need to change the language setting in
check_cookie() before the language files are loaded.

so next up is "functions.php", where we have to load the
users language setting into the global $language .. go to
the function with the header:

function check_cookie(...)

add $language to the global statement, like so:

    global $language, $db, $pun_config, $pun_root, $cookie_name....
          ^ there

then find the following line:

    $expire = $now + 31536000;    // The cookie expires after a year

and paste the following chunk of code after it:

    // Switch to language specified by cookie:
    if (strlen($_COOKIE[$cookie_name.'_lang']) > 0) {
        if (file_exists($pun_root.'lang/'.$_COOKIE[$cookie_name.'_lang']))
            $language = $_COOKIE[$cookie_name.'_lang'];
    }

now we must initialize the language cookie from the user's setting
when he logs in, so edit "login.php", and find these lines:

    $result = $db->query('SELECT id, username, password, save_pass, status FROM '.$db->prefix.'users WHERE username=\''.addslashes($form_username).'\'') or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
    list($user_id, $db_username, $db_password_hash, $save_pass, $status) = $db->fetch_row($result);

change them to:

    $result = $db->query('SELECT id, username, password, save_pass, status, language FROM '.$db->prefix.'users WHERE username=\''.addslashes($form_username).'\'') or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
    list($user_id, $db_username, $db_password_hash, $save_pass, $status, $setlanguage) = $db->fetch_row($result);

find the setcookie statement:

    setcookie($cookie_name, serialize(array($db_username, $form_password_hash)), $expire, $cookie_path, $cookie_domain, $cookie_secure);

and add this line below it:

    setcookie($cookie_name.'_lang', $setlanguage, $expire, $cookie_path, $cookie_domain, $cookie_secure);

in "lang/??common.php" language files, two new strings need to be
added at the end of the "Stuff for the navigator" section:

'Language'              => 'Language',
'Language info'         => 'Select the language in which you wish to browse this forum.',

a user that isn't logged in (Guest), will have a language selection
button in their navigation bar - edit "functions.php" and find the
global statement in function generate_navlinks:

    global $pun_config, $lang_common, $cookie, $cur_user;

add $languages to it:

    global $pun_config, $lang_common, $cookie, $cur_user, $languages;

find this statement:

    return implode(' | ', $links);

and add this code just above it:

    if (isset($languages))
        $links[] = '<a href="language.php">'.$lang_common['Language'].'</a>';

"language.php" is a new script, copy the included file.

Here's the source for the "language.php":

<?php
/***********************************************************************

  Copyright (C) 2002, 2003, 2004  Rickard Andersson (rickard@punbb.org)

  This file is part of PunBB.

  PunBB is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  PunBB is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA  02111-1307  USA

************************************************************************/


$pun_root = './';
require $pun_root.'include/common.php';


// Load the common.php language file
require $pun_root.'lang/'.$language.'/'.$language.'_common.php';

// Switch language:
if (strlen($_GET['set_lang']) > 0)
{
    setcookie($cookie_name.'_lang', $_GET['set_lang'], $expire, $cookie_path, $cookie_domain, $cookie_secure);
    
    if (!$cookie['is_guest'])
        $db->query('UPDATE '.$db->prefix.'users SET language="'.$_GET['set_lang'].'" WHERE id='.$cur_user['id']) or error('Unable to update user language setting', __FILE__, __LINE__, $db->error());
    
    redirect('language.php', 'Switching to your selected language...');
}

$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / '.$lang_common['Language'];
require $pun_root.'header.php';

?>
<table class="punspacer" cellspacing="1" cellpadding="4"><tr><td> </td></tr></table>

<form method="get" action="language.php">
    <table class="punmain" cellspacing="1" cellpadding="4">
        <tr class="punhead">
            <td class="punhead"><?php echo $lang_common['Language'] ?></td>
        </tr>
        <tr>
            <td class="puncon2cent">
<?php

echo $lang_common['Language info'].'<br><br>';

echo '<select name="set_lang">';

if (isset($_COOKIE[$cookie_name.'_lang']))
    $current = $_COOKIE[$cookie_name.'_lang'];
else
    $current = '';

foreach($languages as $langid => $langname)
{
    echo '<option'.($current==$langid ? ' selected' : '').' value="'.$langid.'">'.$langname.'</option>';
}

echo '</select>';

?>
                <br><br>
                <input type="submit" value="<?php echo $lang_common['Submit'] ?>" tabindex="4" accesskey="s">
            </td>
        </tr>
    </table>
</form>

<table class="punspacer" cellspacing="1" cellpadding="4"><tr><td> </td></tr></table>

<?php

require $pun_root.'footer.php';

Re: Individual language selection

Great! Thanx man!!!
I'll try it out ASAP!

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

but where do I put the file language.php?

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

Erm, did you download the Swedish language pack?

Re: Individual language selection

yep... I even had Swedish on befor I modded the forum

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

...But now, when I changed the files back to their previous state, I got this error message instead:

The file 'config.php' doesn't exist or is corrupt. Please run install.php to install PunBB first.

and of course if I click on "install.php" I get this:

The file 'config.php' already exists which would mean that PunBB is already installed. You should go here instead.

but I already knew that.

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

You changed ALL the files back and got that?

9 (edited by Razmooze 2004-06-11 19:46)

Re: Individual language selection

yup... hmm.... this is really weird.

The files I altered (and thus changed back) was:

config.php
login.php
include/common.php
include/functions.php
lang/en/en_common.php
lang/se/se_common.php

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

*getting desperate* :_(

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

11 (edited by Razmooze 2004-06-11 21:43)

Re: Individual language selection

Oh no! I'm such an IDIOT!!!

there was a code fragment left in the config.php file (I should've realized that  before):

...
$language = 'se';

);

define('PUN', 1); 
...

http://www.smyrnakyrkan.se/punbb/upload/img/smilies/crazy.gif

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

So, now... back to the original question... *hrrm*
Why does it bail out as I change the language from english to swedish? I realize there could be a thousand factors involved, but, any ideas possibly?

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

Erm, did you put language.php in? I think it goes with the other pages (viewforum, index, etc)

Re: Individual language selection

Yup... but i discovered the fault now. It was a stupid comma that I forgot to put after a translation string in the se_common.php file.
http://www.smyrnakyrkan.se/punbb/upload/img/smilies/crazy.gif

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

Thanx for a good mod mindplay and thanx for the help Smartys.

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.

Re: Individual language selection

I did some modifications myself, like replacing the redirect-command in language.php with the following:

 redirect('language.php', $lang_common['Language redirect']);

and then added the element

'Language redirect'     => 'Switching to your selected language...', 

into en_common.php and the like...

Another modification i did was to change the location of the Language-link in the menubar to the right of the search-link (by inserting the code for the language link under all different user-cases; Guest, User, Admin).

No electrons were harmed in the creation of this post.
However, many were excited and some may have enjoyed the experience.