how to create a template to support diffent languages?

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
lilili
Forum Newbie
Posts: 4
Joined: Thu Apr 03, 2008 12:33 am

how to create a template to support diffent languages?

Post by lilili »

I am doing a project, in which user first selects a languages,
and then the same html form written with selected language will be displayed. i.e. a user can decide to show the same form in English, French, and so on.

What I can think is I need to create a template html form
and several text xml files writen in different language.
then maybe I need to create a php file to parse the corresponding text xml file based on the language selected by users, and then combine the result to template form.

Since I am not familar with html and php, I do not know how to implent it in details. Can anybody give me a very simple example about how to create a template html and how to compine the parsed result to the template?

Thanks.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: how to create a template to support diffent languages?

Post by yacahuma »

no need to get that fancy(xml)

directory
language
|---EN
|---SP
|---FR

inside each have a file called language.php

language.php(for example in EN,english)
$NAME_LA = 'Name';
$PHONE_LA = 'Phone';
etc
etc

the on top of the page form

Code: Select all

 
include 'language/' . $SESSION['user_selected_lang'] . '/'language.php';
 
<p><label for="">$NAME_LA</label><input...></p>
 

check that the session is a valid value(EN,SP, or whatever)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: how to create a template to support diffent languages?

Post by Kieran Huggins »

You should consider the Gettext functions in PHP. I've never used them personally, but they seem to make a certain amount of sense at first glance.

gettext() also has a handy alias that makes your code a little easier to look at: _()

Is it a perfect solution? Not by a long shot (i18n and l10n are far from "solved") but it certainly seems to handle your template issue with a certain grace.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: how to create a template to support diffent languages?

Post by yacahuma »

kieran,

Why would gettext will be better that just including a language file?
I read the doc in php.net but do not see any extra benefits.


Thank you
matthewl
Forum Newbie
Posts: 13
Joined: Sat May 03, 2008 5:28 am

Re: how to create a template to support diffent languages?

Post by matthewl »

If your going to use includes, you might want to consider using constants rather than vars.

however the gettext functions are designed to standardize the translations.
lilili
Forum Newbie
Posts: 4
Joined: Thu Apr 03, 2008 12:33 am

Re: how to create a template to support diffent languages?

Post by lilili »

Thanks all for your suggestion.
Finally I implemented this function by using include.
gettext is also good. But in my case, the language will not change
once it is configured. so include may be simpler.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: how to create a template to support diffent languages?

Post by yacahuma »

If your going to use includes, you might want to consider using constants rather than vars.
Why is this??
Thank you
Post Reply