SilverStripe 覆盖 URLSegmentFilter 静态
SilverStripe overwriting URLSegmentFilter static
URLSegmentFilter
有一个静态数组 $default_replacements
,其中包含用于将 & 符号转换为(从 & 到 -and-)的字符串URL的。
我正在尝试扩展 class 并将此静态覆盖为 translate 符号转换(只有值是英语 and).
如何覆盖此目标的所有者静态?
class URLSegmentFilterExtension extends Extension {
private static $default_replacements = array(
'/&/u' => '-and-', // I need to translate this using _t()
'/&/u' => '-and-', // And this one
'/\s|\+/u' => '-',
'/[_.]+/u' => '-',
'/[^A-Za-z0-9\-]+/u' => '',
'/[\/\?=#]+/u' => '-',
'/[\-]{2,}/u' => '-',
'/^[\-]+/u' => '',
'/[\-]+$/u' => ''
);
}
首先:URLSegmentFilter
主要在CMS 上下文中运行,您通常只有一个语言环境(取决于编辑会员的设置)。所以单独使用 _t()
可能不是很有帮助?因此,您可能必须获取当前的编辑语言环境(假设您使用的是 Fluent 或 Translatable)并临时设置翻译语言环境。
我没有看到通过那里的扩展挂接翻译的方法。我认为你最好创建一个自定义子类并通过 Injector 使用它。
像这样的东西应该可以工作:
<?php
class TranslatedURLSegmentFilter extends URLSegmentFilter
{
public function getReplacements()
{
$currentLocale = i18n::get_locale();
$contentLocale = Translatable::get_current_locale();
// temporarily set the locale to the content locale
i18n::set_locale($contentLocale);
$replacements = parent::getReplacements();
// merge in our custom replacements
$replacements = array_merge($replacements, array(
'/&/u' => _t('TranslatedURLSegmentFilter.UrlAnd', '-and-'),
'/&/u' => _t('TranslatedURLSegmentFilter.UrlAnd', '-and-')
));
// reset to CMS locale
i18n::set_locale($currentLocale);
return $replacements;
}
}
然后您必须通过配置启用自定义 URLSegmentFilter
,方法是在您的 mysite/_config/config.yml
文件中添加如下内容:
Injector:
URLSegmentFilter:
class: TranslatedURLSegmentFilter
Update:以上示例假设您正在使用 Translatable 模块。如果您使用的是 Fluent,请替换以下行:
$contentLocale = Translatable::get_current_locale();
与:
$contentLocale = Fluent::current_locale();
您可以在 mysite/_config.php
中动态更新配置
$defaultReplacements = Config::inst()->get('URLSegmentFilter', 'default_replacements');
$translatedAnd = _t('URLSegmentFilter.And','-and-');
$defaultReplacements['/&/u'] = $translatedAnd;
$defaultReplacements['/&/u'] = $translatedAnd;
Config::inst()->Update('URLSegmentFilter', 'default_replacements', $defaultReplacements);
URLSegmentFilter
有一个静态数组 $default_replacements
,其中包含用于将 & 符号转换为(从 & 到 -and-)的字符串URL的。
我正在尝试扩展 class 并将此静态覆盖为 translate 符号转换(只有值是英语 and).
如何覆盖此目标的所有者静态?
class URLSegmentFilterExtension extends Extension {
private static $default_replacements = array(
'/&/u' => '-and-', // I need to translate this using _t()
'/&/u' => '-and-', // And this one
'/\s|\+/u' => '-',
'/[_.]+/u' => '-',
'/[^A-Za-z0-9\-]+/u' => '',
'/[\/\?=#]+/u' => '-',
'/[\-]{2,}/u' => '-',
'/^[\-]+/u' => '',
'/[\-]+$/u' => ''
);
}
首先:URLSegmentFilter
主要在CMS 上下文中运行,您通常只有一个语言环境(取决于编辑会员的设置)。所以单独使用 _t()
可能不是很有帮助?因此,您可能必须获取当前的编辑语言环境(假设您使用的是 Fluent 或 Translatable)并临时设置翻译语言环境。
我没有看到通过那里的扩展挂接翻译的方法。我认为你最好创建一个自定义子类并通过 Injector 使用它。
像这样的东西应该可以工作:
<?php
class TranslatedURLSegmentFilter extends URLSegmentFilter
{
public function getReplacements()
{
$currentLocale = i18n::get_locale();
$contentLocale = Translatable::get_current_locale();
// temporarily set the locale to the content locale
i18n::set_locale($contentLocale);
$replacements = parent::getReplacements();
// merge in our custom replacements
$replacements = array_merge($replacements, array(
'/&/u' => _t('TranslatedURLSegmentFilter.UrlAnd', '-and-'),
'/&/u' => _t('TranslatedURLSegmentFilter.UrlAnd', '-and-')
));
// reset to CMS locale
i18n::set_locale($currentLocale);
return $replacements;
}
}
然后您必须通过配置启用自定义 URLSegmentFilter
,方法是在您的 mysite/_config/config.yml
文件中添加如下内容:
Injector:
URLSegmentFilter:
class: TranslatedURLSegmentFilter
Update:以上示例假设您正在使用 Translatable 模块。如果您使用的是 Fluent,请替换以下行:
$contentLocale = Translatable::get_current_locale();
与:
$contentLocale = Fluent::current_locale();
您可以在 mysite/_config.php
$defaultReplacements = Config::inst()->get('URLSegmentFilter', 'default_replacements');
$translatedAnd = _t('URLSegmentFilter.And','-and-');
$defaultReplacements['/&/u'] = $translatedAnd;
$defaultReplacements['/&/u'] = $translatedAnd;
Config::inst()->Update('URLSegmentFilter', 'default_replacements', $defaultReplacements);