Source for file gettext.php
Documentation is available at gettext.php
* SquirrelMail internal gettext functions
* Alternate to the system's built-in gettext. Relies on .po files (can't read
* .mo easily). Uses the session for caching (speed increase). Possible use in
* other PHP scripts? The only SM-specific thing is $sm_language, I think.
* @link http://www.php.net/gettext Original php gettext manual
* @copyright 1999-2020 The SquirrelMail Project Team
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version $Id: gettext.php 14840 2020-01-07 07:42:38Z pdontthink $
/** Almost everything requires global.php... */
require_once(SM_PATH .
'functions/global.php');
global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
$gettext_php_translateStrings, $gettext_php_loaded_language,
$gettext_php_short_circuit;
if (! isset
($gettext_php_loaded)) {
$gettext_php_loaded =
false;
if (! isset
($gettext_php_domain)) {
$gettext_php_domain =
'';
if (! isset
($gettext_php_dir)) {
if (! isset
($gettext_php_translateStrings)) {
$gettext_php_translateStrings =
array();
if (! isset
($gettext_php_loaded_language)) {
$gettext_php_loaded_language =
'';
if (! isset
($gettext_php_short_circuit)) {
$gettext_php_short_circuit =
false;
* Converts .po file into array and stores it in session.
* Used internally by _($str) function
* @internal function is used internally by functions/gettext.php code
global $squirrelmail_language, $gettext_php_translateStrings,
$gettext_php_domain, $gettext_php_dir, $gettext_php_loaded,
$gettext_php_loaded_language, $gettext_php_short_circuit;
* $squirrelmail_language gives 'en' for English, 'de' for German,
* etc. I didn't wanna use getenv or similar, but you easily could
* change my code to do that.
$gettext_php_translateStrings =
array();
$gettext_php_short_circuit =
false; /* initialization */
$filename =
$gettext_php_dir;
if (substr($filename, -
1) !=
'/')
$filename .=
$squirrelmail_language .
'/LC_MESSAGES/' .
$gettext_php_domain .
'.po';
$file =
@fopen($filename, 'r');
/* Uh-ho -- we can't load the file. Just fake it. :-)
This is also for English, which doesn't use translations */
$gettext_php_loaded =
true;
$gettext_php_loaded_language =
$squirrelmail_language;
/* Avoid fuzzy matching when we didn't load strings */
$gettext_php_short_circuit =
true;
if (preg_match('/^msgid "(.*)"$/', $line, $match)) {
while (preg_match('/^[ ]*"(.*)"[ ]*$/', $line, $match)) {
/* msgid "string string" */
} elseif (preg_match('/^msgstr "(.*)"$/', $line, $match)) {
$gettext_php_translateStrings[$key] =
'';
while (preg_match('/^[ ]*"(.*)"[ ]*$/', $line, $match)) {
$gettext_php_translateStrings[$key] .=
$match[1];
/* msgstr "string string" */
$gettext_php_translateStrings[$key] =
$match[1];
$gettext_php_translateStrings[$key] =
/* If there is no translation, just use the untranslated string */
if ($gettext_php_translateStrings[$key] ==
'') {
$gettext_php_translateStrings[$key] =
$key;
$gettext_php_loaded =
true;
$gettext_php_loaded_language =
$squirrelmail_language;
* Alternative php gettext function (short form)
* @link http://www.php.net/function.gettext
* @param string $str English string
* @return string translated string
global $gettext_php_loaded, $gettext_php_translateStrings,
$squirrelmail_language, $gettext_php_loaded_language,
$gettext_php_short_circuit;
if (! $gettext_php_loaded ||
$gettext_php_loaded_language !=
$squirrelmail_language) {
/* Try finding the exact string */
if (isset
($gettext_php_translateStrings[$str])) {
return $gettext_php_translateStrings[$str];
/* See if we should short-circuit */
if ($gettext_php_short_circuit) {
$gettext_php_translateStrings[$str] =
$str;
/* don't do fuzzy matching for strings with sprintf() formating */
/* Look for a string that is very close to the one we want
* Very computationally expensive */
foreach ($gettext_php_translateStrings as $k =>
$v) {
if ($newPercent >
$oldPercent) {
$oldPercent =
$newPercent;
/* Require 80% match or better
* Adjust to suit your needs */
/* Remember this so we don't need to search again */
$gettext_php_translateStrings[$str] =
$oldStr;
/* Remember this so we don't need to search again */
$gettext_php_translateStrings[$str] =
$str;
* Alternative php bindtextdomain function
* Sets path to directory containing domain translations
* @link http://www.php.net/function.bindtextdomain
* @param string $name gettext domain name
* @param string $dir directory that contains all translations
* @return string path to translation directory
global $gettext_php_domain, $gettext_php_dir, $gettext_php_loaded;
if ($gettext_php_domain !=
$name) {
$gettext_php_domain =
$name;
$gettext_php_loaded =
false;
if ($gettext_php_dir !=
$dir) {
$gettext_php_loaded =
false;
* Alternative php textdomain function
* Sets default domain name
* @link http://www.php.net/function.textdomain
* @param string $name gettext domain name
* @return string gettext domain name
global $gettext_php_domain, $gettext_php_loaded;
if ($name !=
false &&
$gettext_php_domain !=
$name) {
$gettext_php_domain =
$name;
$gettext_php_loaded =
false;
return $gettext_php_domain;
Documentation generated on Mon, 13 Jan 2020 04:24:40 +0100 by phpDocumentor 1.4.3