From f28ff24f2aefcba748898b64dc2bed30d0a378bc Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 11 Jun 2020 18:08:32 +0000 Subject: [PATCH] [I18N] Small fixes. Still broken, though :') --- src/Core/I18n/I18n.php | 28 ++++++++++++++++------------ src/Core/I18n/I18nHelper.php | 10 +++++----- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Core/I18n/I18n.php b/src/Core/I18n/I18n.php index fbc1344293..249bf95bab 100644 --- a/src/Core/I18n/I18n.php +++ b/src/Core/I18n/I18n.php @@ -34,6 +34,8 @@ namespace App\Core\I18n; +use Symfony\Component\Translation\Exception\InvalidArgumentException; + // Locale category constants are usually predefined, but may not be // on some systems such as Win32. $LC_CATEGORIES = [ @@ -68,13 +70,10 @@ abstract class I18n * 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 + * _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 * * @throws InvalidArgumentException * @@ -82,14 +81,14 @@ abstract class I18n * * @todo add parameters */ -function _m(string $msg /*, ...*/): string +function _m(): string { $domain = I18nHelper::_mdomain(debug_backtrace()[0]['file']); $args = func_get_args(); switch (count($args)) { case 1: // Empty parameters, simple message - return I18nHelper::$translator->trans($msg, [], $domain); + return I18nHelper::$translator->trans($args[0], [], $domain); case 3: if (is_int($args[2])) { throw new Exception('Calling `_m()` with an explicit number is deprecated, ' . @@ -98,9 +97,14 @@ function _m(string $msg /*, ...*/): string // Falthrough // no break case 2: - if (is_string($args[0]) && !is_array($args[1])) { - // ASCII 4 is EOT, used to separate context from string - $context = array_shift($args) . '\004'; + 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'); + } } if (is_array($args[0])) { diff --git a/src/Core/I18n/I18nHelper.php b/src/Core/I18n/I18nHelper.php index 4bd1888384..a32fa24a77 100644 --- a/src/Core/I18n/I18nHelper.php +++ b/src/Core/I18n/I18nHelper.php @@ -47,7 +47,7 @@ abstract class I18nHelper /** * Looks for which plugin we've been called from to get the gettext domain; - * if not in a plugin subdirectory, we'll use the default 'core'. + * if not in a plugin subdirectory, we'll use the default 'core+intl-icu'. * * @return string */ @@ -68,7 +68,7 @@ abstract class I18nHelper $path = Formatting::normalizePath($path); $cached[$path] = Formatting::pluginFromPath($path); } - return $cached[$path] ?? 'core'; + return $cached[$path] ?? 'core+intl-icu'; } /** @@ -219,17 +219,17 @@ abstract class I18nHelper { $msg = ''; foreach ($params as $var => $val) { - if (is_int($var)) { + if (is_int($val)) { $pref = '='; $op = 'plural'; } elseif (is_string($var)) { $pref = ''; - $op = 'select,'; + $op = 'select'; } else { throw new Exception('Invalid key type. (int|string) only'); } - $res = "{{$var}, {$op} "; + $res = "{{$var}, {$op}, "; $i = 1; $cnt = count($messages); foreach ($messages as $val => $m) {