How can I manipulate translation methods of cakephp 3 to become case insensitive?

a 夏天 提交于 2019-12-12 05:39:42

问题


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"

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"

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;
}

Then my default.po file will become too shorter :) like this

msgid "acg_id"
msgstr "access control group identifier"

回答1:


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';

// [...]


来源:https://stackoverflow.com/questions/29316489/how-can-i-manipulate-translation-methods-of-cakephp-3-to-become-case-insensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!