我怎样才能操纵 cakephp 3 的翻译方法变得不区分大小写?
How can I manipulate translation methods of cakephp 3 to become case insensitive?
这是我的 src/Locale/en_US/default.po 文件的一部分
msgid "Acg_id"
msgstr "access control group identifier"
msgid "acg_id"
msgstr "access control group identifier"
msgid "Acg_Id"
msgstr "access control group identifier"
我的 default.po 文件太长了。而且我觉得这样写是不可能的。
msgid "Acg_id"
msgid "acg_id"
msgid "Acg_Id"
msgstr "access control group identifier"
如何在cakephp core中实现下面的功能
function __($token){
$translation = translation(strtolower($token));//translation returns translation of token if exists else returns null
return $translation ? $translation : $token;
}
然后我的 default.po 文件会变得太短 :) 像这样
msgid "acg_id"
msgstr "access control group identifier"
虽然我认为不区分大小写的翻译有点不好(为什么不简单地使用正确的消息 ID?),您可以轻松地在您的应用程序中预先声明任何 shorthand translation functions config/bootstrap.php
, 之前 包括自动加载器。
// [...]
use Cake\I18n\I18n;
// https://github.com/cakephp/cakephp/blob/3.0.0/src/I18n/functions.php#L26
function __($token, $args = null)
{
if (!$token) {
return null;
}
$arguments = func_num_args() === 2 ? (array)$args : array_slice(func_get_args(), 1);
// side note: you may want to use mb_strtolower instead
return I18n::translator()->translate(strtolower($token), $arguments);
}
// Use composer to load the autoloader.
require ROOT . DS . 'vendor' . DS . 'autoload.php';
// [...]
这是我的 src/Locale/en_US/default.po 文件的一部分
msgid "Acg_id"
msgstr "access control group identifier"
msgid "acg_id"
msgstr "access control group identifier"
msgid "Acg_Id"
msgstr "access control group identifier"
我的 default.po 文件太长了。而且我觉得这样写是不可能的。
msgid "Acg_id"
msgid "acg_id"
msgid "Acg_Id"
msgstr "access control group identifier"
如何在cakephp core中实现下面的功能
function __($token){
$translation = translation(strtolower($token));//translation returns translation of token if exists else returns null
return $translation ? $translation : $token;
}
然后我的 default.po 文件会变得太短 :) 像这样
msgid "acg_id"
msgstr "access control group identifier"
虽然我认为不区分大小写的翻译有点不好(为什么不简单地使用正确的消息 ID?),您可以轻松地在您的应用程序中预先声明任何 shorthand translation functions config/bootstrap.php
, 之前 包括自动加载器。
// [...]
use Cake\I18n\I18n;
// https://github.com/cakephp/cakephp/blob/3.0.0/src/I18n/functions.php#L26
function __($token, $args = null)
{
if (!$token) {
return null;
}
$arguments = func_num_args() === 2 ? (array)$args : array_slice(func_get_args(), 1);
// side note: you may want to use mb_strtolower instead
return I18n::translator()->translate(strtolower($token), $arguments);
}
// Use composer to load the autoloader.
require ROOT . DS . 'vendor' . DS . 'autoload.php';
// [...]