Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Oct 5, 2023
2 parents 05bbe66 + c27fb5d commit edfa0b5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 2 deletions.
10 changes: 9 additions & 1 deletion system/Language/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Language;

use Config\Services;
use InvalidArgumentException;
use MessageFormatter;

/**
Expand Down Expand Up @@ -191,7 +192,14 @@ protected function formatMessage($message, array $args = [])
return $message;
}

return MessageFormatter::formatMessage($this->locale, $message, $args);
$formatted = MessageFormatter::formatMessage($this->locale, $message, $args);
if ($formatted === false) {
throw new InvalidArgumentException(
lang('Language.invalidMessageFormat', [$message, implode(',', $args)])
);
}

return $formatted;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions system/Language/en/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

// "Language" language settings
return [
'invalidMessageFormat' => 'Invalid message format: "{0}", args: "{1}"',
];
25 changes: 25 additions & 0 deletions tests/system/Language/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockLanguage;
use Config\Services;
use InvalidArgumentException;
use MessageFormatter;
use Tests\Support\Language\SecondMockLanguage;

Expand Down Expand Up @@ -126,6 +127,30 @@ public function testGetLineArrayFormatsMessages(): void
$this->assertSame(['45 related books.'], $this->lang->getLine('books.bookList', [91 / 2]));
}

/**
* @see https://github.com/codeigniter4/shield/issues/851
*/
public function testGetLineInvalidFormatMessage(): void
{
// No intl extension? then we can't test this - go away....
if (! class_exists(MessageFormatter::class)) {
$this->markTestSkipped('No intl support.');
}

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(
'Invalid message format: "تم الكشف عن كلمة المرور {0} بسبب اختراق البيانات وشوهدت {1 ، عدد} مرة في {2} في كلمات المرور المخترقة.", args: "password,hits,wording"'
);

$this->lang->setLocale('ar');

$this->lang->setData('Auth', [
'errorPasswordPwned' => 'تم الكشف عن كلمة المرور {0} بسبب اختراق البيانات وشوهدت {1 ، عدد} مرة في {2} في كلمات المرور المخترقة.',
]);

$this->lang->getLine('Auth.errorPasswordPwned', ['password', 'hits', 'wording']);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/891
*/
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.4.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ BREAKING
Message Changes
***************

- Added ``Language.invalidMessageFormat`` error message.

Changes
*******

Expand Down
Binary file modified user_guide_src/source/images/tutorial3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified user_guide_src/source/images/tutorial4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion user_guide_src/source/tutorial/create_news_items.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Create a News Item
******************

Now point your browser to your local development environment where you
installed CodeIgniter and add **/news/create** to the URL.
installed CodeIgniter and add **/news/new** to the URL.
Add some news and check out the different pages you made.

.. image:: ../images/tutorial3.png
Expand Down

0 comments on commit edfa0b5

Please sign in to comment.