phalcon/incubator-translate 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

phalcon/incubator-translate

Composer 安装命令:

composer require phalcon/incubator-translate

包简介

Extra Translate adapters for Phalcon Translate component

README 文档

README

Discord Packagist Version PHP from Packagist codecov Packagist

Issues tracker

https://github.com/phalcon/incubator/issues

Database

You can use your database to store the translations, too.

First of all, you need to up your database. To do this, use [DI][1] (in /public/index.php). Take a look:

use Phalcon\Db\Adapter\Pdo\Mysql;

$di->set(
    'db',
    function () {
        return new Mysql(
            [
                'host'     => 'localhost',
                'username' => 'root',
                'password' => 123456,
                'dbname'   => 'application',
            ]
        );
    }
);

Then, you should get the translation through your controller. Put this on it:

use Phalcon\Translate\Adapter\Database;

class IndexController extends \Phalcon\Mvc\Controller
{
    protected function _getTranslation()
    {
        return new Database(
            [
                'db'       => $this->di->get('db'), // Here we're getting the database from DI
                'table'    => 'translations', // The table that is storing the translations
                'language' => $this->request->getBestLanguage(), // Now we're getting the best language for the user
            ]
        );
    }

    // ...
}

To store the translations, the following table is recommended:

CREATE TABLE `translations` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `language` VARCHAR(5) NOT NULL COLLATE 'utf8_bin',
    `key_name` VARCHAR(48) NOT NULL COLLATE 'utf8_bin',
    `value` TEXT NOT NULL COLLATE 'utf8_bin',
    PRIMARY KEY (`id`)
)

Optional create unique index

CREATE UNIQUE INDEX translations_language_key_name_unique_index ON translations (language, key_name);

The columns are self-described, but pay attention to language — it's a column that stores the language that the user is using, that can be en, en-us or en-US. Now it's your responsibility to decide which pattern you want to use.

To display for your users the translated words you need to set up a variable to store the expressions/translations from your database. This step happens in your controller. Follow the example:

class IndexController extends \Phalcon\Mvc\Controller
{
    protected function _getTranslation()
    {
        // ...
    }

    public function indexAction()
    {
        $this->view->setVar(
            'expression',
            $this->_getTranslation()
        );
    }
}

Then, just output thephrase/sentence/word in your view:

<html>
    <head>
        <!-- ... -->
    </head>
    <body>
        <h1><?php echo $expression->_("IndexPage_Hello_World"); ?></h1>
    </body>
</html>

Or, if you wish you can use [Volt][2]:

<h1>{{ expression._("IndexPage_Hello_World") }}</h1>

Mongo

Implements a Mongo adapter for translations.

A generic collection with a source to store the translations must be created and passed as a parameter.

use MessageFormatter;
use Phalcon\Translate\Adapter\Mongo;
use My\Application\Collections\Translate;

$translate = new Mongo(
    [
        'collection' => Translate::class,
        'language'   => 'en',
    ]
);

echo $translate->t('application.title');

MultiCsv

This adapter extends Phalcon\Translate\Adapter\Csv by allowing one csv file for several languages.

  • CSV example (blank spaces added for readability):
#ignored;     en_US;  fr_FR;   es_ES
label_street; street; rue;     calle
label_car;    car;    voiture; coche
label_home;   home;   maison;  casa
  • PHP example :
// the constructor is inherited from Phalcon\Incubator\Translate\Adapter\CsvMulti
$titles_translater = new Phalcon\Translate\Adapter\MultiCsv(
    "{$config->langDir}/titles.csv",
    "es_ES",
    new InterpolatorFactory(),
    []
);

echo $titles_translater->query('label_home'); // string 'casa'


## Interpolator Intl

It needs the extension [intl](php.net/manual/book.intl.php) to be installed in PHP, and it uses [MessageFormatter](http://php.net/manual/en/class.messageformatter.php) objects in an interpolator interface.
More about the syntax convention can be read on this [formating guide](https://www.sitepoint.com/localization-demystified-understanding-php-intl/) and on the [ICU documentation](http://userguide.icu-project.org/formatparse/messages).

```php
<?php

use Phalcon\Translate\Adapter\NativeArray;
use Phalcon\Translate\Interpolator\Intl;

$translate = new NativeArray(
    [
        'interpolator' => new Intl('en_US'), // this interpolator must be locale aware
        'content' => [
            'hi-name' => 'Hello {name}, it\'s {time, number, integer} o\'clock',
        ],
    ]
);

$name = 'Henry';

// Hello Henry, it's 8 o'clock
$translate->_(
    'hi-name',
    [
        'name' => $name,
        'time' => 8,
    ]
);
<?php

use Phalcon\Translate\Adapter\NativeArray;
use Phalcon\Translate\Interpolator\Intl;

$translate = new NativeArray(
    [
        'interpolator' => new Intl('fr_FR'), // this interpolator must be locale aware
        'content' => [
            'apples' => "{count, plural, =0{Je n'ai aucune pomme} =1{J'ai une pomme} other{J'ai # pommes}}.",
        ],
    ]
);

// thousands separator is " " (blank space) for fr_FR
echo $translate->_(
    'apples',
    [
        'count' => 1000,
    ]
); // J'ai 1 000 pommes

phalcon/incubator-translate 适用场景与选型建议

phalcon/incubator-translate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.75k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「translations」 「translate」 「phalcon」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phalcon/incubator-translate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phalcon/incubator-translate 我们能提供哪些服务?
定制开发 / 二次开发

基于 phalcon/incubator-translate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-06