polyglot/template-provider-memory

PHP Polyglot memory template provider

v1.0.0 2023-09-08 19:34 UTC

This package is not auto-updated.

Last update: 2024-12-14 23:47:34 UTC


README

A simple polyglot simple PHP File template provider.

Install

composer require polyglot/template-provider-php-file:^1.0

Using

Create translation files:

<?php
// /path/to/translations/messages/en_US.php

return [
    'no_deep' => '"no_deep" template in "messages" domain (en_US)',
    'foo' => [
        'bar' => '"foo.bar" template in "messages" domain (en_US)',
        'baz' => '"foo.baz" template in "messages" domain (en_US)',
    ],
    'h' => [
        'e' => [
            'l' => [
                'l' => [
                    'o' => '"h.e.l.l.o" template in "messages" domain (en_US)',
                ],
            ],
        ],
    ],
];
<?php
// /path/to/translations/messages/ru_RU.php

return [
    'no_deep' => '"no_deep" шаблон в домене "messages" (ru_RU)',
    'foo' => [
        'bar' => '"foo.bar" шаблон в домене "messages" (ru_RU)',
        'baz' => '"foo.baz" шаблон в домене "messages" (ru_RU)',
    ],
    'h' => [
        'e' => [
            'l' => [
                'l' => [
                    'o' => '"h.e.l.l.o" шаблон в домене "messages" (ru_RU)',
                ],
            ],
        ],
    ],
];
<?php

$directory = '/path/to/translations';
$provider = new \Polyglot\PhpFileTemplateProvider\PhpFileTemplateProvider($directory);

$provider->get('messages', 'no_deep', 'en_US'); // returns '"no_deep" template in "messages" domain (en_US)'
$provider->get('messages', 'foo.bar', 'en_US'); // returns '"foo.bar" template in "messages" domain (en_US)'
$provider->get('messages', 'foo.baz', 'en_US'); // returns '"foo.baz" template in "messages" domain (en_US)'
$provider->get('messages', 'h.e.l.l.o', 'en_US'); // returns '"h.e.l.l.o" template in "messages" domain (en_US)'
$provider->get('messages', 'no_deep', 'ru_RU'); // returns '"no_deep" шаблон в домене "messages" (ru_RU)'
$provider->get('messages', 'foo.bar', 'ru_RU'); // returns '"foo.bar" шаблон в домене "messages" (ru_RU)'
$provider->get('messages', 'foo.baz', 'ru_RU'); // returns '"foo.baz" шаблон в домене "messages" (ru_RU)'
$provider->get('messages', 'h.e.l.l.o', 'ru_RU'); // returns '"h.e.l.l.o" шаблон в домене "messages" (ru_RU)'

// throws Polyglot\Contract\TemplateProvider\Exception\TemplateNotFound exception
$provider->get('messages', 'h.e.l.l.o', 'fr_FR');