Add Internationalization
- Add Utils/L10n class - Add translator functions to PHP Renderer - Refactor web controllers to prevent duplicated code - Add locale middleware - Add translation file loading - Add i18n settings
This commit is contained in:
parent
13a2068a8b
commit
5b7bb030de
21 changed files with 537 additions and 245 deletions
|
|
@ -21,14 +21,14 @@ class Search
|
|||
*/
|
||||
private $profileModel;
|
||||
/**
|
||||
* @var \Friendica\Directory\Content\L10n
|
||||
* @var \Gettext\TranslatorInterface
|
||||
*/
|
||||
private $l10n;
|
||||
|
||||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Models\Profile $profileModel,
|
||||
\Friendica\Directory\Content\L10n $l10n
|
||||
\Gettext\TranslatorInterface $l10n
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
|
|
@ -66,11 +66,12 @@ AND `account_type` = :account_type';
|
|||
$count = $this->profileModel->getCountForDisplay($sql_where, $values);
|
||||
|
||||
$vars = [
|
||||
'query' => $originalQuery,
|
||||
'page' => $pager->getPage(),
|
||||
'query' => $originalQuery,
|
||||
'field' => $field,
|
||||
'page' => $pager->getPage(),
|
||||
'itemsperpage' => $pager->getItemsPerPage(),
|
||||
'count' => $count,
|
||||
'profiles' => $profiles
|
||||
'count' => $count,
|
||||
'profiles' => $profiles
|
||||
];
|
||||
|
||||
// Render index view
|
||||
|
|
|
|||
9
src/classes/Controllers/Web/BaseController.php
Normal file
9
src/classes/Controllers/Web/BaseController.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Directory\Controllers\Web;
|
||||
|
||||
|
||||
abstract class BaseController
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -2,17 +2,17 @@
|
|||
|
||||
namespace Friendica\Directory\Controllers\Web;
|
||||
|
||||
use \Friendica\Directory\Content\Pager;
|
||||
use \Friendica\Directory\Views\Widget\PopularCountries;
|
||||
use \Friendica\Directory\Views\Widget\PopularTags;
|
||||
use PDO;
|
||||
use Friendica\Directory\Content\Pager;
|
||||
use Friendica\Directory\Views\Widget\PopularCountries;
|
||||
use Friendica\Directory\Views\Widget\PopularLanguages;
|
||||
use Friendica\Directory\Views\Widget\PopularTags;
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Directory
|
||||
class Directory extends BaseController
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
|
|
@ -31,7 +31,7 @@ class Directory
|
|||
*/
|
||||
private $renderer;
|
||||
/**
|
||||
* @var \Friendica\Directory\Content\L10n
|
||||
* @var \Gettext\TranslatorInterface
|
||||
*/
|
||||
private $l10n;
|
||||
|
||||
|
|
@ -40,7 +40,7 @@ class Directory
|
|||
\Friendica\Directory\Models\Profile $profileModel,
|
||||
\Friendica\Directory\Views\Widget\AccountTypeTabs $accountTypeTabs,
|
||||
\Friendica\Directory\Views\PhpRenderer $renderer,
|
||||
\Friendica\Directory\Content\L10n $l10n
|
||||
\Gettext\TranslatorInterface $l10n
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
|
|
@ -50,10 +50,11 @@ class Directory
|
|||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
public function render(Request $request, Response $response, array $args): Response
|
||||
public function render(Request $request, Response $response, array $args): array
|
||||
{
|
||||
$popularTags = new PopularTags($this->atlas, $this->renderer);
|
||||
$popularCountries = new PopularCountries($this->atlas, $this->renderer);
|
||||
$popularLanguages = new PopularLanguages($this->atlas, $this->renderer);
|
||||
|
||||
$pager = new Pager($this->l10n, $request, 20);
|
||||
|
||||
|
|
@ -69,18 +70,19 @@ class Directory
|
|||
$count = $this->profileModel->getCountForDisplay($condition, $values);
|
||||
|
||||
$vars = [
|
||||
'title' => $this->l10n->t('People'),
|
||||
'title' => $this->l10n->gettext('People'),
|
||||
'profiles' => $profiles,
|
||||
'pager_full' => $pager->renderFull($count),
|
||||
'pager_minimal' => $pager->renderMinimal($count),
|
||||
'accountTypeTabs' => $this->accountTypeTabs->render('directory', $args['account_type'] ?? ''),
|
||||
'popularTags' => $popularTags->render(),
|
||||
'popularCountries' => $popularCountries->render(),
|
||||
'popularLanguages' => $popularLanguages->render(),
|
||||
];
|
||||
|
||||
$content = $this->renderer->fetch('directory.phtml', $vars);
|
||||
|
||||
// Render index view
|
||||
return $this->renderer->render($response, 'layout.phtml', ['baseUrl' => $request->getUri()->getBaseUrl(), 'content' => $content]);
|
||||
return ['content' => $content];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Search
|
||||
class Search extends BaseController
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
|
|
@ -29,7 +29,7 @@ class Search
|
|||
*/
|
||||
private $accountTypeTabs;
|
||||
/**
|
||||
* @var \Friendica\Directory\Content\L10n
|
||||
* @var \Gettext\TranslatorInterface
|
||||
*/
|
||||
private $l10n;
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ class Search
|
|||
\Friendica\Directory\Models\Profile $profileModel,
|
||||
\Friendica\Directory\Views\Widget\AccountTypeTabs $accountTypeTabs,
|
||||
\Friendica\Directory\Views\PhpRenderer $renderer,
|
||||
\Friendica\Directory\Content\L10n $l10n
|
||||
\Gettext\TranslatorInterface $l10n
|
||||
)
|
||||
{
|
||||
$this->atlas = $atlas;
|
||||
|
|
@ -48,7 +48,7 @@ class Search
|
|||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
|
||||
public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): array
|
||||
{
|
||||
$pager = new Pager($this->l10n, $request, 20);
|
||||
|
||||
|
|
@ -56,9 +56,20 @@ class Search
|
|||
|
||||
$field = filter_input(INPUT_GET, 'field', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW & FILTER_FLAG_STRIP_HIGH);
|
||||
|
||||
$fieldName = '';
|
||||
|
||||
if ($field) {
|
||||
$query .= '%';
|
||||
$sql_where = '`' . $field . '` LIKE :query';
|
||||
$sql_where = 'p.`' . $field . '` LIKE :query';
|
||||
|
||||
switch($field) {
|
||||
case 'language': $fieldName = $this->l10n->pgettext('field', 'Language'); break;
|
||||
case 'locality': $fieldName = $this->l10n->pgettext('field', 'Locality'); break;
|
||||
case 'region' : $fieldName = $this->l10n->pgettext('field', 'Region') ; break;
|
||||
case 'country' : $fieldName = $this->l10n->pgettext('field', 'Country') ; break;
|
||||
default: $fieldName = ucfirst($field);
|
||||
}
|
||||
|
||||
} else {
|
||||
$sql_where = "MATCH (p.`name`, p.`pdesc`, p.`profile_url`, p.`locality`, p.`region`, p.`country`, p.`tags` )
|
||||
AGAINST (:query IN BOOLEAN MODE)";
|
||||
|
|
@ -78,10 +89,12 @@ AND `account_type` = :account_type';
|
|||
$count = $this->profileModel->getCountForDisplay($sql_where, $values);
|
||||
|
||||
$vars = [
|
||||
'query' => $originalQuery,
|
||||
'count' => $count,
|
||||
'accountTypeTabs' => $this->accountTypeTabs->render('search', $account_type, ['q' => $originalQuery]),
|
||||
'profiles' => $profiles,
|
||||
'query' => $originalQuery,
|
||||
'field' => $field,
|
||||
'fieldName' => $fieldName,
|
||||
'count' => $count,
|
||||
'accountTypeTabs' => $this->accountTypeTabs->render('search', $account_type, ['q' => $originalQuery, 'field' => $field]),
|
||||
'profiles' => $profiles,
|
||||
'pager_full' => $pager->renderFull($count),
|
||||
'pager_minimal' => $pager->renderMinimal($count),
|
||||
];
|
||||
|
|
@ -89,6 +102,6 @@ AND `account_type` = :account_type';
|
|||
$content = $this->renderer->fetch('search.phtml', $vars);
|
||||
|
||||
// Render index view
|
||||
return $this->renderer->render($response, 'layout.phtml', ['baseUrl' => $request->getUri()->getBaseUrl(), 'content' => $content, 'noNavSearch' => true]);
|
||||
return ['content' => $content, 'noNavSearch' => true];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use Slim\Http\Response;
|
|||
/**
|
||||
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
||||
*/
|
||||
class Servers
|
||||
class Servers extends BaseController
|
||||
{
|
||||
/**
|
||||
* @var \Atlas\Pdo\Connection
|
||||
|
|
@ -21,7 +21,7 @@ class Servers
|
|||
*/
|
||||
private $renderer;
|
||||
/**
|
||||
* @var \Friendica\Directory\Content\L10n
|
||||
* @var \Gettext\TranslatorInterface
|
||||
*/
|
||||
private $l10n;
|
||||
/**
|
||||
|
|
@ -32,7 +32,7 @@ class Servers
|
|||
public function __construct(
|
||||
\Atlas\Pdo\Connection $atlas,
|
||||
\Friendica\Directory\Views\PhpRenderer $renderer,
|
||||
\Friendica\Directory\Content\L10n $l10n,
|
||||
\Gettext\TranslatorInterface $l10n,
|
||||
\Psr\SimpleCache\CacheInterface $simplecache
|
||||
)
|
||||
{
|
||||
|
|
@ -42,7 +42,7 @@ class Servers
|
|||
$this->simplecache = $simplecache;
|
||||
}
|
||||
|
||||
public function render(Request $request, Response $response): Response
|
||||
public function render(Request $request, Response $response): array
|
||||
{
|
||||
$stable_version = $this->simplecache->get('stable_version');
|
||||
if (!$stable_version) {
|
||||
|
|
@ -72,7 +72,7 @@ LIMIT :start, :limit';
|
|||
|
||||
foreach ($servers as $key => $server) {
|
||||
$servers[$key]['user_count'] = $this->atlas->fetchValue(
|
||||
'SELECT COUNT(*) FROM `profile` WHERE `available` AND `server_id` = :server_id',
|
||||
'SELECT COUNT(*) FROM `profile` WHERE `available` AND NOT `hidden` AND `server_id` = :server_id',
|
||||
['server_id' => [$server['id'], PDO::PARAM_INT]]
|
||||
);
|
||||
}
|
||||
|
|
@ -85,7 +85,7 @@ AND NOT `hidden`';
|
|||
$count = $this->atlas->fetchValue($stmt);
|
||||
|
||||
$vars = [
|
||||
'title' => $this->l10n->t('Public Servers'),
|
||||
'title' => $this->l10n->gettext('Public Servers'),
|
||||
'servers' => $servers,
|
||||
'pager' => $pager->renderFull($count),
|
||||
'stable_version' => $stable_version,
|
||||
|
|
@ -95,6 +95,6 @@ AND NOT `hidden`';
|
|||
$content = $this->renderer->fetch('servers.phtml', $vars);
|
||||
|
||||
// Render index view
|
||||
return $this->renderer->render($response, 'layout.phtml', ['baseUrl' => $request->getUri()->getBaseUrl(), 'content' => $content]);
|
||||
return ['content' => $content];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue