[英]Localizing static segment of routePath in TYPO3 RouteEnhancer
從TYPO3 9.5版開始, RouteEnhancer
可以用於將擴展名的參數轉換為美觀且易於RouteEnhancer
的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
的靜態配置路徑段,以便將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;
在那里定義自己的類可以提供其他功能。
可以定義其他哪些與路由相關的類?
路由機制非常復雜,因此存在多個可以輕松替換的類。
要獲得預定義和可替換類的概述,您可以驗證
通過打開TYPO3后端中的數組$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']
模塊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;
目前,我不需要編寫自己的解決方案,但是如果您已經編寫了一個解決方案,請隨時發布一個作為答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.