Browse Source

Add a cache for translations, refreshed when the daemon is launched or by a explicit command

pull/978/head
Timothée Jaussoin 5 years ago
parent
commit
0caff697b6
  1. 1
      CHANGELOG.md
  2. 14
      app/widgets/Chats/Chats.php
  3. 2
      daemon.php
  4. 31
      src/Movim/Console/CompileLanguages.php
  5. 2
      src/Movim/Console/ConfigCommand.php
  6. 11
      src/Movim/Console/DaemonCommand.php
  7. 2
      src/Movim/Console/EmojisToJsonCommand.php
  8. 55
      src/Movim/i18n/Locale.php
  9. 7
      src/Movim/i18n/languages.php

1
CHANGELOG.md

@ -17,6 +17,7 @@ v0.18.1 (trunk)
* Handle vcard-temp avatars refresh in PresenceBuffer to also save some SQL requests
* Add ~1500 new emojis to the Javascript selector
* Implement XEP-0201: Best Practices for Message Threads and add Reply feature
* Add a cache for translations, refreshed when the daemon is launched or by a explicit command
v0.18
---------------------------

14
app/widgets/Chats/Chats.php

@ -231,12 +231,12 @@ class Chats extends Base
$messages = collect();
$jidFromToMessages = DB::table('messages')
->where('user_id', $this->user->id)
->whereIn('jidfrom', array_keys($chats))
->unionAll(DB::table('messages')
->where('user_id', $this->user->id)
->whereIn('jidto', array_keys($chats))
);
->whereIn('jidfrom', array_keys($chats))
->unionAll(DB::table('messages')
->where('user_id', $this->user->id)
->whereIn('jidto', array_keys($chats))
);
$selectedMessages = $this->user->messages()
->joinSub(
@ -284,7 +284,9 @@ class Chats extends Base
return $view->draw('_chats_empty_item');
}
public function prepareChat(string $jid, App\Contact $contact, App\Roster $roster = null, App\Message $message = null, $status = null, $active = false)
public function prepareChat(
string $jid, App\Contact $contact, App\Roster $roster = null,
App\Message $message = null, $status = null, $active = false)
{
if (!$this->validateJid($jid)) {
return;

2
daemon.php

@ -8,6 +8,7 @@ use Movim\Bootstrap;
use Movim\Console\DaemonCommand;
use Movim\Console\ConfigCommand;
use Movim\Console\EmojisToJsonCommand;
use Movim\Console\CompileLanguages;
use Symfony\Component\Console\Application;
$bootstrap = new Bootstrap;
@ -17,4 +18,5 @@ $application = new Application;
$application->add(new ConfigCommand);
$application->add(new DaemonCommand);
$application->add(new EmojisToJsonCommand);
$application->add(new CompileLanguages);
$application->run();

31
src/Movim/Console/CompileLanguages.php

@ -0,0 +1,31 @@
<?php
namespace Movim\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Movim\i18n\Locale;
class CompileLanguages extends Command
{
protected function configure()
{
$this
->setName('compileLanguages')
->setDescription('Compile and cache the languages files');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$locale = Locale::start();
$locale->compileIni();
$output->writeln('<info>Compiled hash file</info>');
$locale->compilePos();
$output->writeln('<info>Compiled po files</info>');
return 0;
}
}

2
src/Movim/Console/ConfigCommand.php

@ -99,6 +99,6 @@ class ConfigCommand extends Command
}
}
return 1;
return 0;
}
}

11
src/Movim/Console/DaemonCommand.php

@ -14,6 +14,7 @@ use Ratchet\WebSocket\WsServer;
use Movim\Daemon\Core;
use Movim\Daemon\Api;
use Movim\i18n\Locale;
use App\Configuration;
use Phinx\Migration\Manager;
@ -92,6 +93,14 @@ class DaemonCommand extends Command
exit;
}
$locale = Locale::start();
$locale->compileIni();
$output->writeln('<info>Compiled hash file</info>');
$locale->compilePos();
$output->writeln('<info>Compiled po files</info>');
$output->writeln('<info>Movim daemon launched</info>');
$output->writeln('<info>Base URL: '.$baseuri.'</info>');
@ -111,5 +120,7 @@ class DaemonCommand extends Command
new Api($loop, $socketApi, $core);
(new IoServer($app, $socket, $loop))->run();
return 0;
}
}

2
src/Movim/Console/EmojisToJsonCommand.php

@ -66,6 +66,6 @@ class EmojisToJsonCommand extends Command
$output->writeln('<info>'.\count($json).' emojis saved</info>');
return 1;
return 0;
}
}

55
src/Movim/i18n/Locale.php

@ -9,8 +9,22 @@ class Locale
public $language;
public $hash = [];
private $iniCache = CACHE_PATH.'locales.ini.cache';
private function __construct()
{
if (file_exists($this->iniCache)) {
include $this->iniCache;
$this->hash = $hashes;
} else {
$this->compileIni();
$this->compilePos();
}
}
public function compileIni()
{
$this->hash = [];
$this->loadIni(
LOCALES_PATH . 'locales.ini',
true,
@ -24,6 +38,32 @@ class Locale
$this->loadIni($path);
}
}
$locales = fopen($this->iniCache, "w") or die("Unable to open file!");
fwrite($locales, '<?php' . PHP_EOL . '$hashes = '.var_export($this->hash,true) . ';' . PHP_EOL . '?>');
fclose($locales);
}
public function compilePos()
{
// Clear
foreach (
glob(
CACHE_PATH.
'*.po.cache',
GLOB_NOSORT
) as $cacheFile) {
@unlink($cacheFile);
}
// Cache
foreach (array_keys($this->getList()) as $language) {
$this->load($language);
$locales = fopen(CACHE_PATH.$language.'.po.cache', "w") or die("Unable to open file!");
fwrite($locales, '<?php' . PHP_EOL . '$translations = '.var_export($this->translations,true) . ';' . PHP_EOL . '?>');
fclose($locales);
}
}
/**
@ -59,14 +99,14 @@ class Locale
{
require_once('languages.php');
$lang_list = get_lang_list();
$langList = getLangList();
$dir = scandir(LOCALES_PATH);
$po = [];
foreach ($dir as $files) {
$explode = explode('.', $files);
if (end($explode) == 'po'
&& array_key_exists($explode[0], $lang_list)) {
$po[$explode[0]] = $lang_list[$explode[0]];
&& array_key_exists($explode[0], $langList)) {
$po[$explode[0]] = $langList[$explode[0]];
}
}
@ -87,6 +127,7 @@ class Locale
}
$arr = explode('.', $key);
if (is_array($this->hash)
&& array_key_exists($arr[0], $this->hash)
&& array_key_exists($arr[1], $this->hash[$arr[0]])) {
@ -186,6 +227,14 @@ class Locale
*/
public function loadPo()
{
// Load from the cache
$cacheFile = CACHE_PATH . $this->language . '.po.cache';
if (file_exists($cacheFile) && is_readable($cacheFile)) {
include $cacheFile;
$this->translations = $translations;
return;
}
$pofile = LOCALES_PATH . $this->language . '.po';
if (!file_exists($pofile) || !is_readable($pofile)) {

7
src/Movim/i18n/languages.php

@ -3,9 +3,9 @@
* Return an array containing all the presents languages in i18n/
*
*/
function get_lang_list()
function getLangList()
{
$lang_list = array(
$lang_list = [
'aa' => "Afar",
'ab' => "Abkhazian",
'ach' => "Acholi",
@ -201,8 +201,7 @@ function get_lang_list()
'zh' => "&#20013;&#25991;", // chinois (ecriture simplifiee)
'zh_tw' => "&#21488;&#28771;&#20013;&#25991;", // chinois taiwan (ecr. traditionnelle)
'zu' => "Zulu"
);
];
return $lang_list;
}
Loading…
Cancel
Save