Jump to content

Change Web page language


man12_patil3

Recommended Posts

There are many ways to make a site multi-lingual, and the first step is to have the required text for the site in all the languages you are going to offer.

 

You then need to decide if you are going to use seperate pages for different languages, or store the text alternatives in a database or text file and have the code switch which version to use based on the user's language choice.

Link to comment
Share on other sites

You make a file for each language you need, words and phrases, sentences can be as a DEFINE. Blurbs and longer rants should be as a var.

you then do this

<?php

if (!isset($_SESSION['language'])

{

$language='en';

$_SESSION['language']=$language;

}

include("/includes/language/$language.php");

?>

<body>

<?php

echo GREETING;

echo "<p>$blurb_1";

?>

 

 

 

en.php

<?php

define("GREETING", "Welcome");

$blurb_1='Now is the time for all good men to go bad and carry a big stick. This

stick should be liberally used in defense of all slights, real or perceived, large or small, or

for any reason deemed necessary.'

?>

 

fr.php

<?php

define("GREETING", "Bonjour");

$blurb_1='Le Now est l'heure pour que tous les bons hommes aillent le mauvais et de portent un grand bâton. Ceci le bâton devrait être libéralement utilisé à la défense de tout slights, vrai ou perçu, grand ou petit, ou pour n'importe quelle raison considérée nécessaire.';

?>

 

gr.php

<?php

define("GREETING", "Guten Tag");

$blurb_1='Now ist die Zeit, damit alle guten Männer Schlechtes gehen und einen großen Stock tragen. Dieses allen Stock sollte liberal benutzt werden zum Schutze von slights, real oder empfunden worden, groß oder klein, oder aus jedem möglichem Grund gehalten notwendig.';

?>

 

The standard control is a flag to click and session var, future persistence can be DB/login or cookie or whatever other magic you concoct, or nothing as the flag is usually just fine.

Unless you can do your own translations this is only suitable for apps as sites with lots of verbiage, especially ones that change/update often are a royal PITA.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Don't use placeholders like oni-kun suggested and don't use PHP files with a language array like teamatomic suggested. Sorry no offense intended.

 

Why no placeholders? Because if no translation is available it will show up (eg TEXT_MENU_HEAD) which is very ugly

Why no PHP file with a language array? Because as your website grows you will be maintaining multiple sources (ie PHP files + multiple database tables: blog posts, articles, pages, ..)

 

So how should you do it? Well there isn't any correct way of doing it only easier ways like for example 1 database table that will hold are your original text and it's translation into wathever language the user chose for example:

 

CREATE TABLE language (
  id tinyint NOT NULL auto_increment, -- 127 languages
  name varchar(32),
  name_translated varchar(32),
  code char(2),
  KEY language_code (code),
  PRIMARY KEY (id));

CREATE TABLE translation (
  id integer NOT NULL auto_increment,
  language_id tinyint,
  message text,
  message_translated text,
  KEY translation_language_id (language_id),
  KEY translation_message (message),
  PRIMARY KEY (id));

INSERT INTO language (id, name, name_translated) VALUES (1, 'Dutch', 'Nederlands');

INSERT INTO translation (id, language_id, message, message_translated) VALUES (1, 1, 'Homepage', 'Hoofdpagina');
INSERT INTO translation (id, language_id, message, message_translated) VALUES (2, 1, 'About us', 'Over ons');
INSERT INTO translation (id, language_id, message, message_translated) VALUES (3, 1, 'Written by %s on %s', 'Geschreven door %s op %s');
INSERT INTO translation (id, language_id, message, message_translated) VALUES (4, 1, 'Opinion: to die() or not?', 'Opinie: die() wel of niet gebruiken?');

 

You now have one source that holds translations for everything in your website menu, pages, .. In your application use it like so:

 

<li><a href="#"><?php print translate('Homepage'); ?></a></li>
<li><a href="#"><?php print translate('About us'); ?></a></li>

 

If no translation is available they see Homepage and About us not TEXT_MENU_HOME and TEXT_MENU_ABOUT. They also don't see "Undefined index '..'" when a language file goes missing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.