And now, without further adieu I present to you...my hack:Part I - new codes:The following are additions of either new files or new codes to exiting files in the Geeklog system.
- Creating arrays of possible languages
Or: creating a new file called languages.php (put it near config.php) :
This file just has 2 arrays - one just for the language changer's form and one in general.
You can enter as many languages as you want (but obviously it's useless to add languages you don't write content for anyway).
Text Formatted Code
<?php
$_languages = array (
'en' => 'English',
'he' => 'Hebrew'
);
$_languagefiles = array (
'en' => 'english_utf-8',
'he' => 'hebrew'
);
?>
See the keys? That's why I've stressed before how important is it to use X_
ln. That's the "ln"!
Creating a "determine the language" functionOr:
a hack for system/lib-custom.php :
First of all, let's define the most important function - this is the function I would keep using later on for everything else.
Text Formatted Code
function language() {
global $_CONF, $_USER;
if ( !empty ($_USER['language']))
$retval = $_USER['language'];
else if ( !empty ($_COOKIE['language']))
$retval = $_COOKIE['language'];
else
$retval = $_CONF['language'];
return $retval;
}
The default language is naturally the one from config.php
From now on, I would usually use substr(language(), 0, 2) . Yes, that's the "ln" again!
Creating a "change a language" blockOr:
a hack for system/lib-custom.php :
Note: you will need to create a new Geeklog block and call the following function from there!
Text Formatted Code
function phpblock_language()
{
global $_CONF;
include($_CONF['path'].'languages.php');
$retval = '';
if ( !isset( $_COOKIE[$_CONF['cookie_language']]))
$need_cookies_for_lang = '<br><font size=1>(Requires cookies)</font>';
$lang = language();
$retval .= '<form name="change" action="'. $_CONF['site_url'] . '/switchlang.php">';
$retval .= '<input type="hidden" name="oldlang" value="' . substr($lang, 0, 2) . '">';
$retval .= '<select onchange="change.submit()" name="lang">';
foreach ($_languages as $key=>$value) {
if ($lang == $_languagefiles[$key])
$selected = " selected";
else $selected = "";
$retval .= '<option value="' . $key . '"' . $selected. '>' . $value . '</option>';
}
$retval .= '</select>';
if (isset($need_cookies_for_lang)) $retval .= $need_cookies_for_lang;
$retval .= '</form>';
return $retval;
}
Yes, this is the worst part about this hack. It demands cookies even form anonymous users. But how else can make sure a user only gets his/her language?
Creating a file for the language changer's formOr:
creating a new file called public_files/switchlang.php :
Text Formatted Code
<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Geeklog 1.4 |
// +---------------------------------------------------------------------------+
// | switchLang.php |
// | Switches between translation files. |
// | |
// | Copyright (C) 2006 by the following author: |
// | http://lior.weissbrod.com |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2000-2003 by the following authors: |
// | |
// | Authors: Stratos Gerakakis - admin@stratosgear.com |
// | Robert Dobozy - robogl7@robod.net |
// +---------------------------------------------------------------------------+
// | |
// | This program 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. |
// | |
// | This program 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. |
// | |
// +---------------------------------------------------------------------------+
//
include_once('lib-common.php');
include($_CONF['path'].'languages.php');
//If not allowed, just ignore and return
if ($_CONF['allow_user_language'] == 1) {
if (isset($_REQUEST['oldlang'])) {
$oldlang = strtolower(COM_applyFilter($_REQUEST['oldlang']));
}
// do we really have a new language to switch to?
if (isset($_REQUEST['lang'])) {
$lang = strtolower(COM_applyFilter($_REQUEST['lang']));
// does it exist in our current translation list?
if( is_file( $_CONF['path_language'] . $_languagefiles[$lang] . '.php' )) {
// Set the language cookie.
// Mainly used for the anonymous user so the rest of his session
// will remain in his selected language
setcookie ($_CONF['cookie_language'], $_languagefiles[$lang],
time() + 31536000, $_CONF['cookie_path'],
$_CONF['cookiedomain'], $_CONF['cookiesecure']);
// if he is not anonymous, store the preference in the database
if ($_USER['uid'] > 1) {
DB_query("UPDATE {$_TABLES['users']} SET language='{$_languagefiles[$lang]}'
WHERE uid = {$_USER['uid']}");
}
}
}
}
// If the user came from within your site, send them back to the referrer page
if (isset($_SERVER['HTTP_REFERER']))
if (strpos($_SERVER['HTTP_REFERER'], $_CONF['site_url']) !== false) {
$ret = $_SERVER['HTTP_REFERER'];
// Change the language's ID if needed
if (isset($lang) && isset($oldlang)) {
if (strpos($ret, '&') !== false) {
$ret = split("&", $ret);
$extra_vars = "&" . $ret[1];
$ret = $ret[0];
} else $extra_vars = '';
if (substr($ret, -3) == '_' . $oldlang)
$ret = substr_replace($ret, $lang, -2);
$ret .= $extra_vars;
}
}
// If the user didn't come from within your site, send them to your main page
if (!isset($ret))
$ret = $_CONF['site_url'] . '/';
Header("Location: $ret");
?>
The best thing I like about this is that if you change a language, you'll be taken to the same item you were in only in the new language.
I've made many fixes and improvements in this file.
Showing only stories of a certain languageOr:
a hack for public_html/index.php :
"Empty topic" mode is an interesting case (or /index.php without any parameters - in human lingo). It pulls stories from every topic the user has access to.
So let's add to that...in that user's language!
Text Formatted Code
$sql = " (date <= NOW()) AND (draft_flag = 0)";
// custom code - start
if (empty($topic)) {
$lang = "%_" . substr(language(), 0, 2);
$sql .= " AND s.tid LIKE '$lang' ";
}
// custom code - end
// if a topic was provided only select those stories.
if (!empty($topic)) {
See, that's it!
Part II - hacks to exiting codes:The following are (minimal as possible)
modifications for existing codes. So
use them instead of current codes and that means do
not - I repeat, do
NOT - add them in addition to the original codes as it would create a real mess and/or end life as we know it!
- Showing only topics of a certain language
Or: a hack for public_html/lib-common.php -
function COM_showTopics :
The list of topics is the list of topics the user has access to.
So let's add to that...in that user's language!
Text Formatted Code
$sql = "SELECT tid,topic,imageurl FROM {$_TABLES['topics']}";
// custom code - start
$lang = "%_" . substr(language(), 0, 2);
$sql .= " WHERE tid LIKE '$lang' ";
// custom code - end
if( $_USER['uid'] > 1 )
{
$tids = DB_getItem( $_TABLES['userindex'], 'tids',
"uid = '{$_USER['uid']}'" );
if( !empty( $tids ))
{
// custom code - start
$sql .= " AND (tid NOT IN ('" . str_replace( ' ', "','", $tids )
. "'))" . COM_getPermSQL( 'AND' );
// custom code - end
}
else
{
// custom code - start
$sql .= COM_getPermSQL( 'AND' );
// custom code - end
}
}
else
{
// custom code - start
$sql .= COM_getPermSQL( 'AND' );
// custom code - end
}
if( $_CONF['sortmethod'] == 'alpha' )
Those are just minimal changes. It's just that the "topics' list" code is spread across many lines.
Showing only polls of a certain languageor:
a hack for public_html/lib-common.php -
function COM_showPoll :
Up to Geeklog v1.3.11
only! (or: before polls became a plugin).
I've decided not to hack public_html/polls.php itself, but those of you who are still with me should know how to do it yourselves.
However, surely I must match the homepage's polls to the chosen language, which is pretty easy.
Text Formatted Code
else
{
// custom code - start
$lang = "%_" . substr(language(), 0, 2);
$sql = "AND qid LIKE '$lang' ";
$result = DB_query( "SELECT qid from {$_TABLES['pollquestions']} WHERE display = 1 $sql ORDER BY date DESC" );
// custom code - end
$nrows = DB_numRows( $result );
Again, that's it!
Part III - PluginsThe following are (minimal as possible)
modifications for existing codes in plugins' files. That means you must use them
instead of current codes and
not in addition.
Polls (starting in Geeklog v1.4)
- Featured polls on the homepage
or: a hack for /plugins/polls/functions.inc -
function POLLS_pollVote :
Text Formatted Code
// custom code - start
$lang = "%_" . substr(language(), 0, 2);
$sql = "AND qid LIKE '$lang' ";
$question = DB_query( "SELECT question,voters,commentcode,owner_id,group_id,perm_owner,perm_group,perm_members,perm_anon FROM {$_TABLES['pollquestions']} WHERE qid='$qid' $sql" );
// custom code - end
$Q = DB_fetchArray( $question );
The polls' list pageor:
a hack for public_html/polls/index.php :
Text Formatted Code
// custom code - start
$lang = "%_" . substr(language(), 0, 2);
$sql = "qid LIKE '$lang' ";
$query_arr = array('table' => 'pollquestions',
'sql' => $sql = "SELECT *,UNIX_TIMESTAMP(date) as unixdate, display FROM {$_TABLES['pollquestions']} WHERE $sql",
// custom code - end
'query_fields' => array('question'),
To the brave few who got this far - I hope your site will become multilingual soon!
You can always contact me for help.
And may this hack someday become official!