-
Couldn't load subscription status.
- Fork 6
Added translation extractor for custom tags #162
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9957cf4
Added translation extractor for custom tags
adamwojs 63195a1
Added translation extractor for custom tags choices
adamwojs 8e426a8
Use X instead of Twitter
adamwojs d8e8c91
Fixed translation test
adamwojs 810c448
Update src/bundle/Resources/config/translation.yaml
adamwojs 458e5ff
Applied M. Adamczyk and J. Koralewicz code review suggestions
adamwojs 0bc1944
Refactored config not to override prepended custom tags
alongosz b928b0f
Changed property name to allowlist in ChoiceAttributeExtractor
alongosz 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 hidden or 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 hidden or 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 hidden or 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,131 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:jms="urn:jms:translation" version="1.2"> | ||
| <file source-language="en" target-language="en" datatype="plaintext" original="not.available"> | ||
| <header> | ||
| <tool tool-id="JMSTranslationBundle" tool-name="JMSTranslationBundle" tool-version="1.1.0-DEV"/> | ||
| <note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note> | ||
| </header> | ||
| <body> | ||
| <trans-unit id="7ddd17c1a96742c10988612627d3bf5134bcd912" resname="ezrichtext.custom_tags.ezfacebook.attributes.post_url.label"> | ||
| <source>Post URL</source> | ||
| <target>Post URL</target> | ||
| <note>key: ezrichtext.custom_tags.ezfacebook.attributes.post_url.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="14df6986afb2121f6d8f3a8e3fd3cc2e8719f28e" resname="ezrichtext.custom_tags.ezfacebook.attributes.width.label"> | ||
| <source>Width</source> | ||
| <target>Width</target> | ||
| <note>key: ezrichtext.custom_tags.ezfacebook.attributes.width.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="04a09b59ee06e7996670b684a9ca256d792992d2" resname="ezrichtext.custom_tags.ezfacebook.description"> | ||
| <source>Embed Facebook post</source> | ||
| <target>Embed Facebook post</target> | ||
| <note>key: ezrichtext.custom_tags.ezfacebook.description</note> | ||
| </trans-unit> | ||
| <trans-unit id="48fc0ce4ff40934d1fff06a56055c9ad73f634f4" resname="ezrichtext.custom_tags.ezfacebook.label"> | ||
| <source>Facebook</source> | ||
| <target>Facebook</target> | ||
| <note>key: ezrichtext.custom_tags.ezfacebook.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="a711cbe9c11ea82cc8ad02c93af5727dceb8b34d" resname="ezrichtext.custom_tags.eztwitter.attributes.cards.choice.hidden.label"> | ||
| <source>Hidden</source> | ||
| <target>Hidden</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.cards.choice.hidden.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="b7ef5c07f5cbc982f23e547bc1e1326752853018" resname="ezrichtext.custom_tags.eztwitter.attributes.cards.label"> | ||
| <source>Cards</source> | ||
| <target>Cards</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.cards.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="333d02160158768833ecf1aa1bde5b60a9c3f7d1" resname="ezrichtext.custom_tags.eztwitter.attributes.conversation.choice.none.label"> | ||
| <source>None</source> | ||
| <target>None</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.conversation.choice.none.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="5671ef11994be024b75d6360144708538714d952" resname="ezrichtext.custom_tags.eztwitter.attributes.conversation.label"> | ||
| <source>Conversation</source> | ||
| <target>Conversation</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.conversation.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="7fa382eb492f241c0377d64e8ad5c8ab8aa740fb" resname="ezrichtext.custom_tags.eztwitter.attributes.dnt.label"> | ||
| <source>Do not target</source> | ||
| <target>Do not target</target> | ||
adamwojs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.dnt.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="df37139c5dd99f14d3e1390341bd49d11da3d5ba" resname="ezrichtext.custom_tags.eztwitter.attributes.lang.label"> | ||
| <source>Language code</source> | ||
| <target>Language code</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.lang.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="9dfc2154505a7ab82c6ab07c110e0f6170513fc9" resname="ezrichtext.custom_tags.eztwitter.attributes.link_color.label"> | ||
| <source>Link color</source> | ||
| <target>Link color</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.link_color.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="708de039c84f0c302306526df2a893cdc5f3ba88" resname="ezrichtext.custom_tags.eztwitter.attributes.theme.choice.dark.label"> | ||
| <source>Dark</source> | ||
| <target>Dark</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.theme.choice.dark.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="bc7cb500be683ba8dca30d588849d49acde30377" resname="ezrichtext.custom_tags.eztwitter.attributes.theme.choice.light.label"> | ||
| <source>Light</source> | ||
| <target>Light</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.theme.choice.light.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="38dcf6fd48a4264ead1cce0e8e69f71211fa7670" resname="ezrichtext.custom_tags.eztwitter.attributes.theme.label"> | ||
| <source>Theme</source> | ||
| <target>Theme</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.theme.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="4c8cbac4f038c15c7e71194a1af3726117d28279" resname="ezrichtext.custom_tags.eztwitter.attributes.tweet_url.label"> | ||
| <source>Tweet URL</source> | ||
| <target>Tweet URL</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.tweet_url.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="47da3c994119e5c6e00e9e9fe61495155e8427c2" resname="ezrichtext.custom_tags.eztwitter.attributes.width.label"> | ||
| <source>Width</source> | ||
| <target>Width</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.attributes.width.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="c1fd25ffc048cf47e9bc6bcaae98dd86abb2a7dd" resname="ezrichtext.custom_tags.eztwitter.description"> | ||
| <source>Embed X post</source> | ||
| <target>Embed X post</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.description</note> | ||
| </trans-unit> | ||
| <trans-unit id="3d7387b1935640d467dc4070c0a9fe4c35d81e52" resname="ezrichtext.custom_tags.eztwitter.label"> | ||
| <source>X</source> | ||
| <target>X</target> | ||
| <note>key: ezrichtext.custom_tags.eztwitter.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="605cb001f1553455c945e45234d73b47cf1417b1" resname="ezrichtext.custom_tags.ezyoutube.attributes.autoplay.label"> | ||
| <source>Autoplay</source> | ||
| <target>Autoplay</target> | ||
| <note>key: ezrichtext.custom_tags.ezyoutube.attributes.autoplay.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="7bc17dbacf6bbe39325fe1e60e12c37f6d791657" resname="ezrichtext.custom_tags.ezyoutube.attributes.height.label"> | ||
| <source>Height</source> | ||
| <target>Height</target> | ||
| <note>key: ezrichtext.custom_tags.ezyoutube.attributes.height.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="65fc0855d568f69246fa41e8f447617d3ec051e3" resname="ezrichtext.custom_tags.ezyoutube.attributes.video_url.label"> | ||
| <source>Video URL</source> | ||
| <target>Video URL</target> | ||
| <note>key: ezrichtext.custom_tags.ezyoutube.attributes.video_url.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="aea37ec43e2230a8627b214d93cb548de4e81d1f" resname="ezrichtext.custom_tags.ezyoutube.attributes.width.label"> | ||
| <source>Width</source> | ||
| <target>Width</target> | ||
| <note>key: ezrichtext.custom_tags.ezyoutube.attributes.width.label</note> | ||
| </trans-unit> | ||
| <trans-unit id="8f9f4c953fc856ffdd89f2584789096bc29cd806" resname="ezrichtext.custom_tags.ezyoutube.description"> | ||
| <source>Embed YouTube video</source> | ||
| <target>Embed YouTube video</target> | ||
| <note>key: ezrichtext.custom_tags.ezyoutube.description</note> | ||
| </trans-unit> | ||
| <trans-unit id="522f743b7ab00a525b5d5501de4c59bdeca967a4" resname="ezrichtext.custom_tags.ezyoutube.label"> | ||
| <source>YouTube</source> | ||
| <target>YouTube</target> | ||
| <note>key: ezrichtext.custom_tags.ezyoutube.label</note> | ||
| </trans-unit> | ||
| </body> | ||
| </file> | ||
| </xliff> | ||
81 changes: 81 additions & 0 deletions
81
src/lib/Translation/Extractor/ChoiceAttributeExtractor.php
This file contains hidden or 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,81 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\FieldTypeRichText\Translation\Extractor; | ||
|
|
||
| use JMS\TranslationBundle\Model\MessageCatalogue; | ||
| use JMS\TranslationBundle\Translation\ExtractorInterface; | ||
|
|
||
| /** | ||
| * Generates translation strings for choice custom tag attribute. | ||
| */ | ||
| final class ChoiceAttributeExtractor implements ExtractorInterface | ||
| { | ||
| private const CHOICE_ATTRIBUTE_TYPE = 'choice'; | ||
| private const CHOICE_LABEL_KEY = 'ezrichtext.custom_tags.%s.attributes.%s.choice.%s.label'; | ||
|
|
||
| /** @var array<string, mixed> */ | ||
| private array $customTags; | ||
|
|
||
| private string $domain; | ||
|
|
||
| /** @var string[] */ | ||
| private array $whitelist; | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $customTags Custom tags definitions | ||
| * @param string $domain Target translation domain | ||
| * @param string[] $whitelist Whitelist of custom tags to extract | ||
| */ | ||
| public function __construct(array $customTags, string $domain, array $whitelist = []) | ||
| { | ||
| $this->customTags = $customTags; | ||
| $this->domain = $domain; | ||
| $this->whitelist = $whitelist; | ||
| } | ||
|
|
||
| public function extract(): MessageCatalogue | ||
| { | ||
| $catalogue = new MessageCatalogueBuilder($this->domain); | ||
| foreach ($this->customTags as $tagName => $customTag) { | ||
| if (!in_array($tagName, $this->whitelist, true)) { | ||
| continue; | ||
| } | ||
|
|
||
| $attributes = $customTag['attributes'] ?? []; | ||
| foreach ($attributes as $attributeName => $attribute) { | ||
| $type = $attribute['type'] ?? null; | ||
| if ($type !== self::CHOICE_ATTRIBUTE_TYPE) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($attribute['choices'] as $choice) { | ||
| if (empty($choice)) { | ||
| continue; | ||
| } | ||
|
|
||
| $this->addChoiceLabelMessage($catalogue, $tagName, $attributeName, $choice); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $catalogue->getCatalogue(); | ||
| } | ||
|
|
||
| private function addChoiceLabelMessage( | ||
| MessageCatalogueBuilder $catalogue, | ||
| string $tagName, | ||
| string $attributeName, | ||
| string $choice | ||
| ): void { | ||
| $catalogue->addMessage( | ||
| sprintf(self::CHOICE_LABEL_KEY, $tagName, $attributeName, $choice), | ||
| $choice | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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,84 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\FieldTypeRichText\Translation\Extractor; | ||
|
|
||
| use JMS\TranslationBundle\Model\MessageCatalogue; | ||
| use JMS\TranslationBundle\Translation\ExtractorInterface; | ||
|
|
||
| /** | ||
| * Generates translation strings for custom tags. | ||
| */ | ||
| final class CustomTagExtractor implements ExtractorInterface | ||
| { | ||
| private const CUSTOM_TAG_LABEL = 'ezrichtext.custom_tags.%s.label'; | ||
| private const CUSTOM_TAG_DESCRIPTION = 'ezrichtext.custom_tags.%s.description'; | ||
| private const ATTRIBUTE_LABEL = 'ezrichtext.custom_tags.%s.attributes.%s.label'; | ||
|
|
||
| /** @var array<string, mixed> */ | ||
| private array $customTags; | ||
|
|
||
| private string $domain; | ||
|
|
||
| /** @var string[] */ | ||
| private array $whitelist; | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $customTags Custom tags definitions | ||
| * @param string $domain Target translation domain | ||
| * @param string[] $whitelist Whitelist of custom tags to extract | ||
| */ | ||
| public function __construct(array $customTags, string $domain, array $whitelist = []) | ||
| { | ||
| $this->customTags = $customTags; | ||
| $this->domain = $domain; | ||
| $this->whitelist = $whitelist; | ||
| } | ||
|
|
||
| public function extract(): MessageCatalogue | ||
| { | ||
| $catalogue = new MessageCatalogueBuilder($this->domain); | ||
| foreach ($this->customTags as $tagName => $config) { | ||
| if (!in_array($tagName, $this->whitelist, true)) { | ||
| continue; | ||
| } | ||
|
|
||
| $this->addCustomTagLabelMessage($catalogue, $tagName); | ||
| $this->addCustomTagDescriptionMessage($catalogue, $tagName); | ||
|
|
||
| /** @var string[] $attributes */ | ||
| $attributes = array_keys($config['attributes'] ?? []); | ||
| foreach ($attributes as $attributeName) { | ||
| $this->addAttributeLabelMessage($catalogue, $tagName, $attributeName); | ||
| } | ||
| } | ||
|
|
||
| return $catalogue->getCatalogue(); | ||
| } | ||
|
|
||
| private function addCustomTagLabelMessage(MessageCatalogueBuilder $catalogue, string $tagName): void | ||
| { | ||
| $catalogue->addMessage(sprintf(self::CUSTOM_TAG_LABEL, $tagName), $tagName); | ||
| } | ||
|
|
||
| private function addCustomTagDescriptionMessage(MessageCatalogueBuilder $catalogue, string $tagName): void | ||
| { | ||
| $catalogue->addMessage(sprintf(self::CUSTOM_TAG_DESCRIPTION, $tagName), $tagName); | ||
| } | ||
|
|
||
| private function addAttributeLabelMessage( | ||
| MessageCatalogueBuilder $catalogue, | ||
| string $tagName, | ||
| string $attributeName | ||
| ): void { | ||
| $catalogue->addMessage( | ||
| sprintf(self::ATTRIBUTE_LABEL, $tagName, $attributeName), | ||
| $attributeName | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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,60 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\FieldTypeRichText\Translation\Extractor; | ||
|
|
||
| use JMS\TranslationBundle\Model\Message\XliffMessage; | ||
| use JMS\TranslationBundle\Model\MessageCatalogue; | ||
|
|
||
| /** | ||
| * Utility class for building translation messages catalogue. | ||
| */ | ||
| final class MessageCatalogueBuilder | ||
| { | ||
| private string $domain; | ||
|
|
||
| private MessageCatalogue $catalogue; | ||
|
|
||
| public function __construct(string $domain) | ||
| { | ||
| $this->domain = $domain; | ||
| $this->catalogue = new MessageCatalogue(); | ||
| } | ||
|
|
||
| public function getDomain(): string | ||
| { | ||
| return $this->domain; | ||
| } | ||
|
|
||
| public function reset(): void | ||
| { | ||
| $this->catalogue = new MessageCatalogue(); | ||
| } | ||
|
|
||
| public function getCatalogue(): MessageCatalogue | ||
| { | ||
| return $this->catalogue; | ||
| } | ||
|
|
||
| public function addMessage(string $id, string $desc): void | ||
| { | ||
| $this->catalogue->add($this->createMessage($id, $desc)); | ||
| } | ||
|
|
||
| private function createMessage(string $id, string $desc): XliffMessage | ||
| { | ||
| $message = new XliffMessage($id, $this->domain); | ||
| $message->setNew(false); | ||
| $message->setMeaning($desc); | ||
| $message->setDesc($desc); | ||
| $message->setLocaleString($desc); | ||
| $message->addNote('key: ' . $id); | ||
|
|
||
| return $message; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.