Skip to content

Commit

Permalink
Added translation extractor for custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed May 28, 2024
1 parent 28aa901 commit 9957cf4
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ private function prependJMSTranslation(ContainerBuilder $container): void
'output_dir' => __DIR__ . '/../Resources/translations/',
'output_format' => 'xliff',
'excluded_dirs' => ['Behat', 'Tests', 'node_modules'],
'extractors' => [],
'extractors' => [
'ibexa.field_type.richtext.translation_extractor.custom_tags',
],
],
],
]);
Expand Down
8 changes: 8 additions & 0 deletions src/bundle/Resources/config/translation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ services:
$siteAccessList: '%ibexa.site_access.list%'
tags:
- { name: jms_translation.extractor, alias: ez_online_editor_attributes }

Ibexa\FieldTypeRichText\Translation\Extractor\OnlineEditorCustomTagExtractor:
arguments:
$customTags: '%ibexa.field_type.richtext.custom_tags%'
$domain: '%ibexa.field_type.richtext.custom_tags.translation_domain%'
$filter: ['ezyoutube', 'eztwitter', 'ezfacebook']
tags:
- { name: jms_translation.extractor, alias: ibexa.field_type.richtext.translation_extractor.custom_tags }
111 changes: 111 additions & 0 deletions src/bundle/Resources/translations/custom_tags.en.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?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="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="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>
<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="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 (Twitter) post</source>
<target>Embed X (Twitter) post</target>
<note>key: ezrichtext.custom_tags.eztwitter.description</note>
</trans-unit>
<trans-unit id="3d7387b1935640d467dc4070c0a9fe4c35d81e52" resname="ezrichtext.custom_tags.eztwitter.label">
<source>X (Twitter)</source>
<target>X (Twitter)</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>
108 changes: 108 additions & 0 deletions src/lib/Translation/Extractor/OnlineEditorCustomTagExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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;
use JMS\TranslationBundle\Translation\ExtractorInterface;

/**
* Generates translation strings for custom tags.
*/
final class OnlineEditorCustomTagExtractor 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_KEY = 'ezrichtext.custom_tags.%s.attributes.%s.label';

/** @var array<string, mixed> */
private array $customTags;

private string $domain;

/** @var string[] */
private array $filter;

/**
* @param array<string, mixed> $customTags
* @param string[] $filter
*/
public function __construct(array $customTags, string $domain, array $filter = [])
{
$this->customTags = $customTags;
$this->domain = $domain;
$this->filter = $filter;
}

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

private function addCustomTagLabelMessage(MessageCatalogue $catalogue, string $tagName): void
{
$message = $this->createMessage(
sprintf(self::CUSTOM_TAG_LABEL, $tagName),
$tagName
);

$catalogue->add($message);
}

private function addCustomTagDescriptionMessage(MessageCatalogue $catalogue, string $tagName): void
{
$message = $this->createMessage(
sprintf(self::CUSTOM_TAG_DESCRIPTION, $tagName),
$tagName
);

$catalogue->add($message);
}

private function addAttributeLabelMessage(
MessageCatalogue $catalogue,
string $tagName,
string $attributeName
): void {
$message = $this->createMessage(
sprintf(self::ATTRIBUTE_LABEL_KEY, $tagName, $attributeName),
$attributeName
);

$catalogue->add($message);
}

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;
}
}

0 comments on commit 9957cf4

Please sign in to comment.