1,105,197 Community Members

Multi Language Site PHP

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

I want to do in French (By default) & seconadry option is English.
I have a two image flag on login.php page. It should ALWAYS visible in the site. I created two files lang_FR.php and lang_EN.php. How one set French as default language and translate it according user choice ? Any help by correction or example would be great.

//login.php
<div id="imageflag">
                    <a href="?lang=fr">
                        <input type="hidden" name="lang" value="lang_fr" />
                        <img src="images/france.png" alt="FR" width="16" height="16">
                    </a>&nbsp;
                    <a href="?lang=en">
                        <input type="hidden" name="lang" value="lang_en" />
                        <img src="images/english.png" alt="EN" width="16" height="16">
                    </a>
                </div>
<?php echo WELCOME; ?></p>

//lang_EN.php
<?php
define('WELCOME','Welcome to your business Expenses software');
define('LOGIN','Login');
define('ENTER_EMAIL','Enter your Email');
define('ENTER_PASS','Enter your Password');
?>

//lang_FR.php
<?php
define('WELCOME','Bienvenue a votre entreprise des depenses de logiciels');
define('LOGIN','Login');
define('ENTER_EMAIL','Entrez votre email');
define('ENTER_PASS','Entrez votre mot de passe');
?>
LastMitch
Deleted Member
 
0
 

I have a two image flag on login.php page. It should ALWAYS visible in the site.

What do you mean visible? It should be visible if you put html code like what you did on your code.

I assume these are when you put lang_FR.php and lang_EN.php respectfully:

<a href="?lang=fr">

<a href="?lang=en">

Any help by correction or example would be great.

I don't know what you are trying to do?

I mean you have <input> tags are hidden?

You really didn't explain what you are trying to do.

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
2
 

OK, I used to use php arrays for this until I started messing with GETTEXT. This means using .po files - MUCH easier than keeping arrays in synch.

Here's some recent blog posts I made on it (step by step):

http://diafol.blogspot.co.uk/2013/01/php-localization-with-gettext-on-windows.html

http://diafol.blogspot.co.uk/2013/01/creating-pot-files-in-poedit-for.html

http://diafol.blogspot.co.uk/2013/01/supporting-xgettext-on-windows-from.html

However, if you're not keen on that approach, constants can be a bit awkward, you're probably better off with arrays, but the idea's the same.

WRT flags - it's pretty, but it can be annoying. If I choose my default language, I wouln't want somebody else's flag up there to click. For example, who's flag do you give for English? French would be Canada? Or what about Canadian English? What about Switzerland? I suppose you can argue that your country codes *_FR or *_GB can justify their inclusion, but IMO, they're not worth it.

Also, you should always have a querystring (www.example.com?lang=fr) or better still a mod-rewrite for that: (www.example.com/fr/). This means that your SEO isn't confused as each url landing page should be unique. So avoid just keeping state with a session variable.

The www.example.com/fr/ does NOT mean subdirectories for every language - just in case you're not familiar with rewrites.

So if you want freanch as your default, you could do something like this:

<?php
session_start();
$defaultLang = 'fr';
$page = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
//NO VALID LANG FROM URL
if(!isset($_GET['lang']) || !in_array($_GET['lang'], array('fr','en'))){
    if(isset($_SESSION['lang'])){
        $reloadLang = $_SESSION['lang'];
    }elseif(isset($_COOKIE['lang'])){
        $reloadLang = $_COOKIE['lang'];
    }else{
        $reloadLang = $defaultLang;
    }
    //RELOAD to a VALID url
    header("Location: http://www.example.com/$reloadLang/$page/");
    exit;
//VALID LANG FROM URL    
}else{
    $lang = $_GET['lang'];
    //you could set a cookie here too if req'd
    $_SESSION['lang'] = $lang;
}
//LOAD THE CORRECT LANGUAGE FILE
include("lang/$lang.php");

That should load all your strings for the page or site.

<div id="langs">
    <a href="/fr/<?php echo $page;?>/">FR</a>&nbsp;|&nbsp;<a href="/en/<?php echo $page;?>/">EN</a>&nbsp;<?php echo WELCOME;?>
</div>

You could apply a class for the active link above too, so that it stands out, or even make it static text so that it can't be clicked (as it doesn't make sense to click the active language).

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

@LastMitch

I have a two image flag on login.php page. It should ALWAYS visible in the site.

I would like to keep both image flag also AFTER selecting the language.

In short, i am trying to do based on this tables

**Table "langues"**
id_lang     libelle     sql_legende
1           Francais        FR
2           Anglais         EN

**Table "textes"**
Field   Type    
id_text     int(10)              
ref_lang    int(10)     
libelle     varchar(255)
sql_legende     varchar(255)

@diafol
It sounds good. Can you help me to do with such tables ?

PLEASE let me know how one can use these tables to do it in two languages.

I just need one example in php file. For example on login.php.

Thanks a lot in advanced.

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

You can do this with a db, but it may be even harder to keep lang files in synch with this.

You'd need separate tables for each lang as you wouldn't want to be messing with your main table just to add another lang.

This is probably the last method I'd recommend.

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

In fact i need to follow that table only. Please let me know by example. Thanks in advanced.

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

In fact i need to follow that table only. Please let me know by example. Thanks in advanced.

To be honest, this is a bad idea IMO and therefore I won't offer an example. Maybe somebody else can see the benefits of this over other methods?

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

Anyone have idea to do the site in two language by that table ? I need urgent help.

Thanks in adv.

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

How to do site in two languages in PHP? Any input would be great.

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
1
 

Are you still after a DB solution or have you now thought about a gettext or an array-based approach?

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

@diafol: ANY solution that will take less time and give the same result. I would like something that can be easy to manage in future also. (Database & Website Point of view)

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
2
 

OK, you have 3 main options for a DIY site.

1) GETTEXT with .po files (see my links in a previous post)
2) php array files (or php CONSTANTS)
3) DB storage

If you are going to be the sole translator, you could also write your own language templating system - but that may be a step too far. But if you only have a few pages it may be easier:

includes
    fr
        index.php
        about.php
    en
        index.php
        about.php

These files could be duplicates holding just plain html.

Anyway, each has its pros and cons. The fastest by far is the GETTEXT system, but it can be fiddly to set up initially. Also, caching can be a problem with updating the .po files, but there's a workaround (see links). Another popular system for many open source projects is php arrays.

langs
    fr.php
    en.php

Each lang file would have this type of structure:

$lang = array(
    'welcome' => "Welcome to my amazing site",
    'about' => "About Us",
     ...etc...
);

So in your main 'view' pages, you'd have soemthing like:

<p><?php echo $lang['welcome'];?></p>

Your DB idea is similar, but far more complicated as you need to retrieve all the relevant strings into an array for the page before producing code like the above. You could have all languages in a single table or set up a new table for each language. This can be awkward, but not impossible for situations where you outsource your translations to others.

$query = "SELECT key, lang FROM table WHERE page = 'about'";

where the 'table' could be a variable for a language code if you have separate tables, or it would be 'lang' for a language variable if you have everything in a single table.

So for a single table setup:

id | key | page | en | fr

The problem with this is that the source and the template are 'very separated', so to check the key for a particular string would mean having to have a view of the DB.

OK those are a few thoughts off the top of my head.

Member Avatar
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

There's a Pear class here; http://pear.php.net/package/Translation2 for DB i18n

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: