定制 gettext/translator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gettext/translator

最新稳定版本:v1.2.1

Composer 安装命令:

composer require gettext/translator

包简介

Gettext translator functions

README 文档

README

Latest Version on Packagist Software License Total Downloads

Created by Oscar Otero http://oscarotero.com oom@oscarotero.com (MIT License)

Translator functions to use with gettext/gettext. Useful if you don't have the native gettext extension for php or want to avoid problems with it.

Installation

composer require gettext/translator

Translator

use Gettext\Translator;

//Create a new instance of the translator
$t = new Translator();

//Load the translations from php files (generated by Gettext\Extractors\PhpArray)
$t->loadTranslations(
    'locales/gl/domain1.php',
    'locales/gl/domain2.php',
    'locales/gl/domain3.php',
);

//Now you can use it in your templates
echo $t->gettext('apple');

GettextTranslator

The class Gettext\GettextTranslator uses the gettext extension. It's useful because combines the performance of using real gettext functions but with the same API than Translator class, so you can switch to one or other translator without change code of your app.

use Gettext\GettextTranslator;

//Create a new instance
$t = new GettextTranslator();

//It detects the environment variables to set the locale, but you can change it:
$t->setLanguage('gl');

//Load the domains:
$t->loadDomain('messages', 'project/Locale');
//this means you have the file "project/Locale/gl/LC_MESSAGES/messages.mo"

//Now you can use it in your templates
echo $t->gettext('apple');

Translator functions

To ease the use of translations in your php templates, you can use the provided functions:

use Gettext\TranslatorFunctions;

//Register the translator to use the global functions
TranslatorFunctions::register($t);

echo __('apple'); // it's the same than $t->gettext('apple');

You can scan the php files containing these functions and extract the values with the PhpCode extractor:

<!-- index.php -->
<html>
    <body>
        <?= __('Hello world'); ?>
    </body>
</html>

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.

统计信息

  • 总下载量: 4.09M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 1
  • 依赖项目数: 10
  • 推荐数: 1

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固