Re: Newbie asks about multi-lingual website strategies

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



your all missing something, namely that the browser can tell you what
it's preferred language is (which you can use to select a language in the event
no language has yet been determined for the current session).

you do this by parsing the value of the HTTP_ACCEPT_LANGUAGE request header.
HTTP_ACCEPT_LANGUAGE is in the form: en-us,en,fr,nl,nl-be;q=0.5

<?php

// following if statement exists for testing this code on the command line
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
	$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-us,en,fr,nl,nl-be;q=0.5';

$lLangs = explode(',', preg_replace('#;.*$#', '', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
$sLangs = array_unique(array_map('substr', $lLangs, array_fill(0, count($lLangs), 0), array_fill(0, count($lLangs), 2)));

var_dump($lLangs, $sLangs);

Andrés Robinet wrote:
> We use something similar to the following
> 
> define('DEFAULT_LANG_ID', 'en');
> 
> function getLanguageId() {
>   // Allow for language id override in $_GET, $_POST and $_COOKIE
>   $req_lang_id = $_REQUEST['lang_id'];
>   // Retrieve the one stored in the session if any
>   $sess_lang_id = $_SESSION['lang_id'];
>   // $lang_id will contain the lang id retrieved from request (overrides
> session),
>   // or from session or a default one
>   $lang_id = isset($req_lang_id) ? $req_lang_id : (isset($sess_lang_id) ?
> $sess_lang_id : DEFAULT_LANG_ID);
>   // Save it for next time
>   $_SESSION['lang_id'] = $lang_id;
>   return $lang_id;
> }
> 
> Rob
> 
> 
> Andrés Robinet | Lead Developer | BESTPLACE CORPORATION
> 5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308
> | TEL 954-607-4207 | FAX 954-337-2695
> Email: info@xxxxxxxxxxxxx  | MSN Chat: best@xxxxxxxxxxxxx  |  SKYPE:
> bestplace |  Web: http://www.bestplace.biz | Web: http://www.seo-diy.com
> 
>> -----Original Message-----
>> From: Jason Pruim [mailto:japruim@xxxxxxxxxx]
>> Sent: Tuesday, November 27, 2007 11:15 AM
>> To: Jeff Benetti
>> Cc: php-general@xxxxxxxxxxxxx
>> Subject: Re:  Newbie asks about multi-lingual website strategies
>>
>>
>> On Nov 27, 2007, at 8:37 AM, Jeff Benetti wrote:
>>
>>> I'm a noob so keep the comments to a noob's level please.
>>>
>>> I am doing a website and my client wants the bulk of the text to be
>>> bilingual (French and English).  The last site I did used php and
>>> mysql so I
>>> am getting comfortable with that technology.  Typically I am using a
>>> single
>>> php file and my menu constantly points to the same file with
>>> different id
>>> options example "index.php?id=30" and I want to use the same idea to
>>> choose
>>> a language example "index.php?lang=fr&id=30".  Pretty straight
>>> forward for
>>> many of you folks but before I start reinventing the wheel I
>>> wondered if
>>> anyone could offer any suggestions.  I have a couple of approaches
>>> in mind.
>>>
>> I'm actually in the process of planning this as well... English and
>> Spanish...
>>
>>> 1: Session vars, I have never used this but it seems straight
>> forward.
>>> Drawbacks?
>> Sessions are easy enough to use, just make sure that you are setting
>> the variable in the right place... On a site I did the stored variable
>> was always 1 behind what I wanted it to be... I moved the setting of
>> the variable, and it worked :)
>>> 2: Cookies again not too big a deal, never used cookies either but it
>>> doesn't seem to be mystifying however the fact that the user can turn
>>> cookies off makes me not want to go this route.
>>> 3: Use the mysql database and log each ip address and record the
>>> preference
>>> and maybe the last time on the site.  I am leaning in this direction
>>> because
>>> I think it is the most robust but will it be slow?  First I have to
>>> get the
>>> ip then I have to check to see if it is in my data base and then get
>>> the
>>> language preference.  It would be great to have a standardized
>>> function that
>>> I could use on all of my sites.  I live in a bilingual country
>>> (Canada) so
>>> this could be a real selling point for my services.
>> I don't know much about using mysql for storing IP's in canada... But
>> in the US, all IP's except for businesses are dynamic, they could
>> change every time a user would go to a site.. I could see that causing
>> issues...
>>
>>>
>>> Any any and all comments are welcome, it will be a learning curve no
>>> matter
>>> which route I take so a little advice on the best direction pros
>>> cons would
>>> be great.
>>>
>>> And of course knowing that I will have many many thousands of people
>>> on my
>>> site (hee hee) which option will perform best once I start
>>> accumulating
>>> vistors.  That's one problem I see with the mysql solution, I think
>>> it may
>>> start to be slow unless I start purging vistors who have not shown
>>> up in a
>>> while or limit the number of entries.
>> I'm currently planning to store the text of the pages on my site in a
>> database, so that I can very easily just change the language
>> preference, and get the english, or spanish out of the database. I
>> also think it will help speed up the site for when I have my thousands
>> of users a second :)
>>
>> When you start doing it, let me know your experiences... I plan to
>> actually start coding (If me and the site owner can see eye to eye on
>> some stuff...) in january.
>>
>>
>> --
>>
>> Jason Pruim
>> Raoset Inc.
>> Technology Manager
>> MQC Specialist
>> 3251 132nd ave
>> Holland, MI, 49424
>> www.raoset.com
>> japruim@xxxxxxxxxx
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux