diff --git a/Classes/EelHelper/BackendHelper.php b/Classes/EelHelper/BackendHelper.php new file mode 100644 index 0000000..d7de355 --- /dev/null +++ b/Classes/EelHelper/BackendHelper.php @@ -0,0 +1,96 @@ +userService->getInterfaceLanguage(); + } + + /** + * Get the translated value for an id or original label in the interface language + * + * @param string $id + * @param string|null $originalLabel + * @param array $arguments + * @param string $source + * @param string|null $package + * @param integer|null $quantity + * @param string|null $locale + * @return string + * @throws Exception + */ + public function translate($id, $originalLabel = null, array $arguments = [], $source = 'Main', $package = null, $quantity = null, $locale = null): string + { + if ($locale === null) { + $locale = $this->userService->getInterfaceLanguage(); + } + + if ( + $originalLabel === null && + $arguments === [] && + $source === 'Main' && + $package === null && + $quantity === null + ) { + return preg_match(self::I18N_LABEL_ID_PATTERN, $id) === 1 ? $this->translateByShortHandString($id, $locale) : $id; + } + + return $this->translationHelper->translate($id, $originalLabel, $arguments, $source, $package, $quantity, $locale); + } + + /** + * @param string $shortHandString + * @return string + * @throws Exception + */ + protected function translateByShortHandString($shortHandString, $locale) + { + $shortHandStringParts = explode(':', $shortHandString); + if (count($shortHandStringParts) === 3) { + list($package, $source, $id) = $shortHandStringParts; + return $this->translationHelper->translate($id, null, [], $source, $package, null, $locale); + } + + throw new \InvalidArgumentException(sprintf('The translation shorthand string "%s" has the wrong format', $shortHandString), 1613464966); + } + + /** + * All methods are considered safe + * + * @param string $methodName + * @return boolean + */ + public function allowsCallOfMethod($methodName) + { + return true; + } +} diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index a0b2566..67749fa 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -6,6 +6,7 @@ Neos: Carbon.FileContent: 'Carbon\Eel\EelHelper\FileContentHelper' Carbon.Array: 'Carbon\Eel\EelHelper\ArrayHelper' Carbon.Number: 'Carbon\Eel\EelHelper\NumberHelper' + Carbon.Backend: 'Carbon\Eel\EelHelper\BackendHelper' Neos: fusion: autoInclude: diff --git a/README.md b/README.md index ccedc4a..4b8f27d 100755 --- a/README.md +++ b/README.md @@ -375,6 +375,29 @@ Format a localized number with grouped thousands - `decimals` (integer, optional) Sets the number of decimal points, defaults to `0` - `locale` (string, optional) String locale - example (de_DE|en|ru_RU) +## Backend Helper + +### `Carbon.Backend.language()` + +Returns the language from the interface + +### `Carbon.Backend.translate(id, originalLabel, arguments, source, package, quantity, locale)` + +Get the translated value (in the language of the interface) for an id or original label. If only id is set and contains a translation shorthand string, translate according to that shorthand. + +In all other cases: +Replace all placeholders with corresponding values if they exist in the translated label. + +- `id` (string) Id to use for finding translation (trans-unit id in XLIFF) +- `originalLabel` (string, optional) The original translation value (the untranslated source string) +- `arguments` (array, optional) Array of numerically indexed or named values to be inserted into placeholders +- `source` (string, optional) Name of file with translations +- `package` (string, optional) Target package key +- `quantity` (mixed, optional) A number to find plural form for (float or int), NULL to not use plural forms +- `locale` (string, optional) An identifier of locale to use (NULL for use the interface language) + +Returns the ranslated label or source label / ID key + ## Installation Carbon.Eel is available via packagist. Just run