Skip to content
Permalink
Browse files

perf(languages): improved js caching of languages by using simplecache

fixes #7458
  • Loading branch information...
jdalsem committed Dec 16, 2014
1 parent 81e36b7 commit ab17ee54dd3e71f8bb230ec3c9a4368c492b7cd1
Showing with 36 additions and 23 deletions.
  1. +20 −0 engine/lib/languages.php
  2. +1 −6 views/default/js/elgg.php
  3. +14 −17 views/default/js/languages.php
  4. +1 −0 views/default/js/languages/en.php
@@ -236,3 +236,23 @@ function get_language_completeness($language) {
function get_missing_language_keys($language) {
return _elgg_services()->translator->getMissingLanguageKeys($language);
}
/**
* Initializes simplecache views for translations
*
* @return void
*/
function _elgg_translations_init() {
$translations = _elgg_services()->translator->getInstalledTranslations();
foreach ($translations as $key => $language) {
// make the js view available for each language
elgg_extend_view("js/languages/$key.js", "js/languages");
// register the js view for use in simplecache
elgg_register_simplecache_view("js/languages/$key.js");
}
}
return function(\Elgg\EventsService $events) {
$events->registerHandler('init', 'system', '_elgg_translations_init');
};
@@ -64,12 +64,7 @@
elgg.config.language = '<?php echo (empty($CONFIG->language) ? 'en' : $CONFIG->language); ?>';
!function () {
var languagesUrl = elgg.config.wwwroot + 'ajax/view/js/languages?language=' + elgg.get_language();
if (elgg.config.simplecache_enabled) {
languagesUrl += '&lc=' + elgg.config.lastcache;
}
define('elgg', ['jquery', languagesUrl], function($, translations) {
define('elgg', ['jquery', 'languages/' + elgg.get_language()], function($, translations) {
elgg.add_translation(elgg.get_language(), translations);
$(function() {
@@ -1,33 +1,30 @@
<?php
/**
* @uses $vars['language']
* @uses $vars['lc'] if present, client will be sent long expires headers
*/
$language = $vars['language'];
$lastcache = elgg_extract('lc', $vars, 0);
$language = elgg_extract('language', $vars);
// @todo add server-side caching
if ($lastcache) {
// we're relying on lastcache changes to predict language changes
$etag = '"' . md5("$language|$lastcache") . '"';
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+6 months")), true);
header("Pragma: public", true);
header("Cache-Control: public", true);
header("ETag: $etag");
if (empty($language)) {
// try to detect it
preg_match("/\/js\/languages\/(.*?).js+/", current_page_url(), $matches);
if (!empty($matches) && isset($matches[1])) {
$language = $matches[1];
}
}
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) === $etag) {
header("HTTP/1.1 304 Not Modified");
exit;
}
if (empty($language)) {
// fallback to 'en'
$language = 'en';
}
$all_translations = elgg_get_config('translations');
$translations = $all_translations['en'];
if ($language != 'en') {
if ($language != 'en' && isset($all_translations[$language])) {
$translations = array_merge($translations, $all_translations[$language]);
}
?>
define(<?php echo json_encode($translations); ?>);
@@ -1,2 +1,3 @@
<?php
// @todo This view is no longer need for Core. Can be removed in 2.0
echo elgg_view('js/languages', array('language' => 'en'));

0 comments on commit ab17ee5

Please sign in to comment.
You can’t perform that action at this time.