[opw-web] Optimize skin variables and localization
- From: Owen Taylor <otaylor src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [opw-web] Optimize skin variables and localization
- Date: Fri, 4 Apr 2014 16:18:52 +0000 (UTC)
commit d04e4ab14e2e593439552a7d1bee697442b57668
Author: Owen W. Taylor <otaylor fishsoup net>
Date: Fri Apr 4 12:16:44 2014 -0400
Optimize skin variables and localization
* use preg_replace_callback() to avoid searching through text
for each variable or (worse) each localized string.
* Don't include the file of localized strings *each* time we
need to localize something, instead keep it around.
classes/class_lang.php | 125 ++++++++++++++++++++++++++----------------------
classes/class_skin.php | 20 +++++---
2 files changed, 80 insertions(+), 65 deletions(-)
---
diff --git a/classes/class_lang.php b/classes/class_lang.php
index 99ff8ec..d54c461 100644
--- a/classes/class_lang.php
+++ b/classes/class_lang.php
@@ -14,16 +14,34 @@ class lang
// Constructor
function __construct()
{
- global $config;
+ global $config, $core;
$this->lang_name = $config->lang_name;
- $this->lang_vars = array();
+ $this->lang_vars = null;
+ $this->lang_data = null;
}
- // Function to parse localization data
- function parse($data)
+ function _init_lang_vars()
{
- global $core, $user, $gsod, $config;
+ global $core, $config, $user;
+
+ if ($this->lang_vars !== null)
+ return;
+
+ $this->lang_vars = array(
+ 'host' => htmlspecialchars($core->base_uri()),
+ 'site_name' => htmlspecialchars($config->site_name),
+ 'username' => htmlspecialchars($user->username),
+ 'timezone' => htmlspecialchars(date('T'))
+ );
+ }
+
+ function _load()
+ {
+ global $config;
+
+ if ($this->lang_data !== null)
+ return;
if (file_exists(realpath("lang/{$this->lang_name}.php")))
{
@@ -37,49 +55,60 @@ class lang
$gsod->trigger($title, $message);
}
- $data = $this->set_defaults($data);
-
- foreach ($lang_data as $key => $value)
- {
- $value = $this->parse_vars($value);
- $data = str_replace("{{{$key}}}", $value, $data);
- }
-
- // Show unlocalized data as is
- $data = preg_replace('/\{\{(.*?)\}\}/', '$1', $data);
+ // Add default values
+ $lang_data['lang_name'] = $this->lang_name;
+ $lang_data['site_name'] = $config->site_name;
+ $lang_data['site_copyright'] = $config->site_copyright;
- // Done!
- return $data;
+ $this->lang_data = $lang_data;
}
- // Parses language variables
- function parse_vars($data)
+ function _replace($matches)
{
- global $config, $core, $user;
+ $k = $matches[1];
+ if (isset($this->lang_data[$k]))
+ return $this->parse_vars($this->lang_data[$k]);
+ else
+ return $k;
+ }
- // Substitute generic data within the laguage files
- $data = str_replace("[[host]]", htmlspecialchars($core->base_uri()), $data);
- $data = str_replace("[[site_name]]", $config->site_name, $data);
- $data = str_replace("[[username]]", htmlspecialchars($user->username), $data);
- $data = str_replace("[[timezone]]", htmlspecialchars(date('T')), $data);
+ // Function to parse localization data
+ function parse($data)
+ {
+ global $core, $user, $gsod, $config;
- // Replace placeholder with values
- foreach ($this->lang_vars as $key => $value)
- {
- $data = str_replace("[[$key]]", $value, $data);
- }
+ $this->_load();
+ $this->_init_lang_vars();
- // Remove unknown placeholders
- $data = preg_replace('/\[\[(.*?)\]\]/', '', $data);
+ // Replace localized strings; unknown strings are left as is
+ return preg_replace_callback('/\{\{(.*?)\}\}/',
+ array($this, '_replace'),
+ $data);
+ }
- // Done!
- return $data;
+ function _replace_var($matches)
+ {
+ $k = $matches[1];
+ if (isset($this->lang_vars[$k]))
+ return $this->lang_vars[$k];
+ else
+ // Remove unknown placeholders
+ return '';
}
+ // Parses language variables
+ function parse_vars($data)
+ {
+ return preg_replace_callback('/\[\[(.*?)\]\]/',
+ array($this, '_replace_var'),
+ $data);
+ }
// Function to assign language variables
function assign($data, $value = "")
{
+ $this->_init_lang_vars();
+
if (!is_array($data) && $value)
{
$this->lang_vars[$data] = $value;
@@ -98,26 +127,11 @@ class lang
{
global $config, $core, $user;
- // Return default data
- switch ($key)
- {
- case 'lang_name':
- return $this->lang_name;
- case 'site_name':
- return $config->site_name;
- case 'site_copyright':
- return $config->site_copyright;
- }
-
- // Get language data from lang file
- if (file_exists(realpath('lang/' . $this->lang_name . '.php')))
- {
- include('lang/' . $this->lang_name . '.php');
- }
+ $this->_load();
- if (isset($lang_data[$key]))
+ if (isset($this->lang_data[$key]))
{
- $data = $lang_data[$key];
+ $data = $this->lang_data[$key];
// Parse placeholders
$data = $this->parse_vars($data);
@@ -132,15 +146,10 @@ class lang
}
// Function to assign default variables
- function set_defaults($data)
+ function get_defaults()
{
global $config;
- $data = str_replace("{{lang_name}}", $this->lang_name, $data);
- $data = str_replace("{{site_name}}", $config->site_name, $data);
- $data = str_replace("{{site_copyright}}", $config->site_copyright, $data);
-
- return $data;
}
// Function to exclude a string from being treated as a key
diff --git a/classes/class_skin.php b/classes/class_skin.php
index 733fdee..ab216f6 100644
--- a/classes/class_skin.php
+++ b/classes/class_skin.php
@@ -63,6 +63,16 @@ class skin
$this->skin_title = $value;
}
+ function _replace_skin_var($matches)
+ {
+ $k = $matches[1];
+ if (isset($this->skin_vars[$matches[1]]))
+ return $this->skin_vars[$matches[1]];
+ else
+ // Remove unknown placeholders
+ return '';
+ }
+
// Function to parse template variables
function parse($file_name)
{
@@ -95,13 +105,9 @@ class skin
$data = file_get_contents($file_name);
// Parse the skin vars
- foreach ($this->skin_vars as $key => $value)
- {
- $data = str_replace("[[$key]]", $value, $data);
- }
-
- // Remove unknown placeholders
- $data = preg_replace('/\[\[(.*?)\]\]/', '', $data);
+ $data = preg_replace_callback('/\[\[(.*?)\]\]/',
+ array($this, '_replace_skin_var'),
+ $data);
// Remove line breaks and tabs
$data = preg_replace('/[\n]+/', '', $data);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]