Skip to content

Commit

Permalink
New: Add backend helper
Browse files Browse the repository at this point in the history
Carbon.Backend.language(): Return interface language
Carbon.Backend.translate(): Translate string in the interface language
  • Loading branch information
jonnitto committed Feb 17, 2021
1 parent 523c647 commit 92c7ce9
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Classes/EelHelper/BackendHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Carbon\Eel\EelHelper;

use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Exception;
use Neos\Flow\I18n\EelHelper\TranslationHelper;
use Neos\Neos\Service\UserService;

class BackendHelper implements ProtectedContextAwareInterface
{

const I18N_LABEL_ID_PATTERN = '/^[a-z0-9]+\.(?:[a-z0-9][\.a-z0-9]*)+:[a-z0-9.]+:.+$/i';

/**
* @Flow\Inject
* @var UserService
*/
protected $userService;

/**
* @Flow\Inject
* @var TranslationHelper
*/
protected $translationHelper;

/**
* Returns the language from the interface
*
* @return string
*/
public function language(): string
{
return $this->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;
}
}
1 change: 1 addition & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 92c7ce9

Please sign in to comment.