Skip to content

Commit

Permalink
Fix same-param for subject
Browse files Browse the repository at this point in the history
  • Loading branch information
richardDobron committed Apr 16, 2024
1 parent 9f46c03 commit dce4181
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/locale_detection.md
Expand Up @@ -13,7 +13,7 @@ To implement locale detection in your PHP project, you can use the `HTTP2` libra
composer require pear/http2
```

# Usage
## Usage
After installing the `HTTP2 library`, you can utilize it in your PHP code to detect the user's locale. Here's a basic example:

```php
Expand All @@ -40,10 +40,10 @@ $locale = $supportedLanguages[$clientLanguage];
\fbt\FbtConfig::set('locale', $locale);
```

# Explanation
## Explanation

## HTTP Accept-Language Header
### HTTP Accept-Language Header
The `negotiateLanguage()` method works by parsing the HTTP `Accept-Language` header sent by the user's browser. This header contains information about the user's preferred languages in order of priority. The method then matches these languages against the provided list and returns the best match.

## Fallback Locale
### Fallback Locale
In case the user's preferred language is not available or cannot be determined, it's essential to have a fallback locale. This ensures that your application always defaults to a suitable language/locale even when the detection process fails.
Expand Up @@ -278,6 +278,7 @@ private function _extractTableTextsFromStringArrayItem($node, array $variations,

switch ($node->name) {
case 'param':
case 'sameParam':
$texts[] = $variations[$arg0] ?? '{' . $arg0 . '}';

break;
Expand Down
15 changes: 15 additions & 0 deletions src/fbt/Transform/NodeVisitor.php
Expand Up @@ -3,7 +3,9 @@
namespace fbt\Transform;

use fbt\Services\CollectFbtsService;
use fbt\Transform\FbtTransform\Translate\IntlVariations;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
Expand All @@ -14,6 +16,19 @@ class NodeVisitor extends NodeVisitorAbstract
{
public function enterNode(Node $node)
{
if ($node instanceof FuncCall
&& $node->name instanceof Name
&& $node->name->toString() === 'fbt') {
if (isset($node->args[2]) && $node->args[2]->value instanceof Node\Expr\Array_) {
foreach ($node->args[2]->value->items as $attribute) {
if ($attribute->key->value === 'subject') {
if (! ($attribute->value instanceof String_)) {
$attribute->value = new LNumber(IntlVariations::GENDER_MALE);
}
}
}
}
}
if ($node instanceof StaticCall
&& $node->class instanceof Name
&& in_array($node->class->toString(), ['fbt', 'fbt\fbt'])) {
Expand Down
9 changes: 9 additions & 0 deletions tests/fbt/fbtTest.php
Expand Up @@ -838,6 +838,15 @@ public function testValuesThatLookLikeTokenPatterns()
$this->assertSame('with tokens {tokenB} and B', $fbt);
}

public function testSubject()
{
$fbt = fbt('You<fbt:param name="lineBreak"><br></fbt:param>see<fbt:same-param name="lineBreak"/>the world', 'expose subject', [
'subject' => IntlVariations::GENDER_MALE,
]);

$this->assertSame('You<br/>see<br/>the world', (string)$fbt);
}

public function testJsonSerialization()
{
$fbt = fbt('simple text', 'desc');
Expand Down

0 comments on commit dce4181

Please sign in to comment.