Giter VIP home page Giter VIP logo

php-i18n-l10n's Introduction

php-I18n-L10n

PHP library/api to provide Internationalisation and Localisation

Codacy Badge Lint and analyse files Run phpunit tests codecov FOSSA Status HitCount Packagist Latest Stable Version

License

FOSSA Status

About

We use the phpmyadmin/twig-i18n-extension for the Twig extension.

How to use

composer require wdes/php-i18n-l10n

Have a look at example file example/simple.php

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

Here is an example to use them : scripts/update-example.sh

php-i18n-l10n's People

Contributors

fossabot avatar williamdes avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

php-i18n-l10n's Issues

\Twig\Environment::createTemplate will return \Twig\TemplateWrapper, not \Twig_Template

As title, the Twig will change return instance in this \Twig\Environment::createTemplate method when Twig version is v2.11.0.

The failed Travis CI build log is available here.

The previous Travis CI build log is fine because the package used the Twig version is v2.6.0.

And it also effects the preg_match_all behavior inside MemoryCache::extractDoDisplayFromCache method.

References

Script to create initial po files

Searching into /home/jaspe/Documents/wdes/doctum/build/twigcache for *.php, output Doctum.pot into /home/jaspe/Documents/wdes/doctum/locale
PoDir: /home/jaspe/Documents/wdes/doctum/locale/
File: /home/jaspe/Documents/wdes/doctum/locale/fr.po
msgfmt: /home/jaspe/Documents/wdes/doctum/locale/fr.po: AVERTISSEMENT : l'en-tête du fichier .po est absent ou invalide
                                                        AVERTISSEMENT : la conversion d'encodage ne fonctionnera pas
msgfmt: 1 erreur fatale trouvée

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.