Move nlsconfig manipulation code inside of this new object.
Cache nlsconfig info about the current language - only need to load
nls.php config file now if manipulating an entire config entry.
*/
static public function stripAccessKey($label)
{
- $multibyte = isset($GLOBALS['registry']->nlsconfig['multibyte'][$GLOBALS['registry']->getLanguageCharset()]);
- return preg_replace('/_([A-Za-z])/',
- $multibyte && preg_match('/[\x80-\xff]/', $label) ? '' : '\1',
- $label);
+ return preg_replace('/_([A-Za-z])/', $GLOBALS['registry']->nlsconfig->curr_multibyte && preg_match('/[\x80-\xff]/', $label) ? '' : '\1', $label);
}
/**
return $stripped_label;
}
- if (isset($GLOBALS['registry']->nlsconfig['multibyte'][$GLOBALS['registry']->getLanguageCharset()])) {
+ if ($GLOBALS['registry']->nlsconfig->curr_multibyte) {
/* Prefix parenthesis with the UTF-8 representation of the LRO
* (Left-to-Right-Override) Unicode codepoint U+202D. */
- return $stripped_label . "\xe2\x80\xad" . '(<span class="accessKey">'
- . strtoupper($accessKey) . '</span>' . ')';
+ return $stripped_label . "\xe2\x80\xad" .
+ '(<span class="accessKey">' . strtoupper($accessKey) .
+ '</span>' . ')';
}
return str_replace('_' . $accessKey, '<span class="accessKey">' . $accessKey . '</span>', $label);
return $apps;
case 'list-horde-languages':
- return array_map(create_function('$val', 'return preg_replace(array("/&#x([0-9a-f]{4});/ie", "/(&[^;]+;)/e"), array("Horde_String::convertCharset(pack(\"H*\", \"$1\"), \"ucs-2\", \"UTF-8\")", "Horde_String::convertCharset(html_entity_decode(\"$1\", ENT_COMPAT, \"iso-8859-1\"), \"iso-8859-1\", \"UTF-8\")"), $val);'), $GLOBALS['registry']->nlsconfig['languages']);
+ return array_map(create_function('$val', 'return preg_replace(array("/&#x([0-9a-f]{4});/ie", "/(&[^;]+;)/e"), array("Horde_String::convertCharset(pack(\"H*\", \"$1\"), \"ucs-2\", \"UTF-8\")", "Horde_String::convertCharset(html_entity_decode(\"$1\", ENT_COMPAT, \"iso-8859-1\"), \"iso-8859-1\", \"UTF-8\")"), $val);'), $GLOBALS['registry']->nlsconfig->languages);
case 'list-blocks':
$collection = Horde_Block_Collection::singleton();
}
if (empty($params['locales'])) {
- $key_list = array_keys($GLOBALS['registry']->nlsconfig['spelling']);
+ $key_list = array_keys($GLOBALS['registry']->nlsconfig->spelling);
asort($key_list, SORT_LOCALE_STRING);
$params['locales'] = array();
foreach ($key_list as $lcode) {
- $params['locales'][] = array('l' => $GLOBALS['registry']->nlsconfig['languages'][$lcode], 'v' => $lcode);
+ $params['locales'][] = array('l' => $GLOBALS['registry']->nlsconfig->languages[$lcode], 'v' => $lcode);
}
}
));
}
- $t->set('addimg', Horde::img(isset($GLOBALS['registry']->nlsconfig['rtl'][$GLOBALS['language']]) ? 'lhand.png' : 'rhand.png', Horde_Core_Translation::t("Add source")));
- $t->set('removeimg', Horde::img(isset($GLOBALS['registry']->nlsconfig['rtl'][$GLOBALS['language']]) ? 'rhand.png' : 'lhand.png', Horde_Core_Translation::t("Remove source")));
+ $t->set('addimg', Horde::img($GLOBALS['registry']->nlsconfig->curr_rtl ? 'lhand.png' : 'rhand.png', Horde_Core_Translation::t("Add source")));
+ $t->set('removeimg', Horde::img($GLOBALS['registry']->nlsconfig->curr_rtl ? 'rhand.png' : 'lhand.png', Horde_Core_Translation::t("Remove source")));
if (empty($data['no_up'])) {
$t->set('upimg', Horde::img('nav/up.png', Horde_Core_Translation::t("Move up")));
Horde::url($registry->get('webroot', 'horde') . '/services/language.php', false, -1));
$html .= '<input type="hidden" name="url" value="' . @htmlspecialchars(Horde::selfUrl(false, false, true)) . '" />';
$html .= '<select name="new_lang" onchange="document.language.submit()">';
- foreach ($registry->nlsconfig['languages'] as $key => $val) {
+ foreach ($registry->nlsconfig->languages as $key => $val) {
$sel = ($key == $session->get('horde', 'language')) ? ' selected="selected"' : '';
$html .= "<option value=\"$key\"$sel>$val</option>";
}
throw new Horde_Exception('SimpleXML not available.');
}
- if (isset($GLOBALS['registry']->nlsconfig['charsets'][$GLOBALS['language']])) {
- $this->_charset = $GLOBALS['registry']->nlsconfig['charsets'][$GLOBALS['language']];
+ if ($charset = $GLOBALS['registry']->nlsconfig->curr_charset) {
+ $this->_charset = $charset;
}
switch ($source) {
/* Sort to match explicitly set positions. */
ksort($this->_menu);
- if (!empty($registry->nlsconfig['rtl'][$GLOBALS['language']])) {
+ if ($registry->nlsconfig->curr_rtl) {
$this->_menu = array_reverse($this->_menu);
}
/**
* NLS configuration.
*
- * @var array
+ * @var Horde_Registry_Nlsconfig
*/
- public $nlsconfig = array();
+ public $nlsconfig;
/**
* Stack of in-use applications.
/* Always need to load applications information. */
$this->_loadApplications();
- /* Load the language configuration. */
- $this->nlsconfig = Horde::loadConfiguration('nls.php', 'horde_nls_config', 'horde');
+ /* Initialize language configuration object. */
+ $this->nlsconfig = new Horde_Registry_Nlsconfig();
/* Initialize the localization routines and variables. */
$this->setLanguageEnvironment(null, 'horde');
$this->applications = $this->_apiList = $this->_confCache = $this->_interfaces = array();
unset($this->_apis);
+ $GLOBALS['session']->remove('horde', 'nls/');
$GLOBALS['session']->remove('horde', 'registry/');
$this->_saveCache('apis');
$this->_saveCache('app');
*/
public function getLanguageCharset()
{
- return empty($this->nlsconfig['charsets'][$GLOBALS['language']])
- ? 'ISO-8859-1'
- : $this->nlsconfig['charsets'][$GLOBALS['language']];
+ return ($charset = $this->nlsconfig->curr_charset)
+ ? $charset
+ : 'ISO-8859-1';
}
/**
return $charset;
}
- return isset($this->nlsconfig['emails'][$GLOBALS['language']])
- ? $this->nlsconfig['emails'][$GLOBALS['language']]
- : (isset($this->nlsconfig['charsets'][$GLOBALS['language']]) ? $this->nlsconfig['charsets'][$GLOBALS['language']] : 'ISO-8859-1');
+ return ($charset = $this->nlsconfig->curr_emails)
+ ? $charset
+ : $this->getLanguageCharset();
}
/**
}
/* Check if the user selected a language from the login screen */
- if (!empty($lang) && $this->isValidLang($lang)) {
+ if (!empty($lang) && $this->nlsconfig->validLang($lang)) {
return basename($lang);
}
}
/* Use site-wide default, if one is defined */
- if (!empty($this->nlsconfig['defaults']['language'])) {
- return basename($this->nlsconfig['defaults']['language']);
+ if ($language = $this->nlsconfig->curr_default) {
+ return basename($this->nlsconfig->curr_default);
}
/* Try browser-accepted languages. */
}
$lang = $this->_mapLang(trim($lang));
- if ($this->isValidLang($lang)) {
+ if ($this->nlsconfig->validLang($lang)) {
$language = $lang;
break;
}
* ll_LL, followed by just ll. */
if (!isset($partial_lang)) {
$ll_LL = Horde_String::lower(substr($lang, 0, 2)) . '_' . Horde_String::upper(substr($lang, 0, 2));
- if ($this->isValidLang($ll_LL)) {
+ if ($this->nlsconfig->validLang($ll_LL)) {
$partial_lang = $ll_LL;
} else {
$ll = $this->_mapLang(substr($lang, 0, 2));
- if ($this->isValidLang($ll)) {
+ if ($this->nlsconfig->validLang($ll)) {
$partial_lang = $ll;
}
}
}
/**
- * Determines whether the supplied language is valid.
- *
- * @param string $language The abbreviated name of the language.
- *
- * @return boolean True if the language is valid, false if it's not
- * valid or unknown.
- */
- public function isValidLang($language)
- {
- return !empty($this->nlsconfig['languages'][$language]);
- }
-
- /**
* Sets the language.
*
* @param string $lang The language abbreviation.
*/
public function setLanguage($lang = null)
{
- if (empty($lang) || !$this->isValidLang($lang)) {
+ if (empty($lang) || !$this->nlsconfig->validLang($lang)) {
$lang = $this->preferredLang();
}
$trans_lang .= '_' . Horde_String::upper($lang_parts[1]);
}
- return empty($this->nlsconfig['aliases'][$trans_lang])
+ return empty($this->nlsconfig->aliases[$trans_lang])
? $trans_lang
- : $this->nlsconfig['aliases'][$trans_lang];
+ : $this->nlsconfig->aliases[$trans_lang];
}
}
--- /dev/null
+<?php
+/**
+ * Interface to NLS configuration.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package Core
+ */
+class Horde_Registry_Nlsconfig
+{
+ /**
+ * The cached configuration data.
+ *
+ * @var array
+ */
+ protected $_config;
+
+ /**
+ */
+ public function __get($name)
+ {
+ /* These entries can be cached in the session. */
+ $cached = array(
+ 'curr_charset',
+ 'curr_default',
+ 'curr_emails',
+ 'curr_multibyte',
+ 'curr_rtl'
+ );
+
+ if (in_array($name, $cached) &&
+ $GLOBALS['session']->exists('horde', 'nls/' . $name)) {
+ return $GLOBALS['session']->get('horde', 'nls/' . $name);
+ }
+
+ if (!isset($this->_config)) {
+ $this->_config = Horde::loadConfiguration('nls.php', 'horde_nls_config', 'horde');
+ }
+
+ switch ($name) {
+ case 'aliases':
+ case 'charsets':
+ case 'encodings':
+ case 'emails':
+ case 'languages':
+ case 'multibyte':
+ case 'rtl':
+ case 'spelling':
+ $ret = isset($this->_config[$name])
+ ? $this->_config[$name]
+ : array();
+ break;
+
+ case 'charsets_sort':
+ $ret = $this->charsets;
+ natcasesort($ret);
+ break;
+
+ case 'curr_charset':
+ /* Return charset for the current language. */
+ $ret = isset($this->_config->charsets[$GLOBALS['language']])
+ ? $this->_config->charsets[$GLOBALS['language']]
+ : null;
+ break;
+
+ case 'curr_default':
+ /* The default langauge, as specified by the config file. */
+ $ret = isset($this->_config->defaults['language'])
+ ? $this->_config->defaults['language']
+ : null;
+ break;
+
+ case 'curr_emails':
+ /* Return e-mail charset for the current language. */
+ $ret = isset($this->_config->emails[$GLOBALS['language']])
+ ? $this->_config->emails[$GLOBALS['language']]
+ : null;
+ break;
+
+ case 'curr_multibyte':
+ /* Is the current language charset multibyte? */
+ $ret = isset($this->_config['multibyte'][$GLOBALS['registry']->getLanguageCharset()]);
+ break;
+
+ case 'curr_rtl':
+ /* Is the current language RTL? */
+ $ret = isset($this->_config['rtl'][$GLOBALS['language']]);
+ break;
+
+ case 'encodings_sort':
+ $ret = $this->encodings;
+ asort($ret);
+ break;
+
+ default:
+ $ret = null;
+ break;
+ }
+
+ if (in_array($name, $cached)) {
+ $GLOBALS['session']->set('horde', 'nls/' . $name, $ret);
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Check whether a language string is valid.
+ *
+ * @param string $lang The language to check.
+ *
+ * @return boolean Whether the language is valid.
+ */
+ public function validLang($lang)
+ {
+ if (!$GLOBALS['session']->exists('horde', 'nls/valid_' . $lang)) {
+ $GLOBALS['session']->set('horde', 'nls/valid_' . $lang, isset($this->languages[$lang]));
+ }
+
+ return $GLOBALS['session']->get('horde', 'nls/valid_' . $lang);
+ }
+
+}
{
$css_list = array('screen.css');
- if (isset($GLOBALS['registry']->nlsconfig['rtl'][$GLOBALS['language']])) {
+ if ($GLOBALS['registry']->nlsconfig->curr_rtl) {
$css_list[] = 'rtl.css';
}
<file name="Api.php" role="php" />
<file name="Application.php" role="php" />
<file name="Caller.php" role="php" />
+ <file name="Nlsconfig.php" role="php" />
</dir> <!-- /lib/Horde/Registry -->
<dir name="Script">
<file name="Files.php" role="php" />
<install as="Horde/Registry/Api.php" name="lib/Horde/Registry/Api.php" />
<install as="Horde/Registry/Application.php" name="lib/Horde/Registry/Application.php" />
<install as="Horde/Registry/Caller.php" name="lib/Horde/Registry/Caller.php" />
+ <install as="Horde/Registry/Nlsconfig.php" name="lib/Horde/Registry/Nlsconfig.php" />
<install as="Horde/Script/Files.php" name="lib/Horde/Script/Files.php" />
<install as="Horde/Themes/Cache.php" name="lib/Horde/Themes/Cache.php" />
<install as="Horde/Themes/Css.php" name="lib/Horde/Themes/Css.php" />
'type' => 'enum',
'enum' => array_merge(
array('' => _("Default")),
- $GLOBALS['registry']->nlsconfig['encodings']
+ $GLOBALS['registry']->nlsconfig->encodings_sort
),
'desc' => _("Default charset for sending e-mail messages:")
);
case 'language':
if (!$prefs->isLocked('language')) {
- $ui->override['language'] = $registry->nlsconfig['languages'];
+ $ui->override['language'] = $registry->nlsconfig->languages;
array_unshift($ui->override['language'], _("Default"));
}
$ui->override['timezone'] = Horde_Nls::getTimezones();
array_unshift($ui->override['timezone'], _("Default"));
}
-
- if (!$prefs->isLocked('sending_charset')) {
- asort($registry->nlsconfig['encodings']);
- }
break;
case 'remote':
/* Build the <select> widget containing the available languages. */
if (!$is_auth && !$prefs->isLocked('language')) {
$langs = array();
- foreach ($registry->nlsconfig['languages'] as $key => $val) {
+ foreach ($registry->nlsconfig->languages as $key => $val) {
$langs[] = array(
'sel' => ($key == $GLOBALS['language']),
'val' => $key,
$vars = Horde_Variables::getDefaultVariables();
-$rtl = isset($registry->nlsconfig['rtl'][$language]);
+$rtl = $registry->nlsconfig->curr_rtl;
$title = _("Help");
$show = isset($vars->show)
? Horde_String::lower($vars->show)
$ajax_url = Horde::getServiceLink('ajax', 'horde');
$ajax_url->pathInfo = 'sidebarUpdate';
- $rtl = intval(isset($registry->nlsconfig['rtl'][$language]));
+ $rtl = intval($registry->nlsconfig->curr_rtl);
$show_sidebar = !isset($_COOKIE['horde_sidebar_expanded']) || $_COOKIE['horde_sidebar_expanded'];
$width = intval($prefs->getValue('sidebar_width'));
$t->set('charset_label', Horde::label('charset', _("C_harset")));
$t->set('charset_tabindex', ++$tabindex);
$charset_array = array();
- asort($registry->nlsconfig['encodings']);
- foreach (array_merge(array('' => _("Default")), $registry->nlsconfig['encodings']) as $encoding => $label) {
+ $enc_list = $registry->nslconfig->encodings;
+ asort($enc_list);
+ foreach (array_merge(array('' => _("Default")), $enc_list) as $encoding => $label) {
$charset_array[] = array(
'label' => $label,
'selected' => (strcasecmp($encoding, $charset) === 0),
);
// default message character set
+
$_prefs['default_msg_charset'] = array(
- 'value' => isset($GLOBALS['registry']->nlsconfig['emails'][$GLOBALS['language']])
- ? $GLOBALS['registry']->nlsconfig['emails'][$GLOBALS['language']]
- : (isset($GLOBALS['registry']->nlsconfig['charsets'][$GLOBALS['language']])
- ? $GLOBALS['registry']->nlsconfig['charsets'][$GLOBALS['language']]
+ 'value' => $GLOBALS['registry']->getEmailCharset()
+ ? $GLOBALS['registry']->getEmailCharset()
+ : ($GLOBALS['registry']->getLanguageCharset()
+ ? $GLOBALS['registry']->getLanguageCharset()
: ''),
'type' => 'enum',
'enum' => array_merge(
- array('' => _("Default (US-ASCII)")), $GLOBALS['registry']->nlsconfig['encodings']
+ array('' => _("Default (US-ASCII)")), $GLOBALS['registry']->nlsconfig->encodings_sort
),
'desc' => _("The default charset for messages with no charset information:"),
'help' => 'prefs-default_msg_charset'
$ui->nobuttons = true;
}
break;
-
- case 'viewing':
- /* Sort encodings. */
- if (!$prefs->isLocked('default_msg_charset')) {
- asort($registry->nlsconfig['encodings']);
- }
- break;
}
/* Hide appropriate prefGroups. */
/* Generate paging links. */
if ($pageOb['pagecount']) {
- $rtl = !empty($registry->nlsconfig['rtl'][$language]);
+ $rtl = $registry->nlsconfig->curr_rtl;
if ($pageOb['page'] == 1) {
$pages_first = Horde::img($rtl ? 'nav/last-grey.png' : 'nav/first-grey.png');
$pages_prev = Horde::img($rtl ? 'nav/right-grey.png' : 'nav/left-grey.png');
$n_template->set('back_to', Horde::widget($mailbox_url, sprintf(_("Back to %s"), $h_page_label), 'widget', '', '', sprintf(_("Bac_k to %s"), $h_page_label), true));
-$rtl = !empty($registry->nlsconfig['rtl'][$language]);
+$rtl = $registry->nlsconfig->curr_rtl;
if (Horde_Util::nonInputVar('prev_url')) {
$n_template->set('prev', Horde::link($prev_url, _("Previous Message")));
$n_template->set('prev_img', Horde::img($rtl ? 'nav/right.png' : 'nav/left.png', $rtl ? '>' : '<'));
}
if ($this->indent) {
if ($this->lastChild) {
- $html .= Horde::img(empty($GLOBALS['registry']->nlsconfig['rtl'][$GLOBALS['language']]) ? 'tree/joinbottom.png' : 'tree/rev-joinbottom.png', '\\');
+ $html .= Horde::img($GLOBALS['registry']->nlsconfig->curr_rtl ? 'tree/joinbottom.png' : 'tree/rev-joinbottom.png', '\\');
} else {
- $html .= Horde::img(empty($GLOBALS['registry']->nlsconfig['rtl'][$GLOBALS['language']]) ? 'tree/join.png' : 'tree/rev-join.png', '+');
+ $html .= Horde::img($GLOBALS['registry']->nlsconfig->curr_rtl ? 'tree/join.png' : 'tree/rev-join.png', '+');
}
}
}
/* Build the charset options. */
- $charsets = $registry->nlsconfig['encodings'];
- asort($charsets);
- $all_charsets = $registry->nlsconfig['charsets'];
- natcasesort($all_charsets);
- foreach ($all_charsets as $charset) {
+ $charsets = $registry->nlsconfig->encodings_sort;
+ foreach ($registry->nlsconfig->charsets as $charset) {
if (!isset($charsets[$charset])) {
$charsets[$charset] = $charset;
}