sNews 1.6 (previous version) > Mods/addons

[MOD] Multi-language simple Mod

(1/10) > >>

etatus:

EDITED: The last version of this mod can be read below in:   http://snewscms.com/forum/index.php?topic=6421.msg43288#msg43288

Hi, I'm going to explain how to add multiple languages support to our site in 3 steps:


* STEP 1: Modify the starting of snews.php (changes in red):

--- Quote ---/*** CONFIGURATION VARIABLES ***/
// DATABASE VARIABLES
function db($variable) {
   $db = array();
   $db ['website'] = 'siteurl';
   $db['dbhost'] = 'localhost'; //MySQL Host
   $db['dbname'] = 'dbname'; //Database Name
   $db['dbuname'] = 'dbuname'; //Database Username
   $db['dbpass'] = 'dbpasswd'; //Database password
   $db['prefix'] = 'snews_'; //Database prefix
   $db['sessionlang'] = 'EN'; //eTaTuS// Default session language (DB tables without lang prefix for this language)
   $db['dberror'] = '<strong>There was an error while connecting to the database.</strong> <br /> Check your database settings.'; //Database error message   
   //eTaTuS// A language prefix is built
   if ($variable=='prefix') {
      $lang=session_lang();
      return $db['prefix'].($lang==$db['sessionlang'] ? '' : $lang.'_' ); // Database prefix with language
   }else return $db[$variable];
}

//eTaTuS// SESSION LANGUAGE
function session_lang() {
   if (isset($_GET['sessionlang'])) $_SESSION['lang'] = $_GET['sessionlang'];
   if ($_SESSION['lang']) $lang = $_SESSION['lang'];
   else {
      $lang=db('sessionlang');
      $_SESSION['lang'] = $lang;
   }
   return $lang;
}

// LANGUAGE VARIABLES
function l($variable) {
   //eTaTuS// Session language initialization
   $lang=session_lang();
   if ($lang != 'EN' && file_exists('snews_'.$lang.'.php')) {include('snews_'.$lang.'.php');} else {
   $l = array();

   #SITE LANGUAGE VARIABLES
       ...

--- End quote ---


* STEP 2: Add to your index.php the flag links wherever you want with the corresponding language code:
--- Quote ---<!-- //eTaTuS// Language links -->
<a href="&sessionlang=EN"><img src="flags/flag_en.gif" title="English" border="0" /></a>
<a href="&sessionlang=ES"><img src="flags/flag_es.gif" title="Spanish" border="0" /></a>
<!-- Here other flag links... -->

--- End quote ---


* STEP 3: Duplicate DB to others languages:

* Let's suppose our db prefix is 'snews_', our tables will be: snews_articles, snews_categories, snews_comments, snews_settings. These tables are the tables for the default session language set in STEP 1
* If we want to add Spanish language for example, we must copy the original tables to snews_ES_articles, snews_ES_categories, snews_ES_comments, snews_ES_settings (we add a language prefix)
* Then we can change the language of our site by clicking in the flag and translate the settings and articles as administrator
NOTICE:

* The language set on administrator panel is useless now. It will be used the session language set in STEP 1 instead.
* You need to upload your flag images and set their path in flag links
* There are another complex ways to add this feature, this one is quite simple and keeps independent tables for each language (fine for my purposes)
I hope you enjoy it!  ;) Don't hesitate to send me feedback...

EDITED: The last version of this mod can be read below in:   http://snewscms.com/forum/index.php?topic=6421.msg43288#msg43288

etatus:
If you have problems with GET sessionlang parameter (because it goes at the end of the current URL), you can use a POST form to send it:


* STEP 1 modifications (changes in red):

--- Quote ---//eTaTuS// SESSION LANGUAGE
function session_lang() {
   if (isset($_POST['sessionlang'])) $_SESSION['lang'] = $_POST['sessionlang'];
   if ($_SESSION['lang']) $lang = $_SESSION['lang'];
   else {
      $lang=db('sessionlang');
      $_SESSION['lang'] = $lang;
   }
   return $lang;
}
--- End quote ---


* STEP 2 modifications (new code for flag links):

--- Quote ---<!-- //eTaTuS// Language post form -->
<form method="post">
<input type="hidden" name="sessionlang" value="" />
<a href="" onclick="this.parentNode.sessionlang.value='EN';this.parentNode.submit();"><img src="flags/flag_en.gif" title="English" border="0" /></a>
<a href="" onclick="this.parentNode.sessionlang.value='ES';this.parentNode.submit();"><img src="flags/flag_es.gif" title="Spanish" border="0" /></a>
<!-- Here other flag links... -->
</form>
--- End quote ---

Joost:
@etatus

This is the third Multiple language mod on the forum and I am doing a similar project off-line. Reasons for running my own project, is because the ones I've tested so far, have difficulties being accessed by visitors and being indexed by search engines.
The problem is to access a page directly, when it is not a default language. Have you found a way to solve this issue?

Keyrocks:
Perhaps there might be some value in having all 3 authors (led by one) working together on a single, emerging Multiple Languages mod that may, perhaps, be adapted as the best choice for sNews?  ;)

etatus:
@Joost: I was thinking about what you commented about accessing a page directly when it is not a default language (this problem is common with RSS links) and one option could be change the db function to add language to all URL (for example from http://website.com/home/welcome to http://website.com/ES/home/welcome):


--- Quote ---if ($variable=='website') return $db['website'].session_lang().'/'; // Website URL with language
--- End quote ---

And of course it is necessary a solution to remove the language extra path (ES/) and convert it to a GET parameter from incoming requests and this could be done through .htaccess file. Does anyone know how to do this?

Navigation

[0] Message Index

[#] Next page

Go to full version