Skip to content

Commit

Permalink
Merge 0116556 into 7132a3e
Browse files Browse the repository at this point in the history
  • Loading branch information
abbadon1334 committed Jul 15, 2019
2 parents 7132a3e + 0116556 commit 453a4b2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -13,7 +13,8 @@
"prefer-stable": true,
"require": {
"php": ">=7.2",
"atk4/core": "dev-develop"
"atk4/core": "dev-develop",
"ext-json": "*"
},
"require-dev": {
"atk4/data": "dev-develop",
Expand Down
23 changes: 22 additions & 1 deletion src/I18Next/Locale/Processor/Key.php
Expand Up @@ -16,8 +16,14 @@ final class Key extends AbstractProcessor
*
* @return string|null
*/
public function processKey(string $key, ?string $context = null, ?int $counter = null)
public function processKey(string $key, ?string $context = null, ?int $counter = null) :?string
{
$key_piped = $this->processDirectPipedKey($key, $counter);

if (null !== $key_piped) {
return $key_piped;
}

// if defined add context needed in any case
if ($context) {
$key .= '_'.$context;
Expand All @@ -31,6 +37,21 @@ public function processKey(string $key, ?string $context = null, ?int $counter =
return $this->processWithNamespaceWithCounter($key, $counter);
}

private function processDirectPipedKey($key, ?int $counter = null) : ?string
{
$key_piped = explode('|', $key);

if (count($key_piped) === 1) {
return null;
}

if (null === $counter || 1 === $counter) {
return $key_piped[0];
}

return $key_piped[1];
}

/**
* @param string $key
* @param int|null $counter
Expand Down
30 changes: 30 additions & 0 deletions tests/Translator_CaseDirect_Test.php
@@ -0,0 +1,30 @@
<?php

namespace I18Next\Tests;

class Translator_CaseDirect_Test extends TranslatorBaseCase
{
public function testDirectPipe1()
{
$this->setupTranslatorLanguages('it', 'en');

$result = $this->translator->_('friend|friends', ['count' => 1]);
$this->assertEquals('friend', $result);
}

public function testDirectPipe3()
{
$this->setupTranslatorLanguages('it', 'en');

$result = $this->translator->_('friend|friends', ['count' => 3]);
$this->assertEquals('friends', $result);
}

public function testDirectPipe4()
{
$this->setupTranslatorLanguages('it', 'en');

$result = $this->translator->_('i have a friend|i have {{count}} friends', ['count' => 3]);
$this->assertEquals('i have 3 friends', $result);
}
}

0 comments on commit 453a4b2

Please sign in to comment.