Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 17th, 2005, 09:40 AM
Lüpher Cypher
Guest
 
Posts: n/a
Default Internationalizing pages

Hi,
I'm writing a site in PHP with mySQL. I've decided to have it in several
languages, but don't want to use XML just yet. So, I was wondering if
anyone has done it before and have some ideas. :)
Basically, I've come up with this: create include files with error
messages/page text/etc. and define 2-dimensional arrays, say:

global $errors;
$errors = array("id_error1" => ("en" => "error in english",
"de" => "error in german",
"fr" => "error in french",
...
),
"id_error2" => ...
);

then define a function, say,

getLangText($array,$id) {
global $$array;
global $lang;
$msg = $$array[$id];
return $msg[$lang];
}

where $lang is a global variable containing locality string. So, if we
need error1, we'd call:

echo getLangText("errors","error1");

This would work, but I thought maybe someone has a more elegant solution :)

Thanks
  #2  
Old July 17th, 2005, 09:40 AM
J.O. Aho
Guest
 
Posts: n/a
Default Re: Internationalizing pages

Lüpher Cypher wrote:[color=blue]
> Hi,
> I'm writing a site in PHP with mySQL. I've decided to have it in several
> languages, but don't want to use XML just yet. So, I was wondering if
> anyone has done it before and have some ideas. :)
> Basically, I've come up with this: create include files with error
> messages/page text/etc. and define 2-dimensional arrays, say:
>
> global $errors;
> $errors = array("id_error1" => ("en" => "error in english",
> "de" => "error in german",
> "fr" => "error in french",
> ...
> ),
> "id_error2" => ...
> );
>
> then define a function, say,
>
> getLangText($array,$id) {
> global $$array;
> global $lang;
> $msg = $$array[$id];
> return $msg[$lang];
> }
>
> where $lang is a global variable containing locality string. So, if we
> need error1, we'd call:
>
> echo getLangText("errors","error1");
>
> This would work, but I thought maybe someone has a more elegant solution :)[/color]

What I had done, if I had done somethign similare would have been

create languges include files for each language, eg: lang_en.php lang_fi.php

In the langauge file, I would use defines for each error message.

Depending on what language is used, I would include that languages and use the
defines to get the error message you want.

IMHO this would be easier to handle and you get less ram used and a fraction
faster too.


//Aho


  #3  
Old July 17th, 2005, 09:40 AM
Tony Marston
Guest
 
Posts: n/a
Default Re: Internationalizing pages


"J.O. Aho" <user@example.net> wrote in message
news:2r4p5gF15f3nuU1@uni-berlin.de...[color=blue]
> Lüpher Cypher wrote:[color=green]
>> Hi,
>> I'm writing a site in PHP with mySQL. I've decided to have it in several
>> languages, but don't want to use XML just yet. So, I was wondering if
>> anyone has done it before and have some ideas. :)
>> Basically, I've come up with this: create include files with error
>> messages/page text/etc. and define 2-dimensional arrays, say:
>>
>> global $errors;
>> $errors = array("id_error1" => ("en" => "error in english",
>> "de" => "error in german",
>> "fr" => "error in french",
>> ...
>> ),
>> "id_error2" => ...
>> );
>>
>> then define a function, say,
>>
>> getLangText($array,$id) {
>> global $$array;
>> global $lang;
>> $msg = $$array[$id];
>> return $msg[$lang];
>> }
>>
>> where $lang is a global variable containing locality string. So, if we
>> need error1, we'd call:
>>
>> echo getLangText("errors","error1");
>>
>> This would work, but I thought maybe someone has a more elegant solution
>> :)[/color]
>
> What I had done, if I had done somethign similare would have been
>
> create languges include files for each language, eg: lang_en.php
> lang_fi.php
>
> In the langauge file, I would use defines for each error message.
>
> Depending on what language is used, I would include that languages and use
> the defines to get the error message you want.
>
> IMHO this would be easier to handle and you get less ram used and a
> fraction faster too.[/color]

I agree. Have a separate file for each language rather than a single file
for every language.

--
Tony Marston

http://www.tonymarston.net



  #4  
Old July 17th, 2005, 09:40 AM
Lüpher Cypher
Guest
 
Posts: n/a
Default Re: Internationalizing pages

Tony Marston wrote:
[color=blue]
> "J.O. Aho" <user@example.net> wrote in message
> news:2r4p5gF15f3nuU1@uni-berlin.de...
>[color=green]
>>Lüpher Cypher wrote:
>>[color=darkred]
>>>Hi,
>>>I'm writing a site in PHP with mySQL. I've decided to have it in several
>>>languages, but don't want to use XML just yet. So, I was wondering if
>>>anyone has done it before and have some ideas. :)
>>>Basically, I've come up with this: create include files with error
>>>messages/page text/etc. and define 2-dimensional arrays, say:
>>>
>>>global $errors;
>>>$errors = array("id_error1" => ("en" => "error in english",
>>> "de" => "error in german",
>>> "fr" => "error in french",
>>> ...
>>> ),
>>> "id_error2" => ...
>>> );
>>>
>>>then define a function, say,
>>>
>>>getLangText($array,$id) {
>>> global $$array;
>>> global $lang;
>>> $msg = $$array[$id];
>>> return $msg[$lang];
>>>}
>>>
>>>where $lang is a global variable containing locality string. So, if we
>>>need error1, we'd call:
>>>
>>>echo getLangText("errors","error1");
>>>
>>>This would work, but I thought maybe someone has a more elegant solution
>>>:)[/color]
>>
>>What I had done, if I had done somethign similare would have been
>>
>>create languges include files for each language, eg: lang_en.php
>>lang_fi.php
>>
>>In the langauge file, I would use defines for each error message.
>>
>>Depending on what language is used, I would include that languages and use
>>the defines to get the error message you want.
>>
>>IMHO this would be easier to handle and you get less ram used and a
>>fraction faster too.[/color]
>
>
> I agree. Have a separate file for each language rather than a single file
> for every language.[/color]

Ok, that makes sense :) The only drawback is that whenever I need to add
a string, I'll have to go through several files and may forget to update
one of them, as opposed to having them all in one file - I'll be
"forced" to update all strings :) The thing is, the site will consist of
several parts, only one of which will be shown, therefore I don't really
need to have one large file for all of them (I put the parts into
separate directories, since they are kind of "subsites" themselves) :)
Another problem with several files is looking up strings by id: when
they are in one file, I'll just have to find one id and I can make sure
the string is ok in all languages at once, with separate files I'll have
to look in several places :) I think that could be important if I add a
language that I don't really know (say, if someone provides a
translation for me) :)

Thanks
  #5  
Old July 17th, 2005, 09:40 AM
Michael Fesser
Guest
 
Posts: n/a
Default Re: Internationalizing pages

.oO(Lüpher Cypher)
[color=blue]
>I'm writing a site in PHP with mySQL. I've decided to have it in several
>languages, but don't want to use XML just yet. So, I was wondering if
>anyone has done it before and have some ideas. :)[/color]

Have a look at gettext.

http://www.php.net/gettext

Micha
  #6  
Old July 17th, 2005, 09:40 AM
steve
Guest
 
Posts: n/a
Default Re: Re: Internationalizing pages

"Lpher_Cypher" wrote:[color=blue]
> Tony Marston wrote:
>[color=green]
> > "J.O. Aho" &lt;user@example.net&gt; wrote in message
> > news:2r4p5gF15f3nuU1@uni-berlin.de...
> >[/color]
> &nbsp;>>Lüpher Cypher wrote:
> &nbsp;>>
> &nbsp;&nbsp;>>>Hi,
> &nbsp;&nbsp;>>>I'm writing a site in PHP with mySQL. I've
> decided to have it in several
> &nbsp;&nbsp;>>>languages, but don't want to use XML just yet.
> So, I was wondering if
> &nbsp;&nbsp;>>>anyone has done it before and have some ideas.
> :)
> &nbsp;&nbsp;>>>Basically, I've come up with this: create
> include files with error
> &nbsp;&nbsp;>>>messages/page text/etc. and define
> 2-dimensional arrays, say:
> &nbsp;&nbsp;>>>
> &nbsp;&nbsp;>>>global $errors;
> &nbsp;&nbsp;>>>$errors = array("id_error1" =&gt; ("en" =&gt;
> "error in english",
> &nbsp;&nbsp;>>> "de" =&gt; "error in german",
> &nbsp;&nbsp;>>> "fr" =&gt; "error in french",
> &nbsp;&nbsp;>>> ...
> &nbsp;&nbsp;>>> ),
> &nbsp;&nbsp;>>> "id_error2" =&gt; ...
> &nbsp;&nbsp;>>> );
> &nbsp;&nbsp;>>>
> &nbsp;&nbsp;>>>then define a function, say,
> &nbsp;&nbsp;>>>
> &nbsp;&nbsp;>>>getLangText($array,$id) {
> &nbsp;&nbsp;>>> global $$array;
> &nbsp;&nbsp;>>> global $lang;
> &nbsp;&nbsp;>>> $msg = $$array[$id];
> &nbsp;&nbsp;>>> return $msg[$lang];
> &nbsp;&nbsp;>>>}
> &nbsp;&nbsp;>>>
> &nbsp;&nbsp;>>>where $lang is a global variable containing
> locality string. So, if we
> &nbsp;&nbsp;>>>need error1, we'd call:
> &nbsp;&nbsp;>>>
> &nbsp;&nbsp;>>>echo getLangText("errors","error1");
> &nbsp;&nbsp;>>>
> &nbsp;&nbsp;>>>This would work, but I thought maybe someone
> has a more elegant solution
> &nbsp;&nbsp;>>>:)
> &nbsp;>>
> &nbsp;>>What I had done, if I had done somethign similare
> would have been
> &nbsp;>>
> &nbsp;>>create languges include files for each language, eg:
> lang_en.php
> &nbsp;>>lang_fi.php
> &nbsp;>>
> &nbsp;>>In the langauge file, I would use defines for each
> error message.
> &nbsp;>>
> &nbsp;>>Depending on what language is used, I would include
> that languages and use
> &nbsp;>>the defines to get the error message you want.
> &nbsp;>>
> &nbsp;>>IMHO this would be easier to handle and you get less
> ram used and a
> &nbsp;>>fraction faster too.[color=green]
> >
> >
> > I agree. Have a separate file for each language rather than[/color]
> a single file[color=green]
> > for every language.[/color]
>
> Ok, that makes sense :) The only drawback is that whenever I
> need to add
> a string, I'll have to go through several files and may forget
> to update
> one of them, as opposed to having them all in one file - I'll
> be
> "forced" to update all strings :) The thing is, the site will
> consist of
> several parts, only one of which will be shown, therefore I
> don't really
> need to have one large file for all of them (I put the parts
> into
> separate directories, since they are kind of "subsites"
> themselves) :)
> Another problem with several files is looking up strings by
> id: when
> they are in one file, I'll just have to find one id and I can
> make sure
> the string is ok in all languages at once, with separate files
> I'll have
> to look in several places :) I think that could be important
> if I add a
> language that I don't really know (say, if someone provides a
> translation for me) :)
>
> Thanks[/color]

Your other option to keep things nicely synchronized (you have a valid
point there), is to create language tables in mysql, where every
column is for a different langugage. That way, things are nicely in
synch. There are no performance issues with mysql since mysql can
cache (by default) query results that are not changing.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-Internat...ict151183.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=506567
  #7  
Old July 17th, 2005, 09:45 AM
Jonathan Smith
Guest
 
Posts: n/a
Default Re: Internationalizing pages

Having worked on localisation projects in the past and being in the
process of writing a PHP/Smarty driven site in multiple languages, I
hope I can help.

I have indeed created what I call "language packs", each file a PHP with
an array of strings something like:

$strings = array(
"TEXT1" => "Text 1",
"TEXT2" => "Text 2",
"TEXT3" => "Text 3"
);

You can use a db approach (such as ACCESS) to manage the string
translation with an export function to produce automatically the
language packs on demand.

Using the strings array approach you can make your localisation text
available both to PHP code and to SMARTY code segments (using the ASSIGN
function to create an equivalent Smarty variable)

You should bear in mind though that INTERNATIONALISATION is not limited
the "translation" of words. If you wish to offer your PHP program to
various audiences around the world bear in mind that DATE and NUMBER
formats are different across the world and you will encounter complex
situations to manage when STORING, VIEWING and MANIPULATING data across
multiple data standards around the world. Rule number one, remember that
everything will be converted back into basic (anglo-saxon based) formats
(20040922, 12345690192.00 etc) for numbers and accents needed to be
handled carefully.

Hope this helps...

Jonathan
  #8  
Old July 17th, 2005, 09:48 AM
Lüpher Cypher
Guest
 
Posts: n/a
Default Re: Internationalizing pages

steve wrote:[color=blue]
> "Lpher_Cypher" wrote:[color=green]
> > Tony Marston wrote:
> >[color=darkred]
> > > "J.O. Aho" &lt;user@example.net&gt; wrote in message
> > > news:2r4p5gF15f3nuU1@uni-berlin.de...
> > >[/color]
> > &nbsp;>>Lüpher Cypher wrote:
> > &nbsp;>>
> > &nbsp;&nbsp;>>>Hi,
> > &nbsp;&nbsp;>>>I'm writing a site in PHP with mySQL. I've
> > decided to have it in several
> > &nbsp;&nbsp;>>>languages, but don't want to use XML just yet.
> > So, I was wondering if
> > &nbsp;&nbsp;>>>anyone has done it before and have some ideas.
> > :)
> > &nbsp;&nbsp;>>>Basically, I've come up with this: create
> > include files with error
> > &nbsp;&nbsp;>>>messages/page text/etc. and define
> > 2-dimensional arrays, say:
> > &nbsp;&nbsp;>>>
> > &nbsp;&nbsp;>>>global $errors;
> > &nbsp;&nbsp;>>>$errors = array("id_error1" =&gt; ("en" =&gt;
> > "error in english",
> > &nbsp;&nbsp;>>> "de" =&gt; "error in german",
> > &nbsp;&nbsp;>>> "fr" =&gt; "error in french",
> > &nbsp;&nbsp;>>> ...
> > &nbsp;&nbsp;>>> ),
> > &nbsp;&nbsp;>>> "id_error2" =&gt; ...
> > &nbsp;&nbsp;>>> );
> > &nbsp;&nbsp;>>>
> > &nbsp;&nbsp;>>>then define a function, say,
> > &nbsp;&nbsp;>>>
> > &nbsp;&nbsp;>>>getLangText($array,$id) {
> > &nbsp;&nbsp;>>> global $$array;
> > &nbsp;&nbsp;>>> global $lang;
> > &nbsp;&nbsp;>>> $msg = $$array[$id];
> > &nbsp;&nbsp;>>> return $msg[$lang];
> > &nbsp;&nbsp;>>>}
> > &nbsp;&nbsp;>>>
> > &nbsp;&nbsp;>>>where $lang is a global variable containing
> > locality string. So, if we
> > &nbsp;&nbsp;>>>need error1, we'd call:
> > &nbsp;&nbsp;>>>
> > &nbsp;&nbsp;>>>echo getLangText("errors","error1");
> > &nbsp;&nbsp;>>>
> > &nbsp;&nbsp;>>>This would work, but I thought maybe someone
> > has a more elegant solution
> > &nbsp;&nbsp;>>>:)
> > &nbsp;>>
> > &nbsp;>>What I had done, if I had done somethign similare
> > would have been
> > &nbsp;>>
> > &nbsp;>>create languges include files for each language, eg:
> > lang_en.php
> > &nbsp;>>lang_fi.php
> > &nbsp;>>
> > &nbsp;>>In the langauge file, I would use defines for each
> > error message.
> > &nbsp;>>
> > &nbsp;>>Depending on what language is used, I would include
> > that languages and use
> > &nbsp;>>the defines to get the error message you want.
> > &nbsp;>>
> > &nbsp;>>IMHO this would be easier to handle and you get less
> > ram used and a
> > &nbsp;>>fraction faster too.[color=darkred]
> > >
> > >
> > > I agree. Have a separate file for each language rather than[/color]
> > a single file[color=darkred]
> > > for every language.[/color]
> >
> > Ok, that makes sense :) The only drawback is that whenever I
> > need to add
> > a string, I'll have to go through several files and may forget
> > to update
> > one of them, as opposed to having them all in one file - I'll
> > be
> > "forced" to update all strings :) The thing is, the site will
> > consist of
> > several parts, only one of which will be shown, therefore I
> > don't really
> > need to have one large file for all of them (I put the parts
> > into
> > separate directories, since they are kind of "subsites"
> > themselves) :)
> > Another problem with several files is looking up strings by
> > id: when
> > they are in one file, I'll just have to find one id and I can
> > make sure
> > the string is ok in all languages at once, with separate files
> > I'll have
> > to look in several places :) I think that could be important
> > if I add a
> > language that I don't really know (say, if someone provides a
> > translation for me) :)
> >
> > Thanks[/color]
>
> Your other option to keep things nicely synchronized (you have a valid
> point there), is to create language tables in mysql, where every
> column is for a different langugage. That way, things are nicely in
> synch. There are no performance issues with mysql since mysql can
> cache (by default) query results that are not changing.
>[/color]

Well, that's exactly what I'm doing for content in db :)
Say, I have a news page so, I have a table:
table news (
....
text_en text ...
text_de text ...
)
and then I just use a function, say, get_db_text($row,$field,$lang)
which appends the language suffix to the field name :)

But then, I still think it's easier to maintain the page content (which
is not in db) through an include file, since if I need to edit something
I wouldn't have to deal with queries etc. :)

Lüph
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles