Tobias N.
Tobias N.

Reputation: 138

TYPO3 TypoScript: Language file with arguments

how can I render a locallang key with arguments in TypoScript? (replace the %s with a value)

<trans-unit id="author">
    <source>created by %s</source>
</trans-unit>

In Fluid it's done the following:

<f:translate key="author" arguments="{0:authorName}"/>

And now via TypoScript? I tried the following:

page.10 = TEXT    
page.10.dataWrap = {LLL:EXT:my_ext/Resources/Private/Language/locallang.xlf:author|'Me'}

=====
Solution 1 via UserFunc:

page.10 = USER_INT
page.10 {
    userFunc = FluidTranslate->main
    extensionName = my_ext
    key = tx_myext_domain_model_mymodel.author
    arguments.0 = Me
}

PHP:

<?php
class FluidTranslate
{
  public function main($content, $conf)
  {
    $extensionName = $conf['extensionName'];
    $key = $conf['key'];
    $arguments = $conf['arguments.'];

    $value = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, $extensionName, $arguments);
    return $value;
  }
}

Upvotes: 4

Views: 1408

Answers (5)

Daniel
Daniel

Reputation: 1085

One can use FLUIDTEMPLATE with inline template in order to use the ViewHelper:

page.10 = FLUIDTEMPLATE
page.10 {
    template = TEXT
    template = {f:translate(key: 'author', arguments: {0: authorName}, extensionName: 'my_ext')}
    variables {
        authorName = TEXT
        authorName {
            field = author_name
        }
    }
}

Upvotes: 0

Tobias N.
Tobias N.

Reputation: 138

Since I'm currently working with TYPO3 CMS 6.2.x the userFunc worked for me.
I extended the TypoScript cause I needed to react on dynamic values, based on CONTENT in combination with select.

TypoScript:

page.99 = CONTENT
page.99 {
  table = tt_content
  select {
    uidInList = 484
    pidInList = 156
  }
  selectFields = uid

  renderObj = COA
  renderObj {

    10 = USER
    10 {
        userFunc = FluidTranslate->main
        extensionName = my_ext
        key = tx_myext_domain_model_mymodelname.langkey
        fields.0 = uid
    }
  }
}

UserFunction:

<?php

class FluidTranslate
{

    /**
     * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
     */
    public $cObj = null;

    public function main($content, $conf)
    {
        $extensionName = $conf['extensionName'];
        $key = $conf['key'];
        $arguments = array();
        $fields = $conf['fields.'];

        foreach ($fields as $field) {
            $arguments[] = $this->cObj->data[$field];
        }


        $value = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, $extensionName, $arguments);
        return $value;
    }
}

Upvotes: 0

Claus Due
Claus Due

Reputation: 4261

Fluid templates support these markers (sprintf() style) because the TranslateViewHelper calls LocalizationUtility which calls vsprintf() on the resolved LLL.

However, this same processing is not available through TypoScript at the current time. Although it technically could be added as a new type of stdWrap function. Previously this feature has been suggested in a form that allowed calling any PHP function - but this was (reasonably so) rejected due to security concerns and the fact that TypoScript developers are second class citizens (compared to raw PHP) in TYPO3.

That being said there is ONE immediately available option that you could use to reproduce the behavior as an stdWrap:

There is a hook location at $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['stdWrap'] which allows you to register a class name which implements \TYPO3\CMS\Frontend\ContentObject\ContentObjectStdWrapHookInterface that can trigger on special stdWrap configurations and perform your sprintf() or vsprintf() call on the LLL value after it is resolved.

On a side note, if you complete such a feature, it would make a lot of sense to suggest/contribute it back to TYPO3. It appears to me to be a reasonable feature request!

Upvotes: 1

Susi
Susi

Reputation: 718

If your problem is only the %s part, then you could use stdwrap.replacement to process that:

  stdWrap.replacement {
    10 {
      search = %s
      replace = Me
    }
  }

See https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Replacement/Index.html

Upvotes: 4

Daniel
Daniel

Reputation: 7016

I don't think this is possible in the TEXT object yet, because for resolving the LLL: from TS the method TypoScriptFrotnendController->sL() is used.

You could however call a user function with the USER object and fetch your label there with LocalizationUtility::translate() where you can pass arguments.

Upvotes: 1

Related Questions