PHPackages                             bayfrontmedia/translation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. bayfrontmedia/translation

ActiveLibrary[Localization &amp; i18n](/categories/localization)

bayfrontmedia/translation
=========================

A PHP translation library utilizing multiple language storage options.

v2.0.1(1y ago)01.2k12MITPHPPHP ^8.0

Since Sep 12Pushed 1y ago1 watchersCompare

[ Source](https://github.com/bayfrontmedia/translation)[ Packagist](https://packagist.org/packages/bayfrontmedia/translation)[ Docs](https://github.com/bayfrontmedia/translation)[ RSS](/packages/bayfrontmedia-translation/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (8)Dependencies (1)Versions (9)Used By (2)

Translation
-----------

[](#translation)

A PHP translation library utilizing multiple language storage options.

- [License](#license)
- [Author](#author)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)

License
-------

[](#license)

This project is open source and available under the [MIT License](LICENSE).

Author
------

[](#author)

[![Bayfront Media](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)

- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github&utm_medium=direct)
- [Bayfront Media GitHub](https://github.com/bayfrontmedia)

Requirements
------------

[](#requirements)

- PHP `^8.0` (Tested up to `8.4`)
- JSON PHP extension
- PDO PHP extension

Installation
------------

[](#installation)

```
composer require bayfrontmedia/translation

```

Usage
-----

[](#usage)

**NOTE:** It is up to you to populate the translation data, no matter where it is stored. This library simply reads the existing data from its source.

### Storage adapter

[](#storage-adapter)

A `Bayfront\Translation\ApadpterInterface` must be passed to the `Bayfront\Translation\Translate` constructor. There are a variety of storage adapters available, each with their own required configuration.

**Defined array**

The defined array adapter allows you to use a predefined array containing all of your translations.

```
use Bayfront\Translation\Adapters\DefinedArray;

$adapter = new DefinedArray([
    'en' => [ // Locale
        'dashboard' => [ // ID
            'title' => 'Account dashboard',
            'greeting' => 'Welcome back, {{name}}'
        ]
    ]
]);

```

**Local**

The local adapter allows you to use local native PHP files containing all of your translation arrays.

```
use Bayfront\Translation\Adapters\Local;

$adapter = new Local('/root_path');

```

The file structure from the root path should be:

```
/root_path
    /locale
        /id.php

```

For example, if the locale is set as `en`, the method `say('dashboard.greeting')` will search for the file `/root_path/en/dashboard.php`, and the array key `greeting`.

Example `dashboard.php`:

```
