summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Staudacher <andy.st@gmail.com>2009-02-02 06:32:42 +0000
committerAndy Staudacher <andy.st@gmail.com>2009-02-02 06:32:42 +0000
commit4842df9dc9c489a3c05667410242e91bf45e843d (patch)
tree113fc13d2c3f7c934bf19a294599cdf243ae72c2
parente3b5eca50ef1e421b2f4b97309882df4dd9f5855 (diff)
Do 1 DB query for l10n per HTTP request (per locale), not one per t() call.
-rw-r--r--core/libraries/I18n.php30
1 files changed, 18 insertions, 12 deletions
diff --git a/core/libraries/I18n.php b/core/libraries/I18n.php
index 04176b75..1419357e 100644
--- a/core/libraries/I18n.php
+++ b/core/libraries/I18n.php
@@ -50,6 +50,8 @@ function t2($singular, $plural, $count, $options=array()) {
class I18n_Core {
private $_config = array();
+ private $_cache = array();
+
private static $_instance;
private function __construct($config) {
@@ -96,21 +98,25 @@ class I18n_Core {
}
private function lookup($locale, $message) {
- // TODO: Load data from locale file instead of the DB.
+ if (!isset($this->_cache[$locale])) {
+ $this->_cache[$locale] = array();
+ // TODO: Load data from locale file instead of the DB.
+ foreach (Database::instance()
+ ->select("key", "translation")
+ ->from("incoming_translations")
+ ->where(array("locale" => $locale))
+ ->get()
+ ->as_array() as $row) {
+ $this->_cache[$locale][$row->key] = unserialize($row->translation);
+ }
+ }
// If message is an array (plural forms), use the first form as message id.
$key = is_array($message) ? array_shift($message) : $message;
- $entry = Database::instance()
- ->select("translation")
- ->from("incoming_translations")
- ->where(array("key" => md5($key, true),
- "locale" => $locale))
- ->limit(1)
- ->get()
- ->current();
-
- if ($entry) {
- return unserialize($entry->translation);
+ $key = md5($key, true);
+
+ if (isset($this->_cache[$locale][$key])) {
+ return $this->_cache[$locale][$key];
} else {
return null;
}