Internationalization

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mlitton
Forum Newbie
Posts: 11
Joined: Sun Sep 21, 2003 4:52 pm
Location: Sweden

Internationalization

Post by mlitton »

Is there a good way too switch between languages in PHP? Like if I wan't a date to be viewed in several different languages.

I've already designed such a class myself but it isn't all that

-- Mike
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

u could set all the date formats in diffrenet langs in a array and when u need to switch just call that array with a key, e.g.

Code: Select all

<?php
$dates[lang1] = date("d/m/y", time());
$dates[lang2] = date("m/d/y", time());
$dates[lang3] = date("y/m/y", time());
?>
when u need it, use the key, .e.g. if u need lang1 then use

Code: Select all

<?php
echo $dates[lang1];
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

unless lang1, lang2, lang3 are defines use

Code: Select all

$dates['lang1'] = ...;

echo $dates['lang1'];
?>
see also: http://php.net/manual/en/language.types.array.php#language.types.array.donts
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

actually the date format is not language dependant :)
you can save all the date formats in a db, array or file, then
make a dropdown or something like that where u can select what date format you want to use :)
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

Cruzado_Mainfrm wrote:actually the date format is not language dependant :)
you can save all the date formats in a db, array or file, then
make a dropdown or something like that where u can select what date format you want to use :)
i did somehting like that. i came up with 76 different formats... i save the person's preference in a tinyint and use an array to get it....

Code: Select all

# time display choices (for misc options control panel)
$tdc=array('0'=>'08/15/2003 22:02:27', '1'=>'08/15/2003 10:02:27', '2'=>'08/15/2003 10:02:27 PM', '3'=>'08/15/2003 10:02:27 pm', '4'=>'Fri Aug 15 2003 22:02:27', '5'=>'Fri Aug 15 2003 10:02:27 PM', '6'=>'Fri Aug 15 2003 10:02:27 pm', '7'=>'08-15-2003 22:02:27', '8'=>'08-15-2003 10:02:27', '9'=>'08-15-2003 10:02:27 PM', '10'=>'08-15-2003 10:02:27 pm', '11'=>'8/15/2003 22:02:27', '12'=>'8/15/2003 10:02:27', '13'=>'8/15/2003 10:02:27 PM', '14'=>'8/15/2003 10:02:27 pm', '15'=>'8-15-2003 22:02:27', '16'=>'8-15-2003 10:02:27', '17'=>'8-15-2003 10:02:27 PM', '18'=>'8-15-2003 10:02:27 pm', '19'=>'08/15/2003 22:02', '20'=>'08/15/2003 10:02', '21'=>'08/15/2003 10:02 PM', '22'=>'08/15/2003 10:02 pm', '23'=>'Fri Aug 15 2003 22:02', '24'=>'Fri Aug 15 2003 10:02 PM', '25'=>'Fri Aug 15 2003 10:02 pm', '26'=>'08-15-2003 22:02', '27'=>'08-15-2003 10:02', '28'=>'08-15-2003 10:02 PM', '29'=>'08-15-2003 10:02 pm', '30'=>'8/15/2003 22:02', '31'=>'8/15/2003 10:02', '32'=>'8/15/2003 10:02 PM', '33'=>'8/15/2003 10:02 pm', '34'=>'8-15-2003 22:02', '35'=>'8-15-2003 10:02', '36'=>'8-15-2003 10:02 PM', '37'=>'8-15-2003 10:02 pm', '38'=>'08/15/03 22:02:27', '39'=>'08/15/03 10:02:27', '40'=>'08/15/03 10:02:27 PM', '41'=>'08/15/03 10:02:27 pm', '42'=>'Fri Aug 15 03 22:02:27', '43'=>'Fri Aug 15 03 10:02:27 PM', '44'=>'Fri Aug 15 03 10:02:27 pm', '45'=>'08-15-03 22:02:27', '46'=>'08-15-03 10:02:27', '47'=>'08-15-03 10:02:27 PM', '48'=>'08-15-03 10:02:27 pm', '49'=>'8/15/03 22:02:27', '50'=>'8/15/03 10:02:27', '51'=>'8/15/03 10:02:27 PM', '52'=>'8/15/03 10:02:27 pm', '53'=>'8-15-03 22:02:27', '54'=>'8-15-03 10:02:27', '55'=>'8-15-03 10:02:27 PM', '56'=>'8-15-03 10:02:27 pm', '57'=>'08/15/03 22:02', '58'=>'08/15/03 10:02', '59'=>'08/15/03 10:02 PM', '60'=>'08/15/03 10:02 pm', '61'=>'Fri Aug 15 03 22:02', '62'=>'Fri Aug 15 03 10:02 PM', '63'=>'Fri Aug 15 03 10:02 pm', '64'=>'08-15-03 22:02', '65'=>'08-15-03 10:02', '66'=>'08-15-03 10:02 PM', '67'=>'08-15-03 10:02 pm', '68'=>'8/15/03 22:02', '69'=>'8/15/03 10:02', '70'=>'8/15/03 10:02 PM', '71'=>'8/15/03 10:02 pm', '72'=>'8-15-03 22:02', '73'=>'8-15-03 10:02', '74'=>'8-15-03 10:02 PM', '75'=>'8-15-03 10:02 pm');
crap. i just realized i missed at least 76 choices... all mine do mdy... well maybe not 76, but i don't have any dmy or ydm or ymd....
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

As a better idea, why not store your date and time formats seperately and let the user pick both? It drastically cuts down the number of combinations you can have.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Just adding ideas...

Or just let them type "M Y D" or whatever as they want it themselves, combined with preview for easier understanding and effect?

If it's more than dates, time, currency etc., I'd use seperate *.lang files, common in some larger projects (phpbb as example).

You could then get the users browser setting.
I have "Accept-Language: en-us,en;q=0.5", so a en.lang.php might work for me in this case...
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

i think those are far too many choices, the user will get annoyed by so many choices, and he can't find his, what you should do, is make a simple sintax where the user can write:

Code: Select all

"&#1111;hour]:&#1111;min]:&#1111;sec] &#1111;ampm] &#1111;month]/&#1111;day]/&#1111;year]" to get "06:45:34 pm 3/15/03"
another example:

Code: Select all

"&#1111;HOUR]:&#1111;min] &#1111;monthname] &#1111;day], &#1111;YEAR]" to get "18:45:34 September 16, 2003"
design the page like this:

[select hours format] [Insert]
[select minutes format] [Insert]
[select ??whatever?? format] [Insert] <-- the last [] is a button...
hope u get me

wasn't that a nice idea?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

JAM wrote:Just adding ideas...

Or just let them type "M Y D" or whatever as they want it themselves, combined with preview for easier understanding and effect?

If it's more than dates, time, currency etc., I'd use seperate *.lang files, common in some larger projects (phpbb as example).

You could then get the users browser setting.
I have "Accept-Language: en-us,en;q=0.5", so a en.lang.php might work for me in this case...
tinyint: 1 bite
varchar needed for their selection: 2 bites+1 for the length of their selection

hence why i went for it like i did
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

yea that is the first option, but u'll have to explain 'every' letter at glance...

also, it will be good after the user writes the format she/he is going to use click in a button that will show him how the date will look like(They should add this to the phpbb forums!).
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

m3rajk:
Yes, I know you need something else than tinyint. I still prefer letting the user choose for themselves rather than save space in this matter.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

lol..you guys scare the crap out of people lol..he only wanted to know one little thing you know :P
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I like when people explore the options, any options.
If there are several good ideas, why not mention them all? ;)

Doesn't ask for an 'easy' way to do it, and by managing to build a class any of these ideas would be pretty selfexplainatory. Or so I hope.
Post Reply