【问题标题】:i18n in Cakephp 3 + WordpressCakephp 3 + Wordpress 中的 i18n
【发布时间】:2015-05-05 17:41:54
【问题描述】:

我正在尝试以最漂亮的方式解决这个问题,我已经在 wordpress 中集成了 cakephp 或在 cakephp 中集成了 wordpress,这取决于你如何看待它。

wordpress 安装存在于 Cakephp /webroot 目录中,首先通过一些 .htaccess 诡计加载,然后将 Cakephp 映射到特定的子域或文件夹。

我在这里遇到的问题是 Cakephp 和 Wordpress i18n/translate 函数是相同的,function __()

我通过简单地将 CakePHP 的 __() 函数声明为 ___() 暂时解决了这个问题。

供应商/cakephp/cakephp/src/i18n/functions.php

if (!function_exists('__')) {
    /**
     * Returns a translated string if one is found; Otherwise, the submitted message.
     *
     * @param string $singular Text to translate.
     * @param mixed $args Array with arguments or multiple arguments in function.
     * @return mixed Translated string.
     * @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__
     */
    function __($singular, $args = null)
    {
        if (!$singular) {
            return;
        }

        $arguments = func_num_args() === 2 ? (array)$args : array_slice(func_get_args(), 1);
        return I18n::translator()->translate($singular, $arguments);
    }

}
else
{
    function ___($singular, $args = null)
    {
        if (!$singular) {
            return;
        }

        $arguments = func_num_args() === 2 ? (array)$args : array_slice(func_get_args(), 1);
        return I18n::translator()->translate($singular, $arguments);
    }
}

wp-includes/i10n.php

/**
 * Retrieve the translation of $text. If there is no translation,
 * or the text domain isn't loaded, the original text is returned.
 *
 * @since 2.1.0
 *
 * @param string $text   Text to translate.
 * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 * @return string Translated text.
 */
function __( $text, $domain = 'default' ) {
    return translate( $text, $domain );
}

所以我的问题是,这是解决问题的最佳方法还是我应该尝试将所有 Cakephp 翻译移动到 Wordpress?不太喜欢更改 Cakephp 核心,因为它会在更新时崩溃。有任何想法吗?

【问题讨论】:

  • 我面临着一种情况,我必须将 WordPress 帖子显示到 cakephp 3 站点中。我已将 WordPress 数据库与 Cakephp 数据库结合起来,并将 wp 文件夹放入 webroot。你能帮我看看剩下的步骤是什么吗?

标签: wordpress internationalization cakephp-3.0


【解决方案1】:

为什么不在 cakephp 内核加载前声明函数呢?也就是说,在 wordpress 中加载声明翻译函数的文件,然后声明您对 cakephp 函数的覆盖。

include wordpress/file_having_translation_functions;
function ___(...) {
    return I18n::...;
}

【讨论】:

  • Wordpress 函数首先加载,Cakephp transl.function 被覆盖,我可以做相反的事情,但这会导致 wp 翻译功能崩溃,这很糟糕。
  • 那就完美了。您可以在 cakephp 应用程序引导程序中自己声明新的翻译函数
  • 将代码放在引导程序而不是核心文件中会更好:) 谢谢。
猜你喜欢
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 2018-06-16
  • 2019-02-18
  • 2012-05-06
  • 1970-01-01
相关资源
最近更新 更多