[UTIL][Common][I18N] Use actor's preferred language for _m and utility to retrieve current language even when no actor is logged in

This commit is contained in:
Diogo Peralta Cordeiro 2022-01-04 21:39:40 +00:00
parent 6247dd4c1a
commit 5a0bbfc795
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
4 changed files with 14 additions and 6 deletions

View File

@ -49,7 +49,7 @@ class Feeds extends FeedController
{ {
$data = $this->query( $data = $this->query(
query: 'note-local:true', query: 'note-local:true',
language: Common::actor()?->getTopLanguage()?->getLocale(), locale: Common::currentLanguage()->getLocale(),
); );
return [ return [
'_template' => 'collection/notes.html.twig', '_template' => 'collection/notes.html.twig',
@ -67,7 +67,7 @@ class Feeds extends FeedController
$actor = $user->getActor(); $actor = $user->getActor();
$data = $this->query( $data = $this->query(
query: 'note-from:subscribed-person,subscribed-group,subscribed-organization,subscribed-business', query: 'note-from:subscribed-person,subscribed-group,subscribed-organization,subscribed-business',
language: $actor->getTopLanguage()->getLocale(), locale: Common::currentLanguage()->getLocale(),
actor: $actor, actor: $actor,
); );
return [ return [

View File

@ -53,7 +53,7 @@ class Feeds extends FeedController
Common::ensureLoggedIn(); Common::ensureLoggedIn();
$data = $this->query( $data = $this->query(
query: 'note-local:false', query: 'note-local:false',
language: Common::actor()?->getTopLanguage()?->getLocale(), locale: Common::currentLanguage()->getLocale(),
); );
return [ return [
'_template' => 'collection/notes.html.twig', '_template' => 'collection/notes.html.twig',
@ -101,7 +101,7 @@ class Feeds extends FeedController
Common::ensureLoggedIn(); Common::ensureLoggedIn();
$data = $this->query( $data = $this->query(
query: '', query: '',
language: Common::actor()?->getTopLanguage()?->getLocale(), locale: Common::currentLanguage()->getLocale(),
); );
return [ return [
'_template' => 'collection/notes.html.twig', '_template' => 'collection/notes.html.twig',

View File

@ -38,6 +38,7 @@ declare(strict_types = 1);
namespace App\Core\I18n; namespace App\Core\I18n;
use App\Util\Common;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\Formatting; use App\Util\Formatting;
use InvalidArgumentException; use InvalidArgumentException;
@ -319,7 +320,7 @@ function _m(...$args): string
switch (\count($args)) { switch (\count($args)) {
case 1: case 1:
// Empty parameters, simple message // Empty parameters, simple message
return I18n::$translator->trans($args[0], [], $domain); return I18n::$translator->trans($args[0], [], $domain, Common::currentLanguage()->getLocale());
case 3: case 3:
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
if (\is_int($args[2])) { if (\is_int($args[2])) {
@ -337,7 +338,7 @@ function _m(...$args): string
if (\is_string($args[0])) { if (\is_string($args[0])) {
$msg = $args[0]; $msg = $args[0];
$params = $args[1] ?? []; $params = $args[1] ?? [];
return I18n::$translator->trans($msg, $params, $domain); return I18n::$translator->trans($msg, $params, $domain, Common::currentLanguage()->getLocale());
} }
// Fallthrough // Fallthrough
// no break // no break

View File

@ -34,11 +34,13 @@ declare(strict_types = 1);
namespace App\Util; namespace App\Util;
use App\Core\I18n\I18n;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Core\Security; use App\Core\Security;
use App\Entity\Actor; use App\Entity\Actor;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Util\Exception\NoLoggedInUser; use App\Util\Exception\NoLoggedInUser;
use Component\Language\Entity\Language;
use Egulias\EmailValidator\EmailValidator; use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation as RFCEmailValidation; use Egulias\EmailValidator\Validation\RFCValidation as RFCEmailValidation;
use Functional as F; use Functional as F;
@ -318,4 +320,9 @@ abstract class Common
} }
return $notes; return $notes;
} }
public static function currentLanguage(): Language
{
return self::actor()?->getTopLanguage() ?? Language::getByLocale(!\is_null(self::$request->headers->get('accept-language')) ? I18n::clientPreferredLanguage(self::$request->headers->get('accept-language')) : self::$request->getDefaultLocale());
}
} }