phalcon / incubator-translate
Phalcon Translate 组件的额外翻译适配器
v0.2.0
2021-02-28 21:59 UTC
Requires
- php: >=7.2
- ext-phalcon: ^4.0
Requires (Dev)
- ext-intl: *
- ext-pdo: *
- codeception/codeception: ^4.1
- codeception/module-asserts: ^1.0.0
- codeception/module-cli: ^1.0
- codeception/module-db: ^1.0
- codeception/module-phpbrowser: ^1.0.0
- phalcon/dd: ^1.1
- phalcon/ide-stubs: ^4.0.0
- squizlabs/php_codesniffer: ^3.5
- vimeo/psalm: ^4.1
- vlucas/phpdotenv: ^4.1
Suggests
- ext-intl: Required for Interpolator Adapter support
- ext-pdo: Required for Database Adapter
- mongodb/mongodb: Required for MongoDB Adapter support
This package is auto-updated.
Last update: 2024-09-13 13:42:17 UTC
README
问题跟踪器
https://github.com/phalcon/incubator/issues
数据库
您也可以使用数据库来存储翻译。
首先,您需要升级您的数据库。为此,使用 [DI][1](在 /public/index.php
中)。看看这个
use Phalcon\Db\Adapter\Pdo\Mysql; $di->set( 'db', function () { return new Mysql( [ 'host' => 'localhost', 'username' => 'root', 'password' => 123456, 'dbname' => 'application', ] ); } );
然后,您应该通过您的 controller
获取翻译。将其放在它上面
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 ] ); } // ... }
为了存储翻译,以下表格是推荐的
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`) )
可选创建唯一索引
CREATE UNIQUE INDEX translations_language_key_name_unique_index ON translations (language, key_name);
列是自描述的,但请注意 language
—— 它是一个存储用户所使用语言的列,可以是 en
、en-us
或 en-US
。现在您有责任决定您想使用哪种模式。
为了向用户显示翻译的单词,您需要设置一个变量来存储数据库中的 expressions/translations
。 这一步发生在您的控制器中。 按照以下示例进行操作
class IndexController extends \Phalcon\Mvc\Controller { protected function _getTranslation() { // ... } public function indexAction() { $this->view->setVar( 'expression', $this->_getTranslation() ); } }
然后,只需在您的视图中输出 phrase/sentence/word
<html> <head> <!-- ... --> </head> <body> <h1><?php echo $expression->_("IndexPage_Hello_World"); ?></h1> </body> </html>
或者,如果您愿意,可以使用 [Volt][2]
<h1>{{ expression._("IndexPage_Hello_World") }}</h1>
Mongo
实现了一个用于翻译的 Mongo 适配器。
必须创建一个通用的集合来存储翻译,并将其作为参数传递。
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
这个适配器通过允许一个 CSV 文件用于多种语言来扩展 Phalcon\Translate\Adapter\Csv。
- CSV 示例(添加了空格以提高可读性)
#ignored; en_US; fr_FR; es_ES
label_street; street; rue; calle
label_car; car; voiture; coche
label_home; home; maison; casa
- PHP 示例
// 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.ac.cn/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