almacil/php-translate

1.1.0 2023-10-13 08:23 UTC

This package is not auto-updated.

Last update: 2024-09-27 12:51:32 UTC


README

GitHub last commit GitHub tag (latest by date) Packagist PHP Version Support GitHub

🈳 Almacil PHP Translate

这是一个用PHP编写的库,用于国际化使用PHP制作的网站和应用程序。


你想贡献吗?
Donate 1€

功能

  • 从json文件进行翻译
  • 使用 Google Translate 在线翻译单词或短语并将其存储到json文件中

安装

可以使用Composer进行安装

composer require almacil/php-translate

用法

创建一个 \Almacil\Translate 实例

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

// Language to translate from auto detectable language
$lang = 'en';

// Directory containing the json files with the translations
$directory = __DIR__ . '/i18n';

// We want the library to search for the translations that it cannot find in the files and to include the translations in the files
$findMissingTranslations = true;

// Create de instance
$translate = new \Almacil\Translate($lang, $directory, $findMissingTranslations);
echo $translate->get('Hola mundo!'); // Hello world!

创建一个短名称的函数以方便使用

// ... after the setup

function t($text, $params = null) {
    global $translate;
    return $translate->get($text, $params);
}

echo '<p>' . t('Hola mundo!') . '</p>'; // <p>Hello World!</p>

参数

我们可以这样在翻译中包含参数

// ./i18n/en.json
{
    "hola-name": "Hello {{name}}!"
}
// Some PHP file
echo '<p>' . t('hola-name', array('name' => 'Rubén')) . '</p>'; // <p>Hello Rubén!</p>

你想贡献吗?
Donate 1€

由开发者用❤️为开发者制作