summaryrefslogtreecommitdiff
path: root/it_text.class
diff options
context:
space:
mode:
authorUrban Müller2007-07-26 13:02:24 +0000
committerUrban Müller2007-07-26 13:02:24 +0000
commit806a5297e7e99d455b97a4f0acaba2f40f470584 (patch)
treeb9fc43ef227da87d873cf3676c08c49fa0dc1240 /it_text.class
parentc3cba034c8009b65c25dd4ef5f54b18d9c8ee7d4 (diff)
downloaditools-806a5297e7e99d455b97a4f0acaba2f40f470584.tar.gz
itools-806a5297e7e99d455b97a4f0acaba2f40f470584.tar.bz2
itools-806a5297e7e99d455b97a4f0acaba2f40f470584.zip
renamed files for autoloader
Diffstat (limited to 'it_text.class')
-rw-r--r--it_text.class378
1 files changed, 378 insertions, 0 deletions
diff --git a/it_text.class b/it_text.class
new file mode 100644
index 0000000..cccd64c
--- /dev/null
+++ b/it_text.class
@@ -0,0 +1,378 @@
+<?php
+/*
+** $Id$
+**
+** ITools - the Internet Tools Library
+**
+** Copyright (C) 1995-2006 by the ITools Authors.
+** This program is free software; you can redistribute it and/or
+** modify it under the terms of either the GNU General Public License
+** or the GNU Lesser General Public License, as published by the Free
+** Software Foundation. See http://www.gnu.org/licenses/ for details.
+**
+*/
+
+class it_text
+{
+ var $defaultlanguage; # Preferred language (without cookie)
+ var $actlanguage; # Selected language
+ var $actlanguagename; # Name of selected language
+ var $languages = array(); # Active languages
+ var $languages_available = array(); # Available languages
+ var $language_failsafe; # First available language. Must be complete (used as fallback)
+ var $statictext = array(); # Text array, read from php file on init
+ var $unknown_labels = array(); # array for spooling unknown labels in debug mode
+
+/**
+ * Constructor
+ * @param $p['cookiename'] optional cookie name (default: 'LANGUAGE')
+ * @param $p['debug'] optional debug mode: display label of unknown texts, set to 'label' to always display labels instead of actual text
+ * @param $p['forcelanguage'] optional language to use instead of user's prefered language
+ * @param $p['phpfile'] optional texts file, defaults to service's phpinclude/texts.php
+ */
+function it_text($p = null)
+{
+ $this->p = (array)$p + array('cookiename' => 'LANGUAGE', 'debug' => false, 'phpfile' => $GLOBALS['ULTRAHOME'] . '/phpinclude/texts.php');
+
+ # Read $this->statictext from php file if it's not defined yet
+ if (!$this->statictext)
+ include($this->p['phpfile']);
+
+ # Get array of supported languages and their names
+ foreach(array_keys($this->statictext['_']) as $code)
+ {
+ # If a language's name in '_' is unset, ignore it
+ if ($languagename = $this->statictext['_'][$code])
+ {
+ $this->languages_available[$code] = $languagename;
+
+ # Only use a language in browser/cookie detection below if it's not diasbled by a leading '-'
+ if (substr($languagename, 0, 1) != '-')
+ {
+ $this->languages[$code] = $languagename;
+ if (!isset($this->language_failsafe))
+ $this->language_failsafe = $code;
+ }
+ }
+ }
+
+ # Set our default language according to browser preference
+ if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
+ {
+ foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang)
+ {
+ if (isset($this->languages[$lang]))
+ {
+ $this->defaultlanguage = $lang;
+ debug("Got language from browser: {$this->defaultlanguage}", 6);
+ break;
+ }
+ else
+ {
+ $short = substr($lang, 0, 2);
+ if (isset($this->languages[$short]))
+ {
+ $this->defaultlanguage = $short;
+ debug("Got language family from browser: {$this->defaultlanguage}", 6);
+ break;
+ }
+ }
+ }
+ if (count($this->languages) == 0)
+ debug("No active languages found!", 5);
+ }
+
+ # If no language matched, use failsafe language
+ if (!$this->languages[$this->defaultlanguage])
+ $this->defaultlanguage = $this->language_failsafe;
+
+ # If a cookie is set, use its value as our active language
+ if (isset($_COOKIE[$this->p['cookiename']]) && ($this->languages[$cookie = $_COOKIE[$this->p['cookiename']]]))
+ {
+ $this->actlanguage = $cookie;
+ debug("Got language from cookie: {$this->actlanguage}", 6);
+ }
+
+ # If the cookie supplied no valid language, we use our calculated default language
+ if (!isset($this->actlanguage))
+ $this->actlanguage = $this->defaultlanguage;
+
+ # If the user wants something else, comply
+ if (isset($this->p['forcelanguge']) && isset($this->languages[$this->p['forcelanguage']]))
+ $this->actlanguage = $this->p['forcelanguage'];
+
+ # And finally, record the name of our active language.
+ $this->actlanguagename = $this->languages[$this->actlanguage];
+
+ # Make this object available under $GLOBALS['it_text'], or add my texts to $GLOBALS['it_text'] if it exists
+ if (!$GLOBALS['it_text'])
+ $GLOBALS['it_text'] =& $this;
+ else
+ $GLOBALS['it_text']->statictext += $this->statictext;
+
+ debug("Used language is {$this->actlanguagename}, default language is {$this->defaultlanguage}.", 6);
+}
+
+/**
+ * Instanciate singleton if necessary
+ */
+function init()
+{
+ if (!$GLOBALS['it_text'])
+ new it_text;
+}
+
+/**
+ * Return a text in the selected language.
+ * @param $label Label of text to return
+ * @param $raw Optional unused obsolete parameter
+ * @param $language Optional language to return text in.
+ * @return Localized text string
+ */
+function text($label, $raw = null, $language = null)
+{
+ if ($this->p['debug'] === 'label')
+ return $label;
+
+ if (!isset($language))
+ $language = $this->actlanguage;
+
+ if ($this->statictext[$label][$language] !== '')
+ return $this->statictext[$label][$language];
+
+ $this->unknown_labels[] = $label;
+ return $this->p['debug'] ? "<blink>$label ($language)</blink>" : $this->statictext[$label][$this->language_failsafe];
+}
+
+
+/**
+ * Return a text in the selected language, with correctly encoded umlauts.
+ * Replaces variables of the form {obj.var} with value, e.g. {user.name}
+ * NOTE: Invalid object names or non-existing variables are simply deleted.
+ * @param $label Label of text to return
+ * @param $values Associative array containing values to fill in
+ * @param $language Optional language to return text in.
+ * @return Localized text string with variables replaced by their values
+ */
+function etext($label, $values = null, $language = null)
+{
+ return $this->transmogrify($this->text($label, null, $language), $values);
+}
+
+
+/**
+ * Change language
+ * @param $language New language to set
+ * @param $setcookie Optional flag if a cookie is to be set (default: true)
+ */
+function set_language($language, $setcookie = true)
+{
+ # If $language is unknown, delete the cookie
+ if ($this->languages_available[$language])
+ {
+ if (!$this->languages[$language])
+ debug('Selected an existing but currently disabled language!', 6);
+ $this->actlanguage = $language;
+ if ($setcookie)
+ SetCookie($this->p['cookiename'], $this->actlanguage, time() + 31536000, '/'); # 1 year
+ }
+ else
+ {
+ $this->actlanguage = $this->defaultlanguage;
+ if ($setcookie)
+ SetCookie($this->p['cookiename'], '', time() + 31536000, '/');
+ }
+}
+
+
+/**
+ * Get active language
+ * @return currently active language
+ */
+function get_language()
+{
+ return $this->actlanguage;
+}
+
+
+/**
+ * Get default language
+ * @return default language
+ */
+function get_default_language()
+{
+ return $this->defaultlanguage;
+}
+
+
+/**
+ * Check if a text entry for a specific label exists
+ * @param $label Label to check
+ * @return true if text exists in actual (or supplied) language, false otherwise.
+ */
+function text_exists($label, $language = null)
+{
+ return isset($this->statictext[$label][isset($language) ? $language : $this->actlanguage]);
+}
+
+
+/**
+ * Create / overwrite a text in the selected language. Call dump_php() to make the change permanent.
+ * @param $label Label of text to change
+ * @param $text New text to set
+ * @param $language Optional language that is to be manipulated
+ */
+function set($label, $text = null, $language = null)
+{
+ if (!isset($language))
+ $language = $this->actlanguage;
+
+ $this->statictext[$label][$language] = $text;
+}
+
+
+/**
+ * Replaces variables of the form {obj.var} with value, e.g. {user.name}
+ * NOTE: Invalid object names or non-existing variables are simply deleted.
+ */
+function transmogrify($text, $values = null)
+{
+ while (preg_match('/{([\w.]+)}/', $text, $regs))
+ {
+ $path = explode('.', $regs[1]);
+ if ($values)
+ $value =& $values;
+ else
+ $value =& $GLOBALS;
+
+ # Recurse into nested arrays/object members
+ foreach ($path as $key)
+ {
+ if (is_object($value))
+ $value =& $value->$key;
+ else
+ $value =& $value[$key];
+ }
+
+ $text = str_replace($regs[0], $value, $text);
+ }
+
+ return $text;
+}
+
+
+/**
+ * when running it_text in debug mode, you can use this to die
+ * in case there were unknown labels with a list of all unknown labels
+ */
+function checkout_unknown_labels()
+{
+ if ($this->unknown_labels)
+ it::fatal('No text found for labels: ' . implode(',', $this->unknown_labels));
+}
+
+
+/**
+ * Replaces special chars with their TeX equivalent
+ * @param $text Text to convert from ISO-8859-1 to TeX encoding
+ * @param $convertlinebreaks Whether to convert LFs to TeX linebreaks
+ * @return TeX encoded text to be inserted into TeX template
+ */
+function texify($text, $convertlinebreaks = true)
+{
+ $translation = array
+ (
+ 'À' => '{\`A}',
+ 'Á' => "{\'A}",
+ 'Â' => '{\^A}',
+ 'Ã' => '{\~A}',
+ 'Ä' => '{\"A}',
+ 'Ç' => '{\c C}',
+ 'È' => '{\`E}',
+ 'É' => "{\'E}",
+ 'Ê' => '{\^E}',
+ 'Ë' => '{\"E}',
+ 'Ì' => '{\`I}',
+ 'Í' => "{\'I}",
+ 'Î' => '{\^I}',
+ 'Ï' => '{\"I}',
+ 'Ñ' => '{\~N}',
+ 'Ò' => '{\`O}',
+ 'Ó' => "{\'O}",
+ 'Ô' => '{\^O}',
+ 'Õ' => '{\~O}',
+ 'Ö' => '{\"O}',
+ 'Ù' => '{\`U}',
+ 'Ú' => "{\'U}",
+ 'Û' => '{\^U}',
+ 'Ü' => '{\"U}',
+ 'ß' => '{\ss}',
+ 'à' => '{\`a}',
+ 'á' => "{\'a}",
+ 'â' => '{\^a}',
+ 'ã' => '{\~a}',
+ 'ä' => '{\"a}',
+ 'ç' => '{\c c}',
+ 'è' => '{\`e}',
+ 'é' => "{\'e}",
+ 'ê' => '{\^e}',
+ 'ë' => '{\"e}',
+ 'ì' => '{\`i}',
+ 'í' => "{\'i}",
+ 'î' => '{\^i}',
+ 'ï' => '{\"i}',
+ 'ñ' => '{\~n}',
+ 'ò' => '{\`o}',
+ 'ó' => "{\'o}",
+ 'ô' => '{\^o}',
+ 'õ' => '{\~o}',
+ 'ö' => '{\"o}',
+ 'ù' => '{\`u}',
+ 'ú' => "{\'u}",
+ 'û' => '{\^u}',
+ 'ü' => '{\"u}',
+ 'ÿ' => '{\"y}',
+ '"' => "''",
+ '\\' => '/',
+ '%' => '\%',
+ '$' => '\$',
+ '#' => '\#',
+ '&' => '\&',
+ '_' => '\_',
+ '^' => '',
+ );
+
+ if ($convertlinebreaks)
+ $translation["\n"] = "\\\\\n";
+
+ return strtr($text, $translation);
+}
+
+/**
+ * Re-create php text file from $this->statictext
+ * @return true if successful, false if not (usually if file is not writeable by user www)
+ */
+function dump_php()
+{
+ $result = false;
+
+ # Special sorting: natural, but _ is the first entry
+ $keys = array_keys($this->statictext);
+ natsort($keys);
+ foreach($keys as $key)
+ $text[$key] = $this->statictext[$key];
+ $this->statictext = array_merge(array('_' => $text['_']), $text);
+
+ $oldmask = umask(002);
+ if ($f = fopen($this->p['phpfile'], 'w'))
+ {
+ $result = (fputs($f, '<?php $this->statictext = ' . strtr(var_export($this->statictext, true), array("=> \n array (" => "=> array(", "array (\n '_'" => "array(\n'_'", "\n ),\n " => "\n),\n", "\n ),\n" => "\n)\n")) . ";\n?>\n") !== false);
+ fclose($f);
+ }
+
+ umask($oldmask);
+ return $result;
+}
+
+} /* End class it_text */
+?>