multilanguage site in PHP

Posted on 2007-10-17
Last Modified: 2012-12-18
We currently have a web site written in (simple) PHP.

We would need to add the multilanguage feature,
that is, that the user can select in which language to view the website.

What would be the best way to implement this?
without changing too much our current PHP code.

Many thanks,
Question by:dportabella
    LVL 21

    Expert Comment

    No need to rewrite too much, but need to translate all your text into a foreign language, and store them say in a database table (each language has different table). Upon visitor visiting your site, default the site to english and let them choose language, if they chose another language, replace all the text you have translated (by retrieving foreign text from the database) and output them to the appropriate places to replace the english text.

    It is a smart idea to have a table for english as well (your default language). Then when the user visits, retrieve the english values from the database. Make all your title, menutext, browser heading, etc.... as variables that are replaceable in terms of which language you have to retrieve from the database.

    Hope this is an understandable explanation.
    LVL 3

    Expert Comment

    If the site isn' t that big, you could also use a language file,i.e. english.php, german.php, each one containing the text (in each language) you want in variables. In your code you just have to use the proper variables, depending in the user's language selection. I would prefer using XML to do that, but it might require a bit more programming work...


    Author Comment


    Thanks for the comments.
    I guess that we are not the only ones with that need,
    and we would prefer to use a standard approach.

    We would prefer not using a sql database.
    Defining variables (in separate php files or xml files) would be nice, but still a little cumbersome.

    We would really like to have everything (the php code + the text in the different languages) all together in the same php files.

    Do you know of full simple concrete implementation examples that we can look at?

    Many thanks,
    LVL 3

    Accepted Solution

    Ok, let's do it using .php files.

    Let's suppose we need 2 languages, english and french (en,fr). We create 2 different subfolders in the site, i.e. /en and /fr.
    In there we have a file (translation.php) which will contain the translations. The contents of this file will look like the following:

    $_text_1 = 'text_1';
    $_text_2 = 'text_2';
    $_text_3 = 'text_3';

    $_text_1 = 'texte_1';
    $_text_2 = 'texte_2';
    $_text_3 = 'texte_3';

    Ok, now we have everything translated in the 2 languages. Notice that we use the same variable names in both files, so that we can easily access the wanted variable.

    Now suppose that we have 2 icons, each one used for the language selection. When a user clicks any of them you POST/GET a variable and reload the page. The variable you POST/GET will contain either 'en', or 'fr', so that you be able to know in which path to look for the text...I think you now get the point ;)

    In each .php page, you will have the following code:

    $lang  = $_GET['lang'];

    $path = "./". $lang ."/translation.php";

    if ( is_file($path) ) {
      include $path;
    Now you include the selected file (/en/translation.php OR /fr/translation.php) and you can use the same variables, but the text is in the language the user has chosen....

    Easy enough, isn't it?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now