We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,586 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Multi Language Site PHP

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');
?>
3
Contributors
7
Replies
1 Week
Discussion Span
3 Months Ago
Last Updated
46
Views
PriteshP23
Junior Poster
113 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47

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

diafol
Keep Smiling
Moderator
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61

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

PriteshP23
Junior Poster
113 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

diafol
Keep Smiling
Moderator
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61

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

PriteshP23
Junior Poster
113 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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?

diafol
Keep Smiling
Moderator
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61

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

Thanks in adv.

PriteshP23
Junior Poster
113 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0771 seconds using 2.69MB