avalon/language

Avalon Language component.

dev-master / 2.0.x-dev 2018-04-07 15:24 UTC

This package is not auto-updated.

Last update: 2024-11-04 06:48:14 UTC


README

This package makes translating easy.

Installation

This package can be installed via composer:

composer require avalon/language

Usage

use Avalon\Language;

// Create a new translation
$myLanguage = new Language(function ($t) {
    $t->name    = 'My Language';
    $t->locale  = 'en_AU';

    // The index is what we use to fetch the string value
    $t->strings = [
        'my_string_index' => 'My String Value',
        'test_x' => 'Test {1}',
        'hello_x' => 'Hello {username}',
        'x_tickets' => "{plural:{0}, {{0} ticket|{0} tickets}}",
    ];
});

// Set our language as the current language to use by passing the `locale` value
Language::setCurrent('en_AU');

// Translate some stuff
Language::translate('my_string_index'); // => 'My String Value'
Language::translate('test_x', ['Hello']); // => 'Test Hello'
Language::translate('hello_x', ['username' => 'Admin']); // => 'Hello Admin'
Language::translate('x_tickets', [1]); // => '1 ticket'
Language::translate('x_tickets', [2]); // => 2 tickets'