vulcanphp/translator
最新稳定版本:v1.0
Composer 安装命令:
composer require vulcanphp/translator
包简介
Simple Translator is for PHP Application to translate texts with many local languages
README 文档
README
Simple Translator is for PHP Application to translate texts with many local languages
Installation
It's recommended that you use Composer to install this PHP Simple Translator
$ composer require vulcanphp/translator
Get Started
After Installing this PHP Simple Translator, initialize it in your application.
<?php // index.php use VulcanPhp\Translator\Drivers\GoogleTranslatorDriver; use VulcanPhp\Translator\Manager\TranslatorFileManager; use VulcanPhp\Translator\Translator; require_once __DIR__ . '/vendor/autoload.php'; // initialize translator advanced Translator::init( // user google translator driver to translate text new GoogleTranslatorDriver( // use filesystem manager to store translated text // local filesystem configuration new TranslatorFileManager([ // source language from 'source' => 'en', // target/convert language to 'convert' => 'bn', // suffix for translated file to make it slim, EX: 'en-[admin].json' // [admin] is suffix for this certain language file 'suffix' => null, // local direcotry to store translated files 'local_dir' => __DIR__ . '/translate', ]) ) ); // or, simply use helper function init_translator([ // local filesystem configuration for manager 'convert' => 'bn', // ... ]); // IMPORTANT:: there is a cool feature for google translator // this is lazyLoad, if you use about hundred+ times to translate texts // from google, then it will make the process slower. to make it faster, // enable lazy translator just after initialize the translate instance in index.php // this is optional but HIGHLY RECOMMENDED enable_lazy_translator(); // Now, simply call translate() function allover in your application var_dump(translate('Hello World!')); // ...
How it's Work?
This Translator can use different external sources to translate texts and store it somewhere in application. as a source this translator has a built in driver (GoogleTranslatorDriver) to translate text. and to store it in local this translator also has a manager (TranslatorFileManager) to do it... for advanced usage you can simply add new sources like driver, manager just follow the interfaces of this class
Direct Usage of GoogleTranslatorEngine
<?php // index.php use VulcanPhp\Translator\Engine\Google\GoogleTranslator; require_once __DIR__ . '/vendor/autoload.php'; // create a instance of google translator engine $engine = GoogleTranslator::create(); // translate a text from google // translate with all languages that supported by google translator var_dump($engine->translateFromString('Today is a sunny day', 'en', 'bn')); var_dump($engine->translateFromString('Today is a sunny day', 'en', 'fr')); var_dump($engine->translateFromString('Today is a sunny day', 'en', 'es')); // translate multiple text at a time from google var_dump( $engine->translateFromArray( [ 'Today is a sunny day', 'What should i wear today?', ], 'en', 'bn' ) ); // ...
Create Multiple Translator Instance for Multiple Languages
<?php // index.php use VulcanPhp\Translator\Drivers\GoogleTranslatorDriver; use VulcanPhp\Translator\Manager\TranslatorFileManager; use VulcanPhp\Translator\Translator; require_once __DIR__ . '/vendor/autoload.php'; // create a translator for bengali language $Translator_BN = Translator::create( new GoogleTranslatorDriver( new TranslatorFileManager([ 'source' => 'en', 'convert' => 'bn', 'local_dir' => __DIR__ . '/translate', ]) ) ) ->getDriver(); var_dump($Translator_BN->translate('Hello World')); // create another translator for spanish language $Translator_ES = Translator::create( new GoogleTranslatorDriver( new TranslatorFileManager([ 'source' => 'en', 'convert' => 'es', 'local_dir' => __DIR__ . '/translate', ]) ) ) ->getDriver(); var_dump($Translator_ES->translate('Hello World')); // ...
统计信息
- 总下载量: 42
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-25