Retrieves an array of strings in various languages from different sources.
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.
Install Transcribe
through Composer:
$ composer require rougin/transcribe
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);
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);
$table
name
- name of the database tablelanguage
- language name based from a locale (e.g en_GB
)text
- a keyword or a text to be translatedtranslation
- translation from the based languageuse 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);
// Returns all stored texts
$texts = $transcribe->all();
// Returns translation of 'name' in 'fil_PH' group (e.g "pangalan")
$text = $transcribe->get('fil_PH.name');
Just implement it to a SourceInterface.
namespace Rougin\Transcribe\Source;
interface SourceInterface
{
/**
* Returns an array of words.
*
* @return array
*/
public function words();
}