本文介绍了如何操纵cakephp 3的翻译方法不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的src / Locale / en_US / default.po文件的一部分
This is a part of my src/Locale/en_US/default.po file
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文件太长。我认为不可能这样写。
my default.po file is too long. And I think it is impossible to write like this.
msgid "Acg_id"
msgid "acg_id"
msgid "Acg_Id"
msgstr "access control group identifier"
如何实现功能在cakephp核心中的下面
How can I implement function below in cakephp core
function __($token){
$translation = translation(strtolower($token));//translation returns translation of token if exists else returns null
return $translation ? $translation : $token;
}
然后我的default.po文件将变得更短:)像这样
Then my default.po file will become too shorter :) like this
msgid "acg_id"
msgstr "access control group identifier"
推荐答案
虽然我认为不区分大小写的翻译是一种不好的做法(为什么不简单地使用正确的消息ID?),可以轻松地预先声明任何,之前,包括自动加载器。
While I think that case insensitive translations are kinda bad practice (why not simply use the proper message IDs?), you can easily pre-declare any of the shorthand translation functions in your apps config/bootstrap.php
, before including the autoloader.
// [...]
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';
// [...]
这篇关于如何操纵cakephp 3的翻译方法不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!