<?php
/**
* Extension du noyau
*
* @package Utopia
* @subpackage Kernel
*/
defined('INC') or exit;
/**
* Support de l'extension PHP gettext pour les langues
*
* @package Utopia
* @subpackage Kernel
*/
class KLang_gettext extends KLang {
private $sectionsDomain = array();
private $currentSection;
public function addLangSection($section) {
// Not available
return NULL;
}
public function deleteLangSection($section) {
// Not available
return NULL;
}
public function getLangSections() {
return array_keys($this -> sectionsDomain);
}
public function getString($id, $section = 'Kernel') {
if ($section != $this -> currentSection) {
if (isset($this -> sectionsDomain[$section])) {
textdomain($this -> sectionsDomain[$section]);
$this -> currentSection = $section;
}
else
$this -> fatalError(debug_backtrace(), 'SectionXFromLangNotLoaded,'.$section);
}
if (($gettext = gettext($id)) == $id && strpos($id, ',') !== FALSE) {
$args = preg_split('/(?<!\\\\),/', $id);
$args[0] = gettext($args[0]);
return stripcslashes(call_user_func_array('sprintf', $args));
}
return $gettext;
}
public function getSectionStrings($section) {
$strings = explode('|', gettext('@strings['.$section.']'));
return array_combine($strings, array_map('gettext', $strings));
}
public function isLangSectionLoaded($section) {
return isset($this -> sectionsDomain[$section]);
}
public function loadLang($fileName, $sectionsToLoad = NULL, $ignoreOverLoading = FALSE) {
$lang = substr($fileName, 0, strpos($fileName, '/'));
putenv('LANGUAGE='.$lang);
// setlocale(LC_ALL, $lang);
// if (defined('LC_MESSAGES'))
// setlocale(LC_MESSAGES, $lang);
// putenv('LANG='.$lang);
$domain = basename($fileName);
bindtextdomain($domain, $this -> langPath);
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);
foreach (explode('|', gettext('@sections')) as $section)
$this -> sectionsDomain[$section] = $domain;
}
public function saveLang($fileName, $sectionsToSave = NULL) {
// Save lang from gettext format to PHP table.
$lang = array();
if ($sectionsToSave === NULL)
$sectionsToSave = array_keys($this -> sectionsDomain);
foreach ($sectionsToSave as $sectionId) {
$strings = explode('|', gettext('@strings['.$sectionId.']'));
$lang[$sectionId] = array_combine($strings, array_map('gettext', $strings)); unset($strings);
ksort($lang[$sectionId], SORT_STRING);
}
ksort($lang, SORT_STRING);
$res = file_put_contents($this -> langPath.$fileName, "<?php\n// last-modified: ".gmdate('Y-m-d\TH:i:s\Z')."\n\nreturn ".var_export($lang, TRUE).";\n?>");
if ($res !== FALSE)
$this -> addToLog('notice', NULL, 'LangXSaved,'.$fileName);
else
$this -> error(debug_backtrace(), 'UnableToSaveLangX,'.$fileName);
return $res;
}
public function setString($id, $value, $section = 'Kernel') {
// Not available
return NULL;
}
}
class KLang_gettextConverter extends KLang_gettext {
public $gettextExecDir = '';
public $deletePoFiles = FALSE;
function __construct($logFile, $configPath, $langPath) {
parent::__construct($logFile, $configPath, $langPath);
// Try to get the execution directory of gettext runtimes (and tools).
if ($this -> gettextExecDir == '') {
$execDir = ini_get('safe_mode_exec_dir');
if (substr($execDir, -1) !== '/')
$execDir.= '/'; // PHP Doc: You have to use / as directory separator on all environments including Windows.
$this -> gettextExecDir = $execDir;
}
}
public function convertFile($fileName) {
$messages = include($this -> langPath.$fileName);
if (!$messages)
$this -> fatalError(debug_backtrace(), 'LangFileXDoesNotExist,'.$this -> langPath.$fileName);
if (is_array($messages)) {
$msgid = array();
$lang = substr($fileName, 0, strpos($fileName, '/'));
// Header
$out = "# Translation for Utopia (".$fileName.").\n"
// ."# Copyright (C) YEAR Free Software Foundation, Inc.\n"
// ."# FIRST AUTHOR <hide@address.com>, YEAR.\n"
."#\n"
."#, fuzzy\n"
."msgid \"\"\n"
."msgstr \"\"\n"
// ."\"Project-Id-Version: PACKAGE VERSION\\n\"\n"
."\"POT-Creation-Date: ".gmdate('Y-m-d H:i')."+0000\\n\"\n"
// ."\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n"
// ."\"Last-Translator: FULL NAME <hide@address.com>\\n\"\n"
."\"Language-Team: ".$lang."\\n\"\n"
."\"MIME-Version: 1.0\\n\"\n"
."\"Content-Type: text/plain; charset=UTF-8\\n\"\n"
."\"Content-Transfer-Encoding: 8bit\\n\"\n";
// Content
$out.= "\n"
// List all sections
."msgid \"@sections\"\n"
."msgstr \"".implode('|', array_keys($messages))."\"\n";
foreach ($messages as $sectionId => $strings) {
$out.= "\n"
."# ".$sectionId."\n"
// List all string IDs
."msgid \"@strings[".$sectionId."]\"\n"
."msgstr \"".implode('|', array_keys($strings))."\"\n"
."\n";
foreach ($strings as $stringId => $stringStr) {
if (!isset($msgid[$stringId])) {
$out.= "msgid \"".$stringId."\"\n"
."msgstr \"".addcslashes($stringStr, '"')."\"\n";
$msgid[$stringId] = 1;
}
}
}
$baseName = $this -> langPath.$lang.'/LC_MESSAGES/'.basename($fileName);
$result = file_put_contents($baseName.'.po', $out); unset($out);
if ($result !== FALSE) {
$baseName = substr(realpath($baseName.'.po'), 0, -3);
// Don't work with Safe Mode (cf. PHP Doc): all words following the initial command string are treated as a single argument. Thus, echo y | echo x becomes echo "y | echo x".
exec($this -> gettextExecDir.'msgfmt -o '.escapeshellarg($baseName.'.mo').' '.escapeshellarg($baseName.'.po'), $output);
$result = empty($output); // $output should be an empty array if there is no error.
}
if ($this -> deletePoFiles)
unlink($baseName.'.po');
return $result;
}
else
$this -> error(debug_backtrace(), 'LangFileXNotValid,'.$this -> langPath.$fileName);
return FALSE;
}
}
?>