Skip to content
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

Added translation extractor for custom tags #162

Merged
merged 8 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.php_cs.cache
node_modules/
yarn.lock
/var
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ private function prependJMSTranslation(ContainerBuilder $container): void
'output_dir' => __DIR__ . '/../Resources/translations/',
'output_format' => 'xliff',
'excluded_dirs' => ['Behat', 'Tests', 'node_modules'],
'extractors' => [],
'extractors' => [
'ibexa.translation_extractor.field_type.ezrichtext.custom_tags',
'ibexa.translation_extractor.field_type.ezrichtext.custom_tags.choices',
],
],
],
]);
Expand Down
19 changes: 19 additions & 0 deletions src/bundle/Resources/config/translation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,26 @@ services:
public: false

Ibexa\FieldTypeRichText\Translation\Extractor\OnlineEditorCustomAttributesExtractor:
deprecated: 'Since ibexa/fieldtype-richtext 4.6.7 the "%service_id%" service is deprecated, will be removed in 5.0.0'
arguments:
$siteAccessList: '%ibexa.site_access.list%'
tags:
- { name: jms_translation.extractor, alias: ez_online_editor_attributes }

Ibexa\FieldTypeRichText\Translation\Extractor\CustomTagExtractor:
arguments:
$customTags: '%ibexa.field_type.richtext.custom_tags%'
$domain: '%ibexa.field_type.richtext.custom_tags.translation_domain%'
$allowlist: ['ezyoutube', 'eztwitter', 'ezfacebook']
tags:
- name: jms_translation.extractor
alias: ibexa.translation_extractor.field_type.ezrichtext.custom_tags

Ibexa\FieldTypeRichText\Translation\Extractor\ChoiceAttributeExtractor:
arguments:
$customTags: '%ibexa.field_type.richtext.custom_tags%'
$domain: '%ibexa.field_type.richtext.custom_tags.translation_domain%'
$allowlist: ['ezyoutube', 'eztwitter', 'ezfacebook']
tags:
- name: jms_translation.extractor
alias: ibexa.translation_extractor.field_type.ezrichtext.custom_tags.choices
131 changes: 131 additions & 0 deletions src/bundle/Resources/translations/custom_tags.en.xliff
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>Don't target</source>
<target>Don't target</target>
<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 src/lib/Translation/Extractor/ChoiceAttributeExtractor.php
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 $allowlist;

/**
* @param array<string, mixed> $customTags Custom tags definitions
* @param string $domain Target translation domain
* @param string[] $allowlist Whitelist of custom tags to extract
*/
public function __construct(array $customTags, string $domain, array $allowlist = [])
{
$this->customTags = $customTags;
$this->domain = $domain;
$this->allowlist = $allowlist;
}

public function extract(): MessageCatalogue
{
$catalogue = new MessageCatalogueBuilder($this->domain);
foreach ($this->customTags as $tagName => $customTag) {
if (!in_array($tagName, $this->allowlist, 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
);
}
}
84 changes: 84 additions & 0 deletions src/lib/Translation/Extractor/CustomTagExtractor.php
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 $allowlist;

/**
* @param array<string, mixed> $customTags Custom tags definitions
* @param string $domain Target translation domain
* @param string[] $allowlist Whitelist of custom tags to extract
*/
public function __construct(array $customTags, string $domain, array $allowlist = [])
{
$this->customTags = $customTags;
$this->domain = $domain;
$this->allowlist = $allowlist;
}

public function extract(): MessageCatalogue
{
$catalogue = new MessageCatalogueBuilder($this->domain);
foreach ($this->customTags as $tagName => $config) {
if (!in_array($tagName, $this->allowlist, 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
);
}
}
Loading
Loading