在 TYPO3 RouteEnhancer 中本地化 routePath 的静态段

Localizing static segment of routePath in TYPO3 RouteEnhancer

从 TYPO3 版本 9.5 开始,RouteEnhancers 可用于将扩展参数转换为漂亮且人类可读的 URL-路径。

扩展 news 的示例配置是这样的:

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

我的问题是:
如何本地化上面 routePaths 的静态配置路径段,以便 pagearticlecategorytag 被翻译成当前语言?

请查看文章 https://typo3worx.eu/2018/12/typo3-routing-extensions-and-enhancers 中的段落 "Localemodifier"。 (博客是有用信息的宝库;))。

与您的问题相关的基本信息是:

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'

我不知道是否可以使用 locallang.xlf 文件来实现路由的静态部分和所用语言之间的这种映射。

可以根据我的问题添加简单的翻译,就像@Ricardo 为我发现的那样,在这个页面上寻找他的答案。

更复杂的事情只能通过替换(至少)一个 class.

替换非常容易,并且由于现有 class 中的方法非常有限,因此将来通过在核心旁边引入自己的代码而导致一些不兼容的危险非常小。

那么,如何替换与RouteEnhancers相关的classes?

所有重要的 class 都在全局数组 $GLOBALS['TYPO3_CONF_VARS'] 中引用,并且可以在文件 typo3conf/LocalConfiguration.phptypo3conf/AdditionalConfiguration.php.
中定义 class 将本地化值映射到定义的局部变量(即 de_DE、en_GB、en_US)所需的注册方式如下:

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

在那里定义自己的 class 提供提供附加功能的选项。

还有哪些路由相关的 class 可以定义?
路由机制相当复杂,因此有几个 class 可以轻松替换。
要大致了解预定义和可替换的 classes,您可以验证
通过打开
在 TYPO3 的后端数组 $GLOBALS['TYPO3_CONF_VARS']['SYS']['routing'] 模块 System -> Configuration 并在页面顶部的下拉字段中选择 $GLOBALS['TYPO3_CONF_VARS'] (Global Configuration).

当前版本TYPO3-9.5.5配置了以下默认-classes

// 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;

目前我认为没有必要编写自己的解决方案,但如果您编写了一个解决方案,请随时 post 作为答案。