|
@@ -17,6 +17,13 @@ namespace Memcache_UI\Core {
|
|
|
*/
|
|
|
protected $gc = NULL;
|
|
|
|
|
|
+ /**
|
|
|
+ * Locale with encoding
|
|
|
+ *
|
|
|
+ * This is a static because a page runs for only one locale.
|
|
|
+ */
|
|
|
+ protected static $locale = 'en_US.UTF8';
|
|
|
+
|
|
|
/**
|
|
|
* Logging level, as per RFC5424
|
|
|
*
|
|
@@ -55,7 +62,8 @@ namespace Memcache_UI\Core {
|
|
|
protected $user = FALSE;
|
|
|
|
|
|
function __construct() {
|
|
|
- $this->getTidy(); // Needed to check extension
|
|
|
+ $this->initLocale(); // Check extension and initialize locale
|
|
|
+ $this->getTidy(); // Needed to check optional extension
|
|
|
}
|
|
|
|
|
|
function __destruct() {
|
|
@@ -176,7 +184,7 @@ namespace Memcache_UI\Core {
|
|
|
}
|
|
|
}
|
|
|
if (!$notified && $this->tidy && !extension_loaded('tidy')) {
|
|
|
- $this->setMessage(strtr('Extension @tidy requested but missing: skipping', array(
|
|
|
+ $this->setMessage(t('Extension @tidy requested but missing: output formatting unavailable.', array(
|
|
|
'@tidy' => 'tidy',
|
|
|
)), LOG_WARNING);
|
|
|
$notified = TRUE;
|
|
@@ -187,6 +195,51 @@ namespace Memcache_UI\Core {
|
|
|
return $this->tidy;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Initialize the locale based on the user Accept-Language header.
|
|
|
+ *
|
|
|
+ * @TODO support "xx" form, and not just "xx_YY".
|
|
|
+ * @TODO support more platforms. UNIX and MacOS X do not handle locales like Linux
|
|
|
+ *
|
|
|
+ * @link @link http://www.php.net/manual/fr/function.bind-textdomain-codeset.php#42631
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ protected function initLocale() {
|
|
|
+ static $notified = FALSE;
|
|
|
+ if (!$notified && !extension_loaded('intl')) {
|
|
|
+ // Do not invoke t() before initializing locale
|
|
|
+ $this->setMessage(strtr('Extension @intl requested but missing: translations unavailable.', array(
|
|
|
+ '@intl' => 'intl',
|
|
|
+ )), LOG_WARNING);
|
|
|
+ }
|
|
|
+
|
|
|
+ $locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
|
|
+ $this->setMessage(strtr('Requested locale: @locale', array('@locale' => $locale)), LOG_DEBUG);
|
|
|
+ if (!empty($locale)) {
|
|
|
+ $matches = array();
|
|
|
+ $count = preg_match('/^([a-z]{2}_[A-Z]{2})(\.(\w+))*/', $locale, $matches);
|
|
|
+ if ($count) {
|
|
|
+ $count = count($matches);
|
|
|
+ }
|
|
|
+ $locale = $count >= 2
|
|
|
+ ? $matches[1]
|
|
|
+ : 'en_US';
|
|
|
+ $codeset = $count >= 4
|
|
|
+ ? $matches[3]
|
|
|
+ : 'UTF8';
|
|
|
+ self::$locale = $locale . '.' . $codeset;
|
|
|
+ setlocale(LC_ALL, self::$locale);
|
|
|
+
|
|
|
+ $domain = 'messages';
|
|
|
+ $translation_path = 'locale';
|
|
|
+ bindtextdomain($domain, $translation_path);
|
|
|
+ textdomain($domain);
|
|
|
+ bind_textdomain_codeset($domain, 'UTF8');
|
|
|
+ $this->setMessage(self::t('Locale: @locale', array('@locale' => self::$locale)), LOG_DEBUG);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Add a message to the messages list if it is above the current logging level.
|
|
|
*
|
|
@@ -205,5 +258,17 @@ namespace Memcache_UI\Core {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Wrapper to combine gettext translation and parameter substitution.
|
|
|
+ *
|
|
|
+ * @param string $message
|
|
|
+ * @param array $args
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ static function t($message, $args = array()) {
|
|
|
+ return strtr(gettext($message), $args);
|
|
|
+ }
|
|
|
}
|
|
|
}
|