Date: 10/09/00
- Next message: uw: "[phplib-dev] cvs commit"
- Previous message: Victor M. Varela: "Re: [phplib-dev] Maybe somebody is interesting in an Interbase layer"
- Next in thread: Ulf Wendel: "Re: [phplib-dev] [Fwd: phplib Template multilingual extension]"
- Reply: Ulf Wendel: "Re: [phplib-dev] [Fwd: phplib Template multilingual extension]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
-------- Original Message --------
From: Michael Kahn <mkahn <email protected>>
Subject: phplib Template multilingual extension
To: kk <email protected>
Hi:
PHPLIB is an excellent extension to PHP! The main feature we are using
is the Template class, so we can split up the scripts my developers
write from the magic our "pixel princess" (graphics and layout person)
does.
However, we had a specific problem that required I extend the Template
class, so I thought I'd send the example back to you in case anyone else
asks. We need to provide multilingual support (french/english in our
case) for several of our virtual hosts. Java-style resource bundles are
sometimes OK, but a lot of the time you need to relayout the whole thing
for french because english (the default we work in) is usually so terse
by comparison, and if you just stick a french string in a spot you
designed for an english one it often looks awful. Hence, templates are
perfect, but we wanted to be able to switch templates automatically
based on the language our customer's browser reports to prefer.
So in my extension the Template->filename() method has been overridden
to work like Apache's built-in multilinugal support. First it retrieves
an array of 2-letter lanugage code preferences from any Accept-Language
headers in the HTTP request, using a TEMPLATE_DEFAULT_LANGUAGE
evironment variable to identify the language of the default templates.
Then it looks in the order of the list for a "$filename.$lang" file, so
if your list was "fr, en" and your template was "mytemplate.ihtml", it
would look for "mytemplate.ihtml.fr", then "mytemplate.ihtml.en" (or in
our case, because the TEMPLATE_DEFAULT_LANGUAGE was en, it would look
for just "mytemplate.ihtml" in the second case). So, if you use
$t->set_file("page","mytemplate.ihtml"), it returns the template for the
first language match on the list that it finds.
Here's the code snippet, hope you find it useful:
function filename($filename) {
$default_lang = $this->get_default_language();
// ... (other stuff)
foreach ($this->get_languages() as $lang) {
if ($lang == $default_lang) {
$lfile = $filename;
}
else {
$lfile = $filename . ".$lang";
}
echo "<!-- looking for ($lfile) -->\n";
if (file_exists($lfile)) {
echo "<!-- found $lfile -->";
return $lfile;
}
}
// .... (more cleanup checking, etc)
}
function get_languages() {
$headers = getallheaders();
$language_list =
preg_replace("/;\s*q=[\d\.]+/","",$headers['Accept-Language']);
$language_list =
preg_replace("/\s*/","",$headers['Accept-Language']);
return explode(",",$language_list);
}
function get_default_language() {
$lang = getenv("DEFAULT_TEMPLATE_LANGUAGE");
return $lang ? $lang : 'en';
}
-- --- Michael Kahn Ottawa, Canada--------------------------------------------------------------------- To unsubscribe, e-mail: phplib-dev-unsubscribe <email protected> For additional commands, e-mail: phplib-dev-help <email protected>
- Next message: uw: "[phplib-dev] cvs commit"
- Previous message: Victor M. Varela: "Re: [phplib-dev] Maybe somebody is interesting in an Interbase layer"
- Next in thread: Ulf Wendel: "Re: [phplib-dev] [Fwd: phplib Template multilingual extension]"
- Reply: Ulf Wendel: "Re: [phplib-dev] [Fwd: phplib Template multilingual extension]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

