Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3 4

Author Topic: [MOD] Multi-language simple Mod  (Read 17146 times)

etatus

  • Newbie
  • *
  • Karma: 2
  • Posts: 8
[MOD] Multi-language simple Mod
« on: December 03, 2007, 01:52:14 PM »


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
           ...

  • 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... -->

  • 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
« Last Edit: January 30, 2008, 08:12:19 PM by etatus »
Logged

etatus

  • Newbie
  • *
  • Karma: 2
  • Posts: 8
Re: [MOD] Multi-language simple Mod
« Reply #1 on: December 03, 2007, 05:43:34 PM »

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;
    }

  • 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>
« Last Edit: December 05, 2007, 08:03:03 PM by etatus »
Logged

Joost

  • Guest
Re: [MOD] Multi-language simple Mod
« Reply #2 on: December 03, 2007, 06:13:28 PM »

@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?

Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: [MOD] Multi-language simple Mod
« Reply #3 on: December 03, 2007, 06:26:41 PM »

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?  ;)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

etatus

  • Newbie
  • *
  • Karma: 2
  • Posts: 8
Re: [MOD] Multi-language simple Mod
« Reply #4 on: December 06, 2007, 02:54:22 PM »

@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

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?
« Last Edit: December 07, 2007, 12:41:47 PM by etatus »
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
Re: [MOD] Multi-language simple Mod
« Reply #5 on: December 07, 2007, 11:49:50 AM »

@etatus : I'm using setup sufficiently similar to Yours ; so, here is how I dealt with url-s:

I use following rewrite rules in .htaccess to handle languages:
Code: [Select]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?category=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z][a-z])/index.php?(.*)$ $2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z][a-z])$ index.php?lang=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z][a-z])/$ index.php?lang=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z][a-z])/(.*)$ index.php?lang=$1&category=$2 [L]

And I'm using get_id('lang') to retrieve language from url as any other parameter in snews. To do that I changed beginning of function getGetParm() to look like this:
Code: [Select]
function getGetParm( $parmName ) {
   if ( $parmName == 'lang' ) {
      $url = explode('/', $_GET['lang']);
   }
   else {
      $url = explode('/', $_GET['category']);
   }
   switch ($parmName) {
      /*
      case 'category' :
         if ( $url[0] ) {
            $parmValue = cleanGetSef( $url[0] );
         }
         break;
      */
      // Test version
      case 'lang' :
         if ( $url[0] ) {
            // FIX THIS : Add language code validation here
            $parmValue = cleanGetSef( $url[0] );
         }
         break;
      case 'category' :
         if ( $url[0] ) {
            $parmValue = ($url[0] == 'sitemap.xml') ? $url[0] : cleanGetSef( $url[0] );
         }
         break;
Starting with case 'category' : and below everything is unchanged. Function getGetParm() is from this MOD.

Of course all links across engine/mods are updated to show proper language component as needed.
Logged

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
Re: [MOD] Multi-language simple Mod
« Reply #6 on: December 07, 2007, 11:57:03 AM »

I might be a dumbass, but I need to ask something...

When having a multiple language site... you need to enter the articles in each language in the specific db, no ? I mean the translation of the actual articles do not automatically happen, right ?

So, whats the big deal with this ? Whats the fault in simply make another DB and add the articles in that language you want, and on the default index simply link to that index.php which is afdded to a subfolder containing the additional files ?

Is it a space thingy ? In order to not need to add an additional subfolder ? In that case I get it, then my question is, in fact, a dumbass question, hehe...
Logged
"It's only dead fish that goes with the flow... "
Updated

etatus

  • Newbie
  • *
  • Karma: 2
  • Posts: 8
Re: [MOD] Multi-language simple Mod
« Reply #7 on: December 07, 2007, 01:57:52 PM »

@codetwist : Great! thank you, I will check and adapt to my multi-language solution and I'll comment here...

@Patric Ahlqvist:

There are two types of online site translation. One (easiest) is the interface language (articles will be in original language) and the other is the translation of articles. Original sNews 1.6 does not allow any kind of online translation (with flag links), so this is the reason because I started to make changes. And once the interface translation was working I decided to implement a simple mechanism to translate articles. I think the main advantage is maintenance. Thus, if you have a complex index.php and make changes, you dont need to repeat changes for every language...

Another issue is external article links. Imagine you enter the site in english (by default) and then you change to spanish. You search for an article and copy the link. Another day you paste de link in the browser, but the page is not found, because the initial language of your site is english. I think this problem is present also in your multi-language solution... So it is necessary to insert the language in URLs.
Logged

Joost

  • Guest
Re: [MOD] Multi-language simple Mod
« Reply #8 on: December 07, 2007, 05:59:26 PM »

@Patric,

Not a dumb question, it is a fundamental question. With just a little knowledge and creativity, we can do anything at the frontside. I have, for example, a multi language sNews site in production (English, German and Dutch). It uses one snews.php,  3 index.php and a database structure, similar to this one (3 times 4 prefixed tables). A major drawback is having to log in separately in all thee languages and a image uploaddirectory, that is only accessible through one admin panel. I might has well used three separate installs and have the same result.
As I mentioned earlier, I have a ML-version in development, not fit for publishing, since the backend is more crippled than the solution I used.
The bottom-line is not what we can do for the visitor, but how to make live easier for the administrator.
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
Re: [MOD] Multi-language simple Mod
« Reply #9 on: December 07, 2007, 08:36:51 PM »

@etatus : If You are seriously at this then few more things; like differences in my implementation from what I read here:

I use following setup:
 - single codebase and directory structure
 - single settings table
 - prefix differentiated content data tables for any language

In single settings table additional field is added to determine for which language (like EN or ES) or ANY (for all of them) particular parameter record is used. Quick review of basic settings will quickly help to determine which ones are site-wide and which ones are language specific. Little exercise in basic SQL is all it takes to finish settings.

As for content data tables ... well, IMHO treating one special language differently causes more problems than gives benefits.

The rest is, probably, irrelevant details:
 - full db[] parameters set for each language (in case if I will want to separate them more than just by prefix) in separate files
 - few adjustments at code responsible for starting up engine and settings administration form to accommodate language specifics.
 
« Last Edit: December 07, 2007, 08:48:38 PM by codetwist »
Logged

etatus

  • Newbie
  • *
  • Karma: 2
  • Posts: 8
Re: [MOD] Multi-language simple Mod
« Reply #10 on: December 09, 2007, 05:52:48 PM »

Hi, I'm still testing, but for now this solution (better than the previous) works fine for me (including the problem with external links):

  • STEP 0: Backup of snews.php, index.php and .htaccess

  • 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['nolangwebsite'] = $db['website']; //eTaTuS// needed for title()
       $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 if ($variable=='website') {
          $lang=session_lang();
          return $db['website'].$lang.'/'; // Website URL with language    
       }else return $db[$variable];

    }

    //eTaTuS// SESSION LANGUAGE
    function session_lang() {
       if (isset($_POST['sessionlang'])) $_SESSION['lang'] = $_POST['sessionlang'];
       else 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
           ...

    // TITLE
    function title() {
       echo '<base href="'.db('nolangwebsite').'" />'; //eTaTuS// Header urls without language
       $categorySEF = get_id('category'); $articleSEF = get_id('article');
       $categoryName = retrieve('name', 'categories', 'seftitle', $categorySEF);
       $articleTitle = retrieve('title', 'articles', 'seftitle', $articleSEF);
           ...

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

  • 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

  • STEP 4: Use this set of RewriteCond and RewriteRule in your .htaccess (thanks to codetwist):
    Quote
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-z_/\-]+)$ index.php?category=$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([A-Z][A-Z])/index.php?(.*)$ $2 [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([A-Z][A-Z])$ index.php?sessionlang=$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([A-Z][A-Z])/$ index.php?sessionlang=$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([A-Z][A-Z])/(.*)$ index.php?sessionlang=$1&category=$2 [L]

NOTICE:
  • The language set on administrator panel is useless now. It will be used the session language set in STEP 1 instead.
  • URLs are now something like this: http://website.com/EN/path-to-resource
  • You need to upload your flag images and set their path in flag links
  • This solution is quite simple and keeps independent tables for each language (fine for my purposes)

Good luck!  ;)
« Last Edit: December 09, 2007, 06:26:59 PM by etatus »
Logged

Joost

  • Guest
Re: [MOD] Multi-language simple Mod
« Reply #11 on: December 09, 2007, 06:22:55 PM »

Well done etatus. With a little help ;) from Codetwist. you've made a major breakthrough.
Also concerning this mod Multiple installations using one sNews engine and one database
I've used three index.phps in three directories there, to switch prefixes. Your method only suggests there are three directories. It will be very useful there. I'll keep a close watch on development here, keep us posted.
Karma for both.
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
Re: [MOD] Multi-language simple Mod
« Reply #12 on: December 09, 2007, 06:45:57 PM »

Congratulations, etatus ;)
Logged

Pali

  • Full Member
  • ***
  • Karma: 16
  • Posts: 111
    • LE-PAVON
Re: [MOD] Multi-language simple Mod
« Reply #13 on: December 09, 2007, 11:31:25 PM »

guys, i have Lang. versions ready... working example: www.metaltradenitra.sk

Just one db, just one index, just one snews.php

there is hacked htaccess, so site/en/category/article/ give english version and site/de/category/article/ give german version.

Database structure same like original snews (i used explode/implode functions).

Dont have installation instructions, because there is included Luka's gallery (with image description) and too many other hacks. If somebody interested i can upload my full working version and you can play (only for skilled people - too many hacks).

Pali
Logged
I like sNews!

Joost

  • Guest
Re: [MOD] Multi-language simple Mod
« Reply #14 on: December 10, 2007, 12:50:32 AM »

Hello Pali,

I would like to play. :)
It looks interesting, especially the language switch.
Logged
Pages: [1] 2 3 4