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

@@ -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');
}
}