Skip to content

Commit

Permalink
FIX Finish removing deprecated i18n functionality. (#11528)
Browse files Browse the repository at this point in the history
This deprecated functionality was partially removed in #10594. This
commit finishes the job.
  • Loading branch information
GuySartorelli authored Jan 7, 2025
1 parent 1b0909f commit 3871869
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 25 deletions.
28 changes: 4 additions & 24 deletions src/i18n/i18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SilverStripe\i18n\Messages\MessageProvider;
use SilverStripe\View\TemplateGlobalProvider;
use InvalidArgumentException;
use SilverStripe\Core\ArrayLib;

/**
* Base-class for storage and retrieval of translated entities.
Expand Down Expand Up @@ -170,20 +171,9 @@ public static function _t($entity, $arg = null)
user_error("Missing default for localisation key $entity", E_USER_WARNING);
}

// Deprecate legacy injection format (`string %s, %d`)
// inject the variables from injectionArray (if present)
$sprintfArgs = [];
if ($default && !preg_match('/\{[\w\d]*\}/i', $default ?? '') && preg_match('/%[s,d]/', $default ?? '')) {
$sprintfArgs = array_values($injection ?? []);
$injection = [];
}

// If injection isn't associative, assume legacy injection format
$failUnlessSprintf = false;
if ($injection && array_values($injection ?? []) === $injection) {
$failUnlessSprintf = true; // Note: Will trigger either a deprecation error or exception below
$sprintfArgs = array_values($injection ?? []);
$injection = [];
// Injection must be associative
if (!empty($injection) && !ArrayLib::is_associative($injection)) {
throw new InvalidArgumentException('Injection must be an associative array');
}

// Detect plurals: Has a {count} argument as well as a `|` pipe delimited string (if provided)
Expand All @@ -201,16 +191,6 @@ public static function _t($entity, $arg = null)
$result = static::getMessageProvider()->translate($entity, $default, $injection);
}

if (!$default && !preg_match('/\{[\w\d]*\}/i', $result ?? '') && preg_match('/%[s,d]/', $result ?? '')) {
throw new Exception('sprintf style localisation cannot be used in translations - detected in $result');
}

if ($failUnlessSprintf) {
// Note: After removing deprecated code, you can move this error up into the is-associative check
// Neither default nor translated strings were %s substituted, and our array isn't associative
throw new InvalidArgumentException('Injection must be an associative array');
}

return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ function project()
*/
function _t($entity, $arg = null)
{
// Pass args directly to handle deprecation
// Pass args directly as we allow variable args.
return call_user_func_array([i18n::class, '_t'], func_get_args());
}

0 comments on commit 3871869

Please sign in to comment.