Transcribe is a simple localization package for PHP. A localization source can be file-based (similar to Laravel's Localization) or from a database connection.
Install Transcribe
through Composer:
$ composer require rougin/transcribe
The Transcribe
package needs to have a localization file which contains the list of texts with its translations (e.g., fil_PH.php
):
// locales/fil_PH.php
$texts = array();
$texts['language'] = 'linguahe';
$texts['name'] = 'pangalan';
$texts['school'] = 'paaralan';
return $texts;
Specify the path of the localization files in the Transcribe
class:
// index.php
use Rougin\Transcribe\Source\DirectorySource;
use Rougin\Transcribe\Transcribe;
// Specify the localization source ---
$path = (string) __DIR__ . '/locales';
$source = new DirectorySource($path);
// -----------------------------------
$transcribe = new Transcribe($source);
Alternatively, the localization source can be from a database. It should have the following fields in a specified 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 language| language | text | translation |
| -------- | ------ | ----------- |
| fil_PH | name | pangalan |
| fil_PH | school | paaralan |
// index.php
use Rougin\Transcribe\Source\DirectorySource;
use Rougin\Transcribe\Transcribe;
// Create a PDO instance -----------------
$dsn = 'mysql:host=localhost;dbname=demo';
$pdo = new PDO($dsn, 'root', '');
// ---------------------------------------
// Specify the fields from the table ---
$table = array('name' => 'words');
$table['language'] = 'language';
$table['translation'] = 'translation';
$table['text'] = 'text';
// -------------------------------------
$source = new DatabaseSource($pdo, $table);
$transcribe = new Transcribe($source);
If having multiple localization sources, the SourceCollection
class can be used to store them into a single class:
// index.php
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
// "$directory" is a DirectorySource
$collection->add($database)->add($directory);
$transcribe = new Transcribe($collection);
Use the get
method to get a specified translation. While use the all
method to get all the available texts:
// 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 custom sources is possible by implementing them to SourceInterface
:
namespace Rougin\Transcribe\Source;
interface SourceInterface
{
/**
* Returns an array of words.
*
* @return array<string, array<string, string>>
*/
public function words();
}
Please see CHANGELOG for more information what has changed recently.
$ composer test
The MIT License (MIT). Please see LICENSE for more information.