diff --git a/docs/locale_detection.md b/docs/locale_detection.md index 2b9c973..7ddbb19 100644 --- a/docs/locale_detection.md +++ b/docs/locale_detection.md @@ -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 @@ -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. diff --git a/src/fbt/Transform/FbtTransform/Processors/FbtFunctionCallProcessor.php b/src/fbt/Transform/FbtTransform/Processors/FbtFunctionCallProcessor.php index da184ba..749a0ff 100644 --- a/src/fbt/Transform/FbtTransform/Processors/FbtFunctionCallProcessor.php +++ b/src/fbt/Transform/FbtTransform/Processors/FbtFunctionCallProcessor.php @@ -278,6 +278,7 @@ private function _extractTableTextsFromStringArrayItem($node, array $variations, switch ($node->name) { case 'param': + case 'sameParam': $texts[] = $variations[$arg0] ?? '{' . $arg0 . '}'; break; diff --git a/src/fbt/Transform/NodeVisitor.php b/src/fbt/Transform/NodeVisitor.php index 016b8ce..f4b8043 100644 --- a/src/fbt/Transform/NodeVisitor.php +++ b/src/fbt/Transform/NodeVisitor.php @@ -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; @@ -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'])) { diff --git a/tests/fbt/fbtTest.php b/tests/fbt/fbtTest.php index bfc3cad..5779dc6 100644 --- a/tests/fbt/fbtTest.php +++ b/tests/fbt/fbtTest.php @@ -838,6 +838,15 @@ public function testValuesThatLookLikeTokenPatterns() $this->assertSame('with tokens {tokenB} and B', $fbt); } + public function testSubject() + { + $fbt = fbt('You
seethe world', 'expose subject', [ + 'subject' => IntlVariations::GENDER_MALE, + ]); + + $this->assertSame('You
see
the world', (string)$fbt); + } + public function testJsonSerialization() { $fbt = fbt('simple text', 'desc');