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.

<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 href="?lang=en">
                        <input type="hidden" name="lang" value="lang_en" />
                        <img src="images/english.png" alt="EN" width="16" height="16">
<?php echo WELCOME; ?></p>

define('WELCOME','Welcome to your business Expenses software');
define('ENTER_EMAIL','Enter your Email');
define('ENTER_PASS','Enter your Password');

define('WELCOME','Bienvenue a votre entreprise des depenses de logiciels');
define('ENTER_EMAIL','Entrez votre email');
define('ENTER_PASS','Entrez votre mot de passe');

Recommended Answers

All 12 Replies

Member Avatar

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

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):




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:

$defaultLang = 'fr';
$page = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
if(!isset($_GET['lang']) || !in_array($_GET['lang'], array('fr','en'))){
        $reloadLang = $_SESSION['lang'];
        $reloadLang = $_COOKIE['lang'];
        $reloadLang = $defaultLang;
    //RELOAD to a VALID url
    header("Location: http://www.example.com/$reloadLang/$page/");
    $lang = $_GET['lang'];
    //you could set a cookie here too if req'd
    $_SESSION['lang'] = $lang;

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

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


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)

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

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.

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

Member Avatar

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?

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

Thanks in adv.

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

Member Avatar

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

@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

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:


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.


Each lang file would have this type of structure:

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.