简体   繁体   中英

Localizing static segment of routePath in TYPO3 RouteEnhancer

Since TYPO3 Version 9.5 RouteEnhancer s can be used to translate parameters of extensions to nice and human-readable URL-paths.

An example configuration for the extension news is this:

routeEnhancers:
  News:
    type: Extbase
    extension: News
    plugin: Pi1
    routes:
      -
        routePath: '/page/{page}'
        _controller: 'News::list'
        _arguments:
          page: '@widget_0/currentPage'
      -
        routePath: '/article/{news_title}'
        _controller: 'News::detail'
        _arguments:
          news_title: news
      -
        routePath: '/category/{category_name}'
        _controller: 'News::list'
        _arguments:
          category_name: overwriteDemand/categories
      -
        routePath: '/tag/{tag_name}'
        _controller: 'News::list'
        _arguments:
          tag_name: overwriteDemand/tags
    defaultController: 'News::list'
    defaults:
      page: '0'
    requirements:
      news_title: '^[a-zA-Z0-9].*$'
      page: \d+
    aspects:
      news_title:
        type: PersistedAliasMapper
        tableName: tx_news_domain_model_news
        routeFieldName: path_segment
      page:
        type: StaticRangeMapper
        start: '1'
        end: '100'
      category_name:
        type: PersistedAliasMapper
        tableName: sys_category
        routeFieldName: title
      tag_name:
        type: PersistedAliasMapper
        tableName: tx_news_domain_model_tag
        routeFieldName: title

My question is:
How is it possible to localize the static configured path-segments of routePath s above, so that page , article , category and tag are translated to the current language?

Please look at the article https://typo3worx.eu/2018/12/typo3-routing-extensions-and-enhancers for the paragraph "Localemodifier". (The blog is a treasure chest of useful information ;) ).

The essential information related to your question is this:

LOCALEMODIFIER
The LocaleModifier “translates” parts of an url between the languages. This is useful if there are static strings in the url, that should look different in the various languages. An example can be a product database, like the following example:

 routeEnhancers: NewsArchive: type: Extbase limitToPages: [13] extension: MyProducts plugin: Pi1 routes: - { routePath: '/{localized_product}/{product}', _controller: 'MyProducts::detail' } defaultController: 'MyProducts::list' aspects: localized_product: type: LocaleModifier default: 'product' localeMap: - locale: 'fr_FR.*|fr_CA.*' value: 'produit' - locale: 'de_DE.*' value: 'produkt' - locale: 'es_ES.*' value: 'producto' 

I don't know if it is possible to use a locallang.xlf file for this kind of mapping between the static part of the route and the languages used.

Simple translations according to my question can be added like @Ricardo discovered for me, look for his answer on this page.

More complicated things are only possible by replacing (at least) one class.

Replacement is quite easy and the danger to get some incompatibilities in future by introducing own code beside the core is quite small as the methods in the existing class are quite limited.

So, how to replace classes related to RouteEnhancers ?

All important classes are referenced in the global array $GLOBALS['TYPO3_CONF_VARS'] and can be defined in the files typo3conf/LocalConfiguration.php or typo3conf/AdditionalConfiguration.php .
The class which is needed for the mapping of localized values to defined locals (ie de_DE, en_GB, en_US) is registered like this:

$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['LocaleModifier'] = \TYPO3\CMS\Core\Routing\Aspect\LocaleModifier::class;

Defining there an own class offers the option to provide additional functionality.

Which other routing related classes can be defined?
The routing mechanism is quite complex, so there exist several classes that can be easily replaced.
To get an overview of the predefined and replaceable classes you can verify the
array $GLOBALS['TYPO3_CONF_VARS']['SYS']['routing'] in the backend of TYPO3 by opening
the module System -> Configuration and there in the top of the page in the drop-down-field chose $GLOBALS['TYPO3_CONF_VARS'] (Global Configuration) .

In the current version TYPO3-9.5.5 the following default-classes are configured

// Aspects
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['LocaleModifier'] = TYPO3\CMS\Core\Routing\Aspect\LocaleModifier::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['PersistedAliasMapper'] = TYPO3\CMS\Core\Routing\Aspect\PersistedAliasMapper::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['PersistedPatternMapper'] = TYPO3\CMS\Core\Routing\Aspect\PersistedPatternMapper::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['StaticRangeMapper'] = TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['StaticValueMapper'] = TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper::class;

// Enhancers
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['Extbase'] = TYPO3\CMS\Extbase\Routing\ExtbasePluginEnhancer::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['PageType'] = TYPO3\CMS\Core\Routing\Enhancer\PageTypeDecorator::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['Plugin'] = TYPO3\CMS\Core\Routing\Enhancer\PluginEnhancer::class;
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers']['Simple'] = TYPO3\CMS\Core\Routing\Enhancer\SimpleEnhancer::class;

Currently I don't see a need to program my own solution, but feel free to post one as answer if you've programmed one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM