codelight/babelfish

此包最新版本(v1.0)没有可用的许可证信息。

v1.0 2020-01-22 19:56 UTC

This package is auto-updated.

Last update: 2024-09-12 00:10:54 UTC


README

Babelfish 是一个轻量级的字符串注册包。它允许你在代码库中使用字符串别名而不是实际字符串。这使得查找和编辑字符串本身变得容易得多。

除此之外,如果你使用 Blade 模板,你不必担心 gettext / poedit / wpml 无法正确解析你的模板。

安装

composer require codelight/babelfish ^1.0

使用方法

在你的 functions.php

<?php

load_theme_textdomain('your_text_domain', get_stylesheet_directory());

// Register aliases and corresponding strings:
babelfish()->register([
    'button.payment' => __('Complete payment',     'your_text_domain'),
    'button.cta'     => __('Buy now for only %s!', 'your_text_domain'),
]);

但是,我建议创建一个单独的文件,例如 translations.php,其中包含所有与翻译相关的信息。

获取翻译

<button>
  <?php echo _t('button.payment'); // Complete payment! ?>
</button>
<button>
  <?php echo _t('button.cta', '$420.69'); // Buy now for only $420.69!  ?>
</button>

如上例所示,翻译函数也支持 sprintf 风格的变量替换。你可以在 _t() 函数中使用任意数量的变量。

Babelfish