Jump to content

Multi-language website function


jetsettt

Recommended Posts

Hi,

 

I am trying to build a multi-language function for my website. I have looked at many tutorials and all seem to suggest that language files are used. However, I wish to have easier administration of the language entries and storing these in a DB would be much better for myself.

 

I have 2 tables. This is my schema.

 

'Lang' table

'Lang_id'

'Lang_name'    (eg  English)

 

'Lang_entries' table

'Lang_entry_id'

'Lang_entry_data'      (eg.  'Hello! This is English text')

'Lang_entry_code'      (eg.  AA0001)   

'Lang_id'                      (eg.  '1')

 

 

Not sure if this is the best way, but I intend to query the 'Lang_entries' table by 'Lang_id' for example and get all of the 'English' entries so that they can be PHP echoed in various places on the page by 'Lang_entry_code'

 

The problem that I am having is that I can only echo the first result from the query.

 

Is it possible to echo any of the  'Lang_entry_data'  by a  'Lang_entry_code'  ?

 

Thankyou in advance if anyone can advise. Not sure how to go about this.

Link to comment
Share on other sites

I have come up with a solution that works but I am unsure if it good practice.

 

To display a language entry from the DB anywhere on the page, I run a DB query such as ...

 

<?php
$query_language = "SELECT lang_entry_data, lang_entry_code FROM lang_entries WHERE lang_entry_code = 'AA0005'";
$language = mysql_query($query_language, $db) or die(mysql_error());
$row_language = mysql_fetch_assoc($language);
  echo $row_language['lang_entry_data']; 
?>

 

 

It seems to work ok without having to load the whole of the language entries into the page for use.

 

This will result in many DB quiries per page, is this bad practice ?

Link to comment
Share on other sites

A lot of queries for a single page would be quite bad for performance and scalability.

 

You can load all the language strings into an assoc array and use that.

 

Something like this (Untested code):

<?php

$query_language = "SELECT lang_entry_data, lang_entry_code
FROM lang_entries
WHERE lang_id = '1'";

$language = mysql_query($query_language, $db) or die(mysql_error());

$lang = array();
while($row = mysql_fetch_assoc($language))
$lang[$row_language['lang_entry_data']] = $row_language['lang_entry_data'];


echo $lang['AA0005']; // output the lang string for AA0005

?>

Link to comment
Share on other sites

Thanks analog,

 

Just what I need. This seems a really good way to add multi-language to a web site. Storing the data in a DB rather than files is much better for me.

 

I had to make a small change to the code to get it to work so that the correct  language entry data could be called by it's ID. All I have to do now to show a language entry anywhere on a page is call it by its ID!  Thanks again analog.  :D

 

$lang[$row_language['lang_entry_data']] = $row_language['lang_entry_data'];

 

Altered to.

 

$lang[$row_language['lang_entry_code']] = $row_language['lang_entry_data'];

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.