Jump to content

Improve my multilanguage code


Lykos22

Recommended Posts

Hi, I have created a website in which I have added the ability the user to choose a language. The structure of my site looks like in the image. Inside core folder, there's the initialize.php file where I have put this:

$allowed_languages = array('fre', 'eng');if(isset($_GET['lang']) === true && in_array($_GET['lang'], $allowed_languages) === true){$_SESSION['lang'] = $_GET['lang'];}elseif(isset($_SESSION['lang']) === false){//set default language if not selected$_SESSION['lang'] = 'eng';}include_once("includes/langs/".$_SESSION['lang'].".php");

and inside the langs folder there are both eng.php and fre.php so the code inside them looks like this:

//eng.php$lang = array();// Menu - Nav$lang['HOME'] = 'Home';$lang['ABOUT'] = 'About';$lang['CONTACT'] = 'Contact';// Index page$lang['WELCOME'] = 'Welcome to my website!';//About page$lang['ABOUT_PAGE'] = 'This is the about page.';

So in navigation I do: <a href="index.php"><?php echo $lang['HOME']; ?></a>,in index.php for example I do: <h2><?php echo $lang['WELCOME']; ?></h2>, etc etc. My question is if there's a page, for example the about.php, in which I have a lot of content like headings, paragraphs, lists etc etc which is the best way to output it on the page??Do I have to do it all like this

$lang['PARAGRAPH_1'] = 'This is the first paragraph.';$lang['PARAGRAPH_2'] = 'This is another paragraph.';$lang['LISTITEM_1'] = 'This is the first list item.';.....<h2><?php echo $lang['ABOUT_PAGE'] ;  ?></h2><p><?php echo $lang['PARAGRAPH_1'] ;  ?></p><p><?php echo $lang['PARAGRAPH_2'] ;  ?></p>

or in pages like this there's a "better" way to approach this???

post-60268-0-31513700-1363084702_thumb.jpg

Link to comment
Share on other sites

That seems very complicated. I will assume there is a maintenance advantage to keeping the English text and the French text in the same document. A strategy I would try is to embed the English version AND the French version of each text item in the HTML itself. Then assign a different class to each version. Something like this:

<h1 class="english">Welcome!</h1><h1 class="french">Bienvenu!</h1>

Your CSS would look like this:

.english {   /* stuff */   <?php if ($_SESSION['lang'] == "fre") echo "display: none;"; ?>}.french {   /* stuff */   <?php if ($_SESSION['lang'] == "eng") echo "display: none;"; ?>}

Yes, you would output a lot of text, but compared to an image, text requires very little bandwidth. You would also need to embed very little PHP in the HTML.

Edited by Deirdre's Dad
Link to comment
Share on other sites

You can store the component in XML structure which will represents the data of the web page. In html page you will only have just templates of html structure. you can use simplexml to load the xml file and inject them inside the html content. you can easily switch between different XML file. In that way you only need to keep track of XML file to add new languages. There is also a way to use XSLT (server side or client side) to transform xml file directly to HTML but that would require knoledge of XSLT.

Link to comment
Share on other sites

You can store the component in XML structure which will represents the data of the web page. In html page you will only have just templates of html structure. you can use simplexml to load the xml file and inject them inside the html content. you can easily switch between different XML file. In that way you only need to keep track of XML file to add new languages. There is also a way to use XSLT (server side or client side) to transform xml file directly to HTML but that would require knoledge of XSLT.
I don't know XML to be honest :sorry: , so I'm not quite sure how to do this..
Link to comment
Share on other sites

You can start it from the tutorial. XML is not complicated at all. xml is just markup structure. The important thing you need to see is the API to parse that file http://php.net/simplexml. Start out with the xml and make the structure of your site in xml. once you have the xml move on to next part, parsing.

Link to comment
Share on other sites

I don't understand how its going to be. Could you give me a simple example or what terms can I use to google for some examples?? Also what if I do this: 1. create two html pages inside includes folder, one named about_eng.php and the other about_fre.php, where each page has all the html content in the specific language2. inside my about.php (root dir) do this:

if(isset($_SESSION['lang']) === true){  $pages = scandir('includes', 0);  unset($pages[0], $pages[1]);  $pg_lang = urlencode($_SESSION['lang']);   //check if the value of pages exist in the array  if(in_array('about_'.$pg_lang.'.php', $pages) === true){   include('includes/about_'.$pg_lang.'.php');  }}else{  //set default page content  include("includes/about_eng.php");}

Edited by Lykos22
Link to comment
Share on other sites

Don't use CSS.Don't use XML unless you're caching your parsed pages. (It's slowww) The method you're using is just fine! Keep in mind that you can use HTML in your text bits. So you can have multiple paragraphs in one variable. If you have a lot of text on different pages, split it into different files.

Link to comment
Share on other sites

Ok, I'd like to expand the topic a little bit more and refer to the case that I have some content on a page that comes from the database. which is the best way to structure the database in that case???For example I have also a page that outputs albums with images with some descriptions (also apply and in posts/ articles/ pages etc etc) and the db table I have created looks like this:

(table name:)albums: (fields) album_id, title_eng, title_fre, description_eng, description_fre, ...

Is this a correct approach? From a search I did in google I saw 2 different: 1st. Just add another field named 'language' where gets from a select menu the language (eng, fre) like this:

(table name:)albums: (fields) album_id, title, description, language

2nd. Like this with the tick. Which approach is better???

Link to comment
Share on other sites

It's more flexible if the language is a field, then you don't have to change the table structure to add or remove languages.
So I'll have to go with this approach
1st. Just add another field named 'language' where gets from a select menu the language (eng, fre) like this: (table name:)albums: (fields) album_id, title, description, language
, right?? In this case the forms for INSERT and UPDATE statements will look something like this:
<form action="" method="post"><label for="title">Title:</label><input type="text" name="title" id="title" /> <label for="description">Description:</label><textarea name="description"></textarea> <label for="lang">Language:</label><select><option value="eng">English</option><option value="fre">French</option></select></form>

or do I have to create 2 input text and textareas for each language??

Edited by Lykos22
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...