1. Go to this page and download the library: Download wdes/php-i18n-l10n library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wdes / php-i18n-l10n example snippets
declare(strict_types = 1);
// Can be removed :)
L10n\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([]);
declare(strict_types = 1);
// Can be removed :)
L10n\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'
]
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.