-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New feature: Translation message placeholder support #121
Draft
TotalWipeOut
wants to merge
18
commits into
laminas:2.27.x
Choose a base branch
from
TotalWipeOut:feature/placeholders
base: 2.27.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2c08407
Initial implementation of placeholders within translated text
TotalWipeOut 7268f05
#7: Pass translated message through placeholder compile
TotalWipeOut 3f9c7c9
#7: Ensure text without a translation is still compiled
TotalWipeOut feac441
#7: Move compilation to translate methods
TotalWipeOut 7d257af
#7: Changes to pass tests
TotalWipeOut f07eef4
#7: Missed returns
TotalWipeOut feee260
Better type inference for psalm
TotalWipeOut 1306a09
Optimise use
TotalWipeOut 1c50b03
Move translator with params to new methods, add corresponding view he…
TotalWipeOut de5e914
docblock tweaks
TotalWipeOut 48a9044
Update psalm baseline and address psalm type issues
TotalWipeOut 7ca402c
Fix param in docblock
TotalWipeOut 5c4f32d
Initial re-work using decorator pattern
TotalWipeOut da1f695
Better type inference for psalm, updated pslam baseline
TotalWipeOut 25ca826
phpcs style fix
TotalWipeOut 2e27540
Update view helper config to use new helpers with param support
TotalWipeOut a9cc082
Adjust some exception messages. correction for phpcs
TotalWipeOut 58bf694
corrections for phpcs
TotalWipeOut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\I18n\Translator\Formatter; | ||
|
||
interface FormatterInterface | ||
{ | ||
/** | ||
* @param iterable<array-key, string> $params | ||
*/ | ||
public function format(string $locale, string $message, iterable $params = []): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\I18n\Translator\Formatter; | ||
|
||
use function str_replace; | ||
|
||
class HandlebarFormatter implements FormatterInterface | ||
{ | ||
public function format(string $locale, string $message, iterable $params = []): string | ||
{ | ||
$compiled = $message; | ||
foreach ($params as $key => $value) { | ||
$compiled = str_replace("{{{$key}}}", $value, $compiled); | ||
} | ||
|
||
return $compiled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\I18n\Translator\Formatter; | ||
|
||
use MessageFormatter; | ||
use Traversable; | ||
|
||
use function iterator_to_array; | ||
|
||
class IcuFormatter implements FormatterInterface | ||
{ | ||
public function format(string $locale, string $message, iterable $params = []): string | ||
{ | ||
if ($params instanceof Traversable) { | ||
$params = iterator_to_array($params); | ||
} | ||
|
||
return MessageFormatter::formatMessage($locale, $message, $params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\I18n\Translator\Formatter; | ||
|
||
use Laminas\I18n\Exception\ParseException; | ||
use Traversable; | ||
|
||
use function call_user_func_array; | ||
use function iterator_to_array; | ||
|
||
class PrintfFormatter implements FormatterInterface | ||
{ | ||
public function format(string $locale, string $message, iterable $params = []): string | ||
{ | ||
if ($params instanceof Traversable) { | ||
$params = iterator_to_array($params); | ||
} | ||
|
||
/** @var string|false $compiled */ | ||
$compiled = call_user_func_array('vsprintf', [$message, $params]); | ||
if ($compiled === false) { | ||
throw new ParseException( | ||
'Error occurred while processing sprintf placeholders for message "' . $message . '"' | ||
); | ||
} | ||
|
||
return $compiled; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will provide the delegator to add the decorator for the new view helpers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I was going to ask regarding that. That would be great.
Thanks for all your time on this!