. // }}} /** * Utility functions for i18n * * @category I18n * @package GNU social * * @author Matthew Gregg * @author Ciaran Gultnieks * @author Evan Prodromou * @author Diogo Cordeiro * @author Hugo Sales * @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; // 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: * * _m($msg) -- simple message * _m($ctx, $msg) -- message with context * _m($msg1, $msg2, $n) -- message that can be singular or plural * _m($ctx, $msg1, $msg2, $n) -- combination of the previous two * * @param string $msg * @param extra params as described above * * @throws InvalidArgumentException * * @return string * * @todo add parameters */ function _m(string $msg /*, ...*/): string { $domain = I18nHelper::_mdomain(debug_backtrace()[0]['file']); $args = func_get_args(); switch (count($args)) { case 1: // Empty parameters return I18nHelper::$translator->trans($msg, [], $domain); case 2: $context = $args[0]; $msg_single = $args[1]; // ASCII 4 is EOT, used to separate context from string return I18nHelper::$translator->trans($context . '\004' . $msg_single, [], $domain); case 3: // '|' separates the singular from the plural version $msg_single = $args[0]; $msg_plural = $args[1]; $n = $args[2]; return I18nHelper::$translator->trans($msg_single . '|' . $msg_plural, ['%d' => $n], $domain); case 4: // Combine both $context = $args[0]; $msg_single = $args[1]; $msg_plural = $args[2]; $n = $args[3]; return I18nHelper::$translator->trans($context . '\004' . $msg_single . '|' . $msg_plural, ['%d' => $n], $domain); default: throw new InvalidArgumentException('Bad parameter count to _m()'); } }