rougin / transcribe
An easy-to-use localization library for PHP.
v0.3.1
2016-09-11 09:32 UTC
Requires
- php: >=5.3.0
- tebru/multi-array: ~0.2
Requires (Dev)
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
README
Transcribe is an easy-to-use localization library for PHP. The localization source can be file-based (similar to Laravel's Localization) or from a database connection.
Installation
Install Transcribe
through Composer:
$ composer require rougin/transcribe
Basic Usage
Load a list of texts from a directory
locales/fil_PH.php
$texts = array(); $texts['language'] = 'linguahe'; $texts['name'] = 'pangalan'; $texts['school'] = 'paaralan'; return $texts;
use Rougin\Transcribe\Source\DirectorySource; use Rougin\Transcribe\Transcribe; $source = new DirectorySource(__DIR__ . '/locales'); $transcribe = new Transcribe($source);
Load a list of texts from a database
The contents of the words table:
language | text | translation |
---|---|---|
fil_PH | name | pangalan |
fil_PH | school | paaralan |
use Rougin\Transcribe\Source\DirectorySource; use Rougin\Transcribe\Transcribe; $pdo = new PDO('mysql:host=localhost;dbname=demo', 'root', ''); $table = array('name' => 'words'); $table['language'] = 'language'; $table['text'] = 'text'; $table['translation'] = 'translation'; $source = new DatabaseSource($pdo, $table); $transcribe = new Transcribe($source);
Must-have properties of $table
name
- name of the database tablelanguage
- language name based from a locale (e.gen_GB
)text
- a keyword or a text to be translatedtranslation
- translation from the based language
Load list of texts from different sources
use Rougin\Transcribe\Source\DatabaseSource; use Rougin\Transcribe\Source\DirectorySource; use Rougin\Transcribe\Source\SourceCollection; use Rougin\Transcribe\Transcribe; $collection = new SourceCollection; // "$database" is a DatabaseSource instance // while "$directory" is a DirectorySource. $collection->add($database)->add($directory); $transcribe = new Transcribe($collection);
Getting a text from the vocabulary
// Returns all stored texts $texts = $transcribe->all(); // Returns translation of 'name' in 'fil_PH' group (e.g "pangalan") $text = $transcribe->get('fil_PH.name');
Adding new source
Just implement it to a SourceInterface.
namespace Rougin\Transcribe\Source; interface SourceInterface { /** * Returns an array of words. * * @return array */ public function words(); }
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Credits
License
The MIT License (MIT). Please see LICENSE for more information.