Add workaround for translations for of opt-in block in editor

We currently don't have tooling for passing translations from scripts to wp.i18n.
This solution provides a temporary way of passing translated strings to wp.i18n for scripts that are
used outside admin pages that are handled via MailPoet.
[MAILPOET-3920]
This commit is contained in:
Rostislav Wolny
2021-11-17 15:39:06 +01:00
committed by Veljko V
parent f95bd95923
commit b137e3fb0c
3 changed files with 34 additions and 6 deletions

View File

@@ -20,14 +20,11 @@ const { optinEnabled, defaultText } = getSetting('mailpoet_data');
const EmptyState = (): JSX.Element => (
<Placeholder
icon={<Icon icon={megaphone} />}
label={__('Marketing opt-in', 'mailpoet')}
label={__('marketing-opt-in-label', 'mailpoet')}
className="wp-block-mailpoet-newsletter-block-placeholder"
>
<span className="wp-block-mailpoet-newsletter-block-placeholder__description">
{__(
'MailPoet marketing opt-in would be shown here if enabled. You can enable from the settings page.',
'mailpoet'
)}
{__('marketing-opt-in-not-shown', 'mailpoet')}
</span>
<Button
isPrimary
@@ -36,7 +33,7 @@ const EmptyState = (): JSX.Element => (
rel="noopener noreferrer"
className="wp-block-mailpoet-newsletter-block-placeholder__button"
>
{__('Enable opt-in for Checkout', 'mailpoet')}
{__('marketing-opt-in-enable', 'mailpoet')}
</Button>
</Placeholder>
);

View File

@@ -48,6 +48,7 @@ class MarketingOptinBlock implements IntegrationInterface {
$script_asset['version'],
true
);
$this->registerEditorTranslations();
}
/**
@@ -76,4 +77,27 @@ class MarketingOptinBlock implements IntegrationInterface {
public function get_script_data() { // phpcs:ignore
return $this->options;
}
/**
* Workaround for registering script translations.
* Currently, we don't generate translation files for scripts. This method enqueues an inline script
* that renders same output as a script translation file would render, when rendered via wp_scripts()->print_translations.
* Note that keys need to match strings in JS files
*/
private function registerEditorTranslations() {
$handle = 'mailpoet-marketing-optin-block-editor-script';
$editorTranslations = $this->wp->getWpScripts()->print_translations($handle, false);
// mailpoet-marketing-optin-block-editor-script is not enqueued
if ($editorTranslations === false) {
return;
}
$translations = [
'' => ['domain' => 'messages'],
'marketing-opt-in-label' => [__('Marketing opt-in', 'mailpoet')],
'marketing-opt-in-not-shown' => [__('MailPoet marketing opt-in would be shown here if enabled. You can enable from the settings page.', 'mailpoet')],
'marketing-opt-in-enable' => [__('Enable opt-in for Checkout', 'mailpoet')],
];
$editorTranslations = str_replace('{ "messages": { "": {} }', '{ "messages": ' . json_encode($translations), $editorTranslations);
$this->wp->wpAddInlineScript($handle, $editorTranslations, 'before');
}
}

View File

@@ -752,4 +752,11 @@ class Functions {
public function wpSetScriptTranslations(string $handle, string $domain = 'default', string $path = null): bool {
return wp_set_script_translations($handle, $domain, $path);
}
/**
* @return \WP_Scripts
*/
public function getWpScripts() {
return wp_scripts();
}
}