Transcribe
is a simple localization package written in PHP in which the translated word can be retrieved easily based on the specified locale. A localization source can be from multiple .php
files or from a database connection using PDO.
Install the Transcribe
package via Composer:
$ composer require rougin/transcribe
Prior in using Transcribe
, a list of words must be provided with its specified translations (e.g., fil_PH.php
):
// locales/fil_PH.php
$texts = array();
$texts['language'] = 'linguahe';
$texts['name'] = 'pangalan';
$texts['school'] = 'paaralan';
return $texts;
Once provided, specify the words in a source (e.g., FileSource
):
// index.php
use Rougin\Transcribe\Source\FileSource;
// ...
$source = new FileSource;
// Add the directory to the source ----
$source->addPath(__DIR__ . '/locales');
// ------------------------------------
After creating the specified source, use the get
method from the Locale
class to get the localized word based on its keyword:
// index.php
use Rougin\Transcribe\Locale;
// ...
/** @var \Rougin\Transcribe\Source\FileSource */
$source = /** ... */;
$locale = new Locale($source);
echo $locale->get('fil_PH.name');
$ php index.php
pangalan
Using the setDefault
method can define the default locale. With this, there is no need to specify it when using the get
method:
// index.php
$locale->setDefault('fil_PH');
// No need to specify "fil_PH" ---
echo $locale->get('name');
// -------------------------------
The previous example uses the FileSource
which uses .php
files in getting localized words. But Transcribe
also provides a way in getting the said localized words through a database using the PdoSource
:
// index.php
use Rougin\Transcribe\Source\PdoSource;
// ...
// Create a PDO instance -----------------
$dsn = 'mysql:host=localhost;dbname=demo';
$pdo = new PDO($dsn, 'root', /** ... */);
// ---------------------------------------
$source = new PdoSource($pdo);
// ...
When using the PdoSource
class, it can also specify the database table and its columns to be used for getting the localized words:
# Contents of the "locales" table
| `id` | `type` | `name` | `text` |
|------|--------|--------|----------|
| 1 | fil_PH | name | pangalan |
| 2 | fil_PH | school | paaralan |
// ...
// Use "locales" table from database ---
$source->setTableName('locales');
// -------------------------------------
// Use "type" column from "locales" table ---
$source->setTypeColumn('type');
// ------------------------------------------
// Use "name" column from "locales" table ---
$source->setNameColumn('name');
// ------------------------------------------
// Use "text" column from "locales" table ---
$source->setTextColumn('text');
// ------------------------------------------
// ...
Note
If the required table and columns were not specified, its default values are the same from the above-example (e.g., locales
for table, and type
, name
, and text
values for the columns).
Then use the same get
method from Locale
class to get the localized word from the database table:
// index.php
// ...
echo $locale->get('fil_PH.name');
$ php index.php
pangalan
To create a custom source, kindly use the SourceInterface
for its implementation:
namespace Rougin\Transcribe\Source;
interface SourceInterface
{
/**
* Returns an array of words.
*
* @return array<string, array<string, string>>
*/
public function words();
}
The words
method should return a list of words in an associative array format:
return array(
'fil_PH' => array(
'language' => 'linguahe',
'name' => 'pangalan',
'school' => 'paaralan',
),
);
The specified method will be used as the reference for finding the localized word from the get
method of Locale
class.
The new release for v0.4.0
will be having a backward compatibility break (BC break). With this, some functionalities from the earlier versions might not be working after upgrading. This was done to increase extensibility, simplicity and maintainbility. This was discussed in one of my blog post which also mentions Transcribe
:
I also want to extend this plan to my personal packages as well like Staticka and Transcribe. With this, I will introduce backward compatibility breaks to them initially as it is hard to migrate their codebase due to minimal to no documentation being provided in its basic usage and its internals. As I checked their code, I realized that they are also over engineered, which is a mistake that I needed to atone for when updating my packages in the future.
Please see Pull Request #1 for the files that were removed or updated in this release and the UPGRADING page for the specified breaking changes.
Please see CHANGELOG for more information what has changed recently.
$ composer test
The MIT License (MIT). Please see LICENSE for more information.