From f53f9da2fcc44c37890b96619c0d9197f86201bb Mon Sep 17 00:00:00 2001 From: August Miller Date: Fri, 28 Jul 2023 12:37:18 -0700 Subject: [PATCH 1/2] Refactor message output, add Twig target --- src/BaseGenerator.php | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/BaseGenerator.php b/src/BaseGenerator.php index 7835b34..d51c09a 100644 --- a/src/BaseGenerator.php +++ b/src/BaseGenerator.php @@ -733,13 +733,44 @@ protected function namespacePath(string $namespace): string protected function messagePhp(string $message): string { $messagePhp = var_export($message, true); - $category = match (true) { + $category = $this->getTranslationCategory(); + + return $category ? sprintf("Craft::t('%s', %s)", $category, $messagePhp) : $messagePhp; + } + + /** + * Places a string into a Twig translation statement. + * + * Output is not enclosed in any `{{ ... }}` or `{% ... %}` tags. + * + * @param string $message The string to output + * @return string Twig statement + */ + protected function messageTwig(string $message): string + { + $messageTwig = var_export($message, true); + $category = $this->getTranslationCategory(); + + return $category ? sprintf("'%1\$s\'|t('%2\$s\', %1\$s)", $category, $messageTwig) : $messageTwig; + } + + /** + * Resolves an appropriate translation category for the target module/component. + * + * The result is not guaranteed to be valid: + * - Modules can register translation categories with any handle/ID they like (and the ID of a module can change at any time); + * - Plugins may register additional translation categories (non-standard) that we don’t/can’t know about; + * - The `site` translation category used by the front-end (and some modules) is not taken into consideration; + * + * @return string|null Translation category handle + */ + protected function getTranslationCategory(): ?string + { + return match (true) { $this->module instanceof Application => 'app', $this->module instanceof PluginInterface => $this->module->id, default => null, }; - - return $category ? sprintf("Craft::t('%s', %s)", $category, $messagePhp) : $messagePhp; } /** From 2f360aa2299102b577308829b8a53ba6809bded7 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Thu, 10 Aug 2023 18:17:39 -0700 Subject: [PATCH 2/2] Cleanup + release note --- CHANGELOG.md | 1 + src/BaseGenerator.php | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6190f2..3953217 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Release Notes for Craft Generator ## Unreleased +- Added `craft\generator\BaseGenerator::messageTwig()`. ([#25](https://github.com/craftcms/generator/pull/25)) - The plugin generator now ensures that the project’s `composer.json` file has `minimum-stability: dev` and `prefer-stable: true` set. ([#22](https://github.com/craftcms/generator/issues/22)) - Fixed a bug where the Service generator was suggesting adding a `config()` method which defined the service config, even when the target was a module. ([#24](https://github.com/craftcms/generator/issues/24)) diff --git a/src/BaseGenerator.php b/src/BaseGenerator.php index d51c09a..571040b 100644 --- a/src/BaseGenerator.php +++ b/src/BaseGenerator.php @@ -733,7 +733,7 @@ protected function namespacePath(string $namespace): string protected function messagePhp(string $message): string { $messagePhp = var_export($message, true); - $category = $this->getTranslationCategory(); + $category = $this->translationCategory(); return $category ? sprintf("Craft::t('%s', %s)", $category, $messagePhp) : $messagePhp; } @@ -745,11 +745,12 @@ protected function messagePhp(string $message): string * * @param string $message The string to output * @return string Twig statement + * @since 1.6.0 */ protected function messageTwig(string $message): string { $messageTwig = var_export($message, true); - $category = $this->getTranslationCategory(); + $category = $this->translationCategory(); return $category ? sprintf("'%1\$s\'|t('%2\$s\', %1\$s)", $category, $messageTwig) : $messageTwig; } @@ -764,7 +765,7 @@ protected function messageTwig(string $message): string * * @return string|null Translation category handle */ - protected function getTranslationCategory(): ?string + private function translationCategory(): ?string { return match (true) { $this->module instanceof Application => 'app',