2017 © Pedro Peláez
 

library php-i18n-l10n

PHP library/api to provide Internationalisation and Localisation

image

wdes/php-i18n-l10n

PHP library/api to provide Internationalisation and Localisation

  • Tuesday, June 19, 2018
  • by wdes
  • Repository
  • 1 Watchers
  • 1 Stars
  • 48 Installations
  • Shell
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

php-I18n-L10n

PHP library/api to provide Internationalisation and Localisation, (*1)

Codacy Badge Lint and analyse files Run tests codecov FOSSA Status Packagist Latest Stable Version, (*2)

License

FOSSA Status, (*3)

About

We use the phpmyadmin/twig-i18n-extension for the Twig extension., (*4)

How to use

composer require wdes/php-i18n-l10n

Have a look at example file example/simple.php, (*5)

Example without a MO file

<?php

declare(strict_types = 1);

// Can be removed :)

require_once __DIR__ . '/../vendor/autoload.php';

use Wdes\phpI18nL10n\plugins\MoReader;
use Wdes\phpI18nL10n\Launcher;
use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\ArrayLoader as TwigLoader;

$moReader = new MoReader();
$moReader->setTranslations(
    [
        'Homepage' => 'Page d\'accueil',
    ]
);
// Load the translation plugin
Launcher::setPlugin($moReader);

$twig = new TwigEnvironment(new TwigLoader());
$twig->addExtension(new ExtensionI18n());
// You can use a file instead, see the example using a mo file
$templateContents = <<<HTML
<html>
    <title>{% trans %}Homepage{% endtrans %}</title>
    <body>
        {% trans %}Homepage{% endtrans %}
    </body>
</html>
HTML;
echo $twig->createTemplate($templateContents)->render([]);

Example with a MO file

<?php

declare(strict_types = 1);

// Can be removed :)

require_once __DIR__ . '/../vendor/autoload.php';

use Wdes\phpI18nL10n\plugins\MoReader;
use Wdes\phpI18nL10n\Launcher;
use Wdes\phpI18nL10n\Twig\Extension\I18n as ExtensionI18n;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;

$dataDir  = __DIR__ . '/locale/';
$moReader = new MoReader();
$moReader->readFile($dataDir . 'fr.mo'); // Load the file you want (a specific language for example)
// Load the translation plugin
Launcher::setPlugin($moReader);

$loader = new TwigLoaderFilesystem([ __DIR__ . '/templates/' ]); // Load all templates from the dir
$twig   = new TwigEnvironment($loader);

$twig->addExtension(new ExtensionI18n());
echo $twig->render(
    'homepage.twig', // Can be found in the templates directory
    [
        'keyForTwig' => 'theValue', // Just an example line ;)
        'say' => 'Hello world'
    ]
);

Scripts

This package includes some scripts that can be usefull scripts/tools, (*6)

Here is an example to use them : scripts/update-example.sh, (*7)

The Versions

19/06 2018

dev-dev

dev-dev https://github.com/wdes/php-I18n-l10n

PHP library/api to provide Internationalisation and Localisation

  Sources   Download

MPL-2.0

The Requires

  • php ^7.1

 

The Development Requires

by William Desportes

15/06 2018

dev-master

9999999-dev https://github.com/wdes/php-I18n-l10n

PHP library/api to provide Internationalisation and Localisation

  Sources   Download

MPL-2.0

The Requires

  • php ^7.1

 

The Development Requires

by William Desportes