Jump to content

multilingual website


thosecars82

Recommended Posts

hello guys

 

I want to develop a multilingual website with php. I do not have many ideas about how to do a site multilingual. I have been looking for some information but what I have got until now seems a bit confusing to me.

 

      The most obvious solution is having one static page for each language. Besides I have seen something about using a table in the database to extract the translations to the corresponding language. However, I have seen that it might take too long for people to see a page because the query might delay the process.

 

 

I do not know if this option is the best. Could you please provide me with some good and brilliant approach to solve this issue in any website of any size and with any number of different languages?

 

 

Thanks in advance

Link to comment
Share on other sites

I thought about that too.

static pages are a pain to maintain.

db pages require extra time to display.

 

 

Best Solution: Caching

use a db solution with a cache feature, that adds the language to the name.

 

 

Hello again,

Thanks. This solution of using a db with a cache feature seems interesting. Do you have any idea about how to set this cache feature with mysql? I am using mysql, apache and php. Is this feature activated by default? Actually, I am quite lost.

Link to comment
Share on other sites

I do multilingual websites, and I have tried the static approach, and I am currently using the database approach. When it comes down to it, you can get the whole text for one page in like thousandths of a second, and its best to do it in one query, so getting it from the database isn't exactly slowing things down. If you are making sites with heavy traffic it may be a problem, but if its just a smaller site, no worries.

 

That being said, I'm thinking of switching to a flatfile system - keeping all the page text in external documents (I'm thinking XML). This will remove the necessity for the database query, and almost be definitely easier to keep up to date than putting text in the database. Its a huge pain in the ass to always be creating database columns and new rows for text for different pages. Editing a file seems like it will be a much more efficient system.

 

However I haven't tried it yet, so I can't say for sure that its a better system. But I can say that the database system works well, is quite quick, and much better than static pages.

Link to comment
Share on other sites

I do multilingual websites, and I have tried the static approach, and I am currently using the database approach. When it comes down to it, you can get the whole text for one page in like thousandths of a second, and its best to do it in one query, so getting it from the database isn't exactly slowing things down. If you are making sites with heavy traffic it may be a problem, but if its just a smaller site, no worries.

 

That being said, I'm thinking of switching to a flatfile system - keeping all the page text in external documents (I'm thinking XML). This will remove the necessity for the database query, and almost be definitely easier to keep up to date than putting text in the database. Its a huge pain in the ass to always be creating database columns and new rows for text for different pages. Editing a file seems like it will be a much more efficient system.

 

However I haven't tried it yet, so I can't say for sure that its a better system. But I can say that the database system works well, is quite quick, and much better than static pages.

 

Thanks for that information. It's useful. I'll think about this.

Link to comment
Share on other sites

I do multilingual websites, and I have tried the static approach, and I am currently using the database approach. When it comes down to it, you can get the whole text for one page in like thousandths of a second, and its best to do it in one query, so getting it from the database isn't exactly slowing things down. If you are making sites with heavy traffic it may be a problem, but if its just a smaller site, no worries.

 

That being said, I'm thinking of switching to a flatfile system - keeping all the page text in external documents (I'm thinking XML). This will remove the necessity for the database query, and almost be definitely easier to keep up to date than putting text in the database. Its a huge pain in the ass to always be creating database columns and new rows for text for different pages. Editing a file seems like it will be a much more efficient system.

 

However I haven't tried it yet, so I can't say for sure that its a better system. But I can say that the database system works well, is quite quick, and much better than static pages.

Hello again

as for the database approach, I have a question. It just came to me this idea:

using only one table with 2 fields like this:

language, sentence

 

lang1, sent1

lang1, sent2

....

lang1, sentm

lang2, sent1

lang2, sent2

...

lang2, sentm

langn, sent1

langn, sent2

...

langn, sentm

 

where each of the m sentences from sent1 to sentm is the translation to each language.

Each sentence would be part of the content of our whole website.

 

Please, I would appreciate that you told me if you have a better database design because this is the only idea I have for now for the database design. An in case this approach is the one you were thinking of, I just want you to tell me so. Hopefully, you might have some better database design than mine.

Link to comment
Share on other sites

The way you have described wouldn't work in that you would have no way of knowing what text matches what area. You would need a third colum that would be a unique identifier as to what the text in the column is.

 

I use a different system:

 

language   pagename   text1           text2   text3  
jp             index.php    konnichiwa   baka    ja-ne
en            index.php    hello           moron   see ya

 

Then when I need text for a page, I use something like this:

 

$language = (isset($_COOKIE['language'])) ? $_COOKIE['language'] : 'jp'; // this code reads 'if the cookie is set, then $language = the cookie, if not, then $language = japanese
$page_text = mysql_query("SELECT text1, text2, text3 FROM page_text WHERE language='{$language}' AND page='index.php' LIMIT 1");
$page_text = mysql_fetch_array($page_text);

 

With this, the entire text for the page is set into the $page_text array, and I can call it through the page like this:

 

echo "<p>{$page_text['text1']}</p>";

 

If the cookie on the page is either set to Japanese, or not set at all, then the output of that code would be:

 

<p >konnichiwa< /p> (although you wouldn't see the p tags in the browser)

 

and if the cookie is set to English, you would see:

 

<p >hello< /p>

Link to comment
Share on other sites

Yes, thats the type of system I developed initially as well. but u also shud add in a string identifier as well

as integer lookups will be a lot faster

 

TABLE LangStrings

  lang integer  /language identifier

  strid integer /string identifier

  string text /the actual text

 

with lang and string being a multi-primary key. This can be used in yer editor to find a specific string id, and return all the language results.  so u can use the same string across pages.

 

The cache function isnt hard to make, with ob_start/ob_get_contents/ob_end_clean functions.

 

all u do is check the existance of the file first, if it exists display the content, otherwise generate the content and save the output

 

if(file_exists($language."_"$page.".html"))
   $content=file_get_contents($language."_"$page.".html");
else {
  ob_start();
  // Display page code here
  $content=ob_get_contents();
  ob_end_clean();
  file_put_contents($language."_".$page.".html");
}
echo $content;

 

 

 

 

 

Link to comment
Share on other sites

Yes, thats the type of system I developed initially as well. but u also shud add in a string identifier as well

as integer lookups will be a lot faster

 

TABLE LangStrings

   lang integer  /language identifier

   strid integer /string identifier

   string text /the actual text

 

with lang and string being a multi-primary key. This can be used in yer editor to find a specific string id, and return all the language results.  so u can use the same string across pages.

 

The cache function isnt hard to make, with ob_start/ob_get_contents/ob_end_clean functions.

 

all u do is check the existance of the file first, if it exists display the content, otherwise generate the content and save the output

 

if(file_exists($language."_"$page.".html"))
   $content=file_get_contents($language."_"$page.".html");
else {
  ob_start();
  // Display page code here
  $content=ob_get_contents();
  ob_end_clean();
  file_put_contents($language."_".$page.".html");
}
echo $content;

 

 

Thank both of you for so brilliant ideas and excellent explanations. I think I am going to try this.

 

Link to comment
Share on other sites

One more question:

 

I am thinking what variable I am going to use to store the language currently selected by the user and I have come with this idea:

            I will declare a java script global variablet.

            Then I will check the language from that variable to be able to look up in the table the corresponding translation.

          In case the user tries to change the language, I will change the value of the javascript global variable.

 

I would appreciate your opinions about this approach. Do you use global variables in javascript to store the language selected by the user and any other needed variables customized for each person who visits the website? I guess you might have other solutions. I am willing to hear about your suggestions.

Thanks a lot in advance.

 

Link to comment
Share on other sites

$language = (isset($_COOKIE['language'])) ? $_COOKIE['language'] : 'jp'; // this code reads 'if the cookie is set, then $language = the cookie, if not, then $language = japanese
$page_text = mysql_query("SELECT text1, text2, text3 FROM page_text WHERE language='{$language}' AND page='index.php' LIMIT 1");
$page_text = mysql_fetch_array($page_text);

I hope you're sanitising $_COOKIE['language']!
Link to comment
Share on other sites

I don't use javascript at all for the language check- if a user has it turned off your site falls a part.

 

I use php cookies. I store a cookie called 'language' on the users machine. At the top of each script, I check the variable set in that cookie, and pull the applicable text out of the database. That is what I was doing with this line of code:

 

$language = (isset($_COOKIE['language'])) ? $_COOKIE['language'] : 'jp'; // this code reads 'if the cookie is set, then $language = the cookie, if not, then $language = japanese

 

It does just what I said - it looks for the cookie, if its there, it uses the language in the cookie. If its not, it uses the default language. I have chosen my default language as Japanese, as the huge majority of my users are Japanese, but that will be different for each customer base. But if you don't choose a default language and require the cookie to be set first, you will lose out on SEO rankings with the search engines, because they don't use cookies.

 

At the bottom of each page I have a link that has the language I'm not using in it. When the page is in English, the link says 'Japanese' (in Japanese). When the page is in Japanese, the link says 'English' (in English!). The link address is to a script that changes the value of the cookie. I also attach a get variable to the link with the url-encoded address of the page I'm viewing. Then on that script, I check the language of the cookie I am currently using and set the cookie as the opposite language. I then use the GET variable to find out what page I was on, and I redirect back to that page. In this way the person clicks the link on the bottom, and the cookie is switched automatically, displaying the same page in the opposite language.

 

I would strongly discourage javascript for this situation. Search engines don't use it, and some people have it turned off.

Link to comment
Share on other sites

Conker - thanks for the concern, but yes, I clean it. I just wrote down the base code for the guy off my head - I didn't feel the need to complicate it with other code that while necessary, isn't part of the core process I was explaining.

Link to comment
Share on other sites

Haku's method of storing language into a cookie, is great for a site that allows everyone to browse the site.

if it's a members only site, u may consider storing this in the user db instead.

or have a variation of both, if the site allows users specific pages, and members others.

 

Good luck with the site

 

Link to comment
Share on other sites

The other nice thing about my method is that you can add more languages very easily - you just change the link to a select, and have the value of each option set to the language. Then you just check that on the other side.

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.