2020-06-04 22:53:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility functions for i18n
|
|
|
|
*
|
|
|
|
* @category I18n
|
|
|
|
* @package GNU social
|
|
|
|
*
|
|
|
|
* @author Matthew Gregg <matthew.gregg@gmail.com>
|
|
|
|
* @author Ciaran Gultnieks <ciaran@ciarang.com>
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
|
|
|
* @author Hugo Sales <hugo@fc.up.pt>
|
|
|
|
* @copyright 2010, 2018-2020 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Core\I18n;
|
|
|
|
|
2020-06-11 19:08:32 +01:00
|
|
|
use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
|
|
|
|
2020-06-04 22:53:34 +01:00
|
|
|
// Locale category constants are usually predefined, but may not be
|
|
|
|
// on some systems such as Win32.
|
|
|
|
$LC_CATEGORIES = [
|
|
|
|
'LC_CTYPE',
|
|
|
|
'LC_NUMERIC',
|
|
|
|
'LC_TIME',
|
|
|
|
'LC_COLLATE',
|
|
|
|
'LC_MONETARY',
|
|
|
|
'LC_MESSAGES',
|
|
|
|
'LC_ALL',
|
|
|
|
];
|
|
|
|
foreach ($LC_CATEGORIES as $key => $name) {
|
|
|
|
if (!defined($name)) {
|
|
|
|
define($name, $key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class I18n
|
|
|
|
{
|
|
|
|
// Dummy class, because the bellow function needs to be outside of
|
|
|
|
// a class, since `rfc/use-static-function` isn't implemented, so
|
|
|
|
// we'd have to use `I18n::_m`, but the autoloader still needs a class with the same name as the file
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for symfony translation with smart domain detection.
|
|
|
|
*
|
|
|
|
* If calling from a plugin, this function checks which plugin it was
|
|
|
|
* being called from and uses that as text domain, which will have
|
|
|
|
* been set up during plugin initialization.
|
|
|
|
*
|
|
|
|
* Also handles plurals and contexts depending on what parameters
|
|
|
|
* are passed to it:
|
|
|
|
*
|
2020-06-11 19:08:32 +01:00
|
|
|
* _m(string $msg) -- simple message
|
|
|
|
* _m(string $ctx, string $msg) -- message with context
|
|
|
|
* _m(string|string[] $msg, array $params) -- message
|
|
|
|
* _m(string $ctx, string|string[] $msg, array $params) -- combination of the previous two
|
2020-06-04 22:53:34 +01:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @todo add parameters
|
|
|
|
*/
|
2020-06-11 19:08:32 +01:00
|
|
|
function _m(): string
|
2020-06-04 22:53:34 +01:00
|
|
|
{
|
|
|
|
$domain = I18nHelper::_mdomain(debug_backtrace()[0]['file']);
|
|
|
|
$args = func_get_args();
|
|
|
|
switch (count($args)) {
|
|
|
|
case 1:
|
2020-06-07 17:50:11 +01:00
|
|
|
// Empty parameters, simple message
|
2020-06-11 19:08:32 +01:00
|
|
|
return I18nHelper::$translator->trans($args[0], [], $domain);
|
2020-06-04 22:53:34 +01:00
|
|
|
case 3:
|
2020-06-07 17:50:11 +01:00
|
|
|
if (is_int($args[2])) {
|
|
|
|
throw new Exception('Calling `_m()` with an explicit number is deprecated, ' .
|
|
|
|
'use an explicit parameter');
|
|
|
|
}
|
|
|
|
// Falthrough
|
|
|
|
// no break
|
|
|
|
case 2:
|
2020-06-11 19:08:32 +01:00
|
|
|
if (is_string($args[0])) {
|
|
|
|
if (!is_array($args[1])) {
|
|
|
|
// ASCII 4 is EOT, used to separate context from string
|
|
|
|
$context = array_shift($args) . '\004';
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException('Bad parameters to `_m()`. '
|
|
|
|
. 'Seemingly called with a context and multiple messages');
|
|
|
|
}
|
2020-06-07 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($args[0])) {
|
|
|
|
$args[0] = I18nHelper::formatICU($args[0], $args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($args[0])) {
|
|
|
|
$msg = $args[0];
|
|
|
|
$params = $args[1] ?? [];
|
|
|
|
return I18nHelper::$translator->trans($context ?? '' . $msg, $params, $domain);
|
|
|
|
}
|
|
|
|
// Fallthrough
|
|
|
|
// no break
|
2020-06-04 22:53:34 +01:00
|
|
|
default:
|
2020-06-07 17:50:11 +01:00
|
|
|
throw new InvalidArgumentException('Bad parameters to `_m()`');
|
2020-06-04 22:53:34 +01:00
|
|
|
}
|
|
|
|
}
|