由于TYPO3版本9.5 RouteEnhancer
s可用于将扩展的参数转换为漂亮且人类可读的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
我的问题是:
如何定位上面routePath
s的静态配置路径段,以便将page
,article
,category
和tag
翻译成当前语言?
请查看文章https://typo3worx.eu/2018/12/typo3-routing-extensions-and-enhancers的“Localemodifier”段落。 (博客是有用信息的宝库;))。
与您的问题相关的基本信息是:
LOCALEMODIFIER LocaleModifier在语言之间“翻译”URL的部分。如果url中有静态字符串,这在各种语言中应该看起来不同,这很有用。示例可以是产品数据库,如以下示例所示:
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一样为我找到,在这个页面上寻找他的答案。
更复杂的事情只能通过替换(至少)一个类来实现。
替换非常简单,并且通过在核心旁边引入自己的代码来获得某些不兼容性的危险非常小,因为现有类中的方法非常有限。
那么,如何替换与RouteEnhancers
相关的类?
所有重要的类都在全局数组$GLOBALS['TYPO3_CONF_VARS']
中引用,并且可以在文件typo3conf/LocalConfiguration.php
或typo3conf/AdditionalConfiguration.php
中定义。
将本地化值映射到定义的本地(即de_DE,en_GB,en_US)所需的类注册如下:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects']['LocaleModifier'] = \TYPO3\CMS\Core\Routing\Aspect\LocaleModifier::class;
定义一个自己的类提供了提供附加功能的选项。
可以定义哪些其他路由相关类?
路由机制非常复杂,因此存在几个可以轻松替换的类。
要获得预定义和可替换类的概述,您可以验证
阵容$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']
在TYPO3的后端打开
模块System
- > Configuration
,在页面顶部的下拉字段中选择了$GLOBALS['TYPO3_CONF_VARS'] (Global Configuration)
。
在当前版本TYPO3-9.5.5中,配置了以下默认类
// 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;
目前我认为不需要编写我自己的解决方案,但如果您编写了一个解决方案,请随意发布一个作为答案。