Class PHP ManageLang
Description
Permet de gérer la langue, dans le contexte de développement web, et de gestion multilingue.
Voici le code source de la classe "ManageLang".
<?php
class manageLang {
// exemple d'usage de la classe
/*
$lang = new manageLang();
$lang->view_iso(); // display the language avalaible in ISO 639-2 Standard. (optional)
$lang->get_lang('fr,en,es'); // get the lang (needed)
$lang->put_env(); // define the environment to use in your webdev ! (needed)
*/
private $code='';
private $compteur=0;
private $dir='';
private $file=array(); // define all files
private $iso;
private $lang;
private $line=array(); // define file in array
private $locale;
private $mtime=array(); // define file's time modified
private $number;
private $origine=''; // define file 'ISO 639-2' original
public function __construct() {
$this->origine = "http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt";
$this->entry = array('biblio','alpha-3','alpha-2','english','french');
$this->lang = 'en';
$this->dir = dirname(__FILE__);
$this->file['new'] = $this->dir.'/'.basename($this->origine);
$this->file['old'] = $this->dir."/ISO-639-2_values_8bits.txt";
if(file_exists($this->file['new'])) $this->mtime['new'] = filemtime($this->file['new']);
$this->obtain_iso();
$this->get_line();
$this->array_lang();
}
private function obtain_iso() {
if(fopen($this->origine, 'r')) $this->get_src();
}
private function get_src() {
$this->headers['origine'] = get_headers($this->origine, 1);
$this->mtime['origine'] = strtotime($this->headers['origine']['Last-Modified']);
unset($this->headers);
if(!file_exists($this->file['new']) || $this->mtime['new'] < $this->mtime['origine'] ) {
$handle = fopen($this->file['new'], 'w');
$file = file_get_contents($this->origine);
fwrite($handle, $file);
fclose($handle);
}
}
private function get_line() {
if(file_exists($this->file['new']) && is_file($this->file['new'])
&& is_readable($this->file['new'])) {
$this->line = file($this->file['new']);
$this->file['ok'] = 'new';
}
elseif(file_exists($this->file['old']) && is_file($this->file['old'])
&& is_readable($this->file['old'])) {
$this->line = file($this->file['old']);
$this->file['ok'] = 'old';
}
else return FALSE;
}
private function array_lang() {
/*
Construire un tableau de lang de style :
$code = array(
// nous permettra de savoir combien de langue sont enregistrée officiellement !
"0" => array(
'biblio' => "xyz", // nom officiel bibliographique sur 3 lettres
'alpha-3' => "xyz", // nom officiel terminologique sur 3 lettres (alpha-3)
'alpha-2' => "ab", // nom officiel sur 2 lettres (alpha-2) __ norme ISO 639 1 __
'english' => "name", // nom officiel de la langue en anglais
'french' => "nom", // nom officiel de la langue en français
),
"n" => array(
),
);
*/
if(!empty($this->line) && is_array($this->line)) {
foreach($this->line as $k => $v) {
$this->code[$k] = explode("|", trim($v));
foreach($this->code[$k] as $k1 => $v1) {
$this->code[$k][$this->entry[$k1]] = $v1;
unset($this->code[$k][$k1]);
}
}
unset($k, $v);
}
}
/*
Avoir un aperçu
*/
public function view_iso() {
if(!empty($this->code) && is_array($this->code)) {
$this->count = count($this->code);
$this->textual = array('Code Alpha-3 Bibliographic','Code Alpha-3 Terminologic','Code Alpha-2','English Name','French Name');
$buffer = "<h1>ISO 639-2 Standard : Languages availables !</h1>\n";
$buffer .= "<h2>".$this->count." languages standards</h2>\n<dl>\n";
foreach($this->code as $k => $v) {
$this->compteur = $k + 1;
$buffer .= '<dt>~ '.$this->compteur." ~</dt>\n";
$this->increment = 0;
foreach($this->code[$k] as $v1) {
$buffer .= '<dd>';
$buffer .= '<em>'.$this->textual[$this->increment].'</em> : ';
if($v1 == "") $buffer .= 'no value';
else $buffer .= '<strong>'.$v1.'</strong>';
$buffer .= "</dd>\n";
$this->increment++;
}
unset($v1, $this->increment);
}
unset($k, $v, $this->compteur);
$buffer .= "</dl>\n";
$buffer .= '<p>The ISO 639-2 Standard is downlable in ASCII <a href="'.$this->origine.'" tabindex="1" title="File ISO 639-2 Standard ASCII">'.$this->origine.'</a> !</p>'."\n";
}
echo $buffer;
unset($buffer);
}
private function get_pref_language_array($str_http_languages) {
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You can get a copy of this license at http://www.gnu.org/licenses/gpl.txt.
*/
/*
Author : Yann Hamon
Modified by : Stephane HUC
*/
$this->langs = explode(',',$str_http_languages);
$this->qcandidat = 0;
$this->nb = count($this->langs);
for ($i=0; $i<$this->nb; $i++) {
for ($j=0; $j<$this->nb; $j++) {
if(isset($this->langs[$j])) $this->languages = trim($this->langs[$j]); // Supprime les espaces avant et après $lang
// Lang est de la forme langue ; q=valeur
if (!strstr($this->languages, ';') && $this->qcandidat != 1) {
// Si la chaine ne contient pas de valeur de préférence q
$this->candidat = $this->languages;
$this->qcandidat = 1;
$this->indicecandidat = $j;
}
else {
// On récupère l'indice q
$this->q = ereg_replace('.*;q=(.*)', '\\1', $this->languages);
if ($this->q > $this->qcandidat) {
$this->candidat = ereg_replace('(.*);.*', '\\1', $this->languages); ;
$this->qcandidat = $this->q;
$this->indicecandidat = $j;
}
}
}
$this->resultat[$i] = $this->candidat;
$this->qcandidat=0;
// On supprime la valeur du tableau
unset($this->langs[$this->indicecandidat]);
$this->langs = array_values($this->langs);
}
return $this->resultat;
}
private function verify_code_lang($lang) {
/*
Pour info, sur l'ancien fichier :
- l'anglais est : $this->code["126"][x];
- le français est : $this->code["139"][x];
- l'allemand est : $this->code["151"][x];
- l'italien est : $this->code["204"][x];
- l'espagnol est : $this->code["407"][x];
etc...
*/
$search = array (',','|',"'",'"',':',';');
$lang = str_replace($search,'|',$lang);
$array = explode('|', $lang);
if(!empty($array) && is_array($array)) {
foreach($array as $k => $v) {
if(!empty($this->code) && is_array($this->code)) {
$this->increment=0;
foreach($this->code as $k1 => $v1) {
if(!empty($this->code[$k1]) && is_array($this->code[$k1])) {
foreach($this->code[$k1] as $v2) {
if(strcmp($v, $this->code[$k1][$this->entry[2]]) == 0) {
// define languages in function to the website !
$this->language[$this->increment] = $this->code[$k1][$this->entry[2]];
}
}
$this->increment++;
}
}
unset($this->increment);
}
}
}
unset($array,$lang,$search);
if(empty($this->language)) $this->language[0] = $this->lang;
}
public function get_lang($lang) {
$this->verify_code_lang($lang);
if(!empty($_GET["lang"]) && in_array($_GET["lang"], $this->language)) {
$this->lg = strip_tags($_GET["lang"]);
}
elseif(!empty($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
$this->preflang = $this->get_pref_language_array($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
$this->result = array_intersect($this->preflang, $this->language);
if (count($this->result) != 0) {
if(!empty($this->result[0])) $this->lg = $this->result[0];
else $this->lg = $this->result[1];
}
else $this->lg = $this->language[0];
}
else $this->lg = $this->language[0];
}
public function put_env() {
if(!empty($this->lg) && is_string($this->lg)) {
$this->LG = strtoupper($this->lg);
$this->LANG = $this->lg."_".$this->LG;
putenv('LANG='.$this->LANG);
setlocale(LC_ALL, $this->LANG);
}
}
}
?>
<<| Page : PHP : class : manageLang : |>>